Skip to content

Commit

Permalink
[8.x] Adds an ability for the Wormhole class to take us back to the p…
Browse files Browse the repository at this point in the history
…resent. (#35261)

* Adds an ability for the Wormhole to take us back to the present.

* Fixes styling.
  • Loading branch information
ambengers authored Nov 18, 2020
1 parent 3f6bb3b commit c35d76e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public function travelTo(DateTimeInterface $date, $callback = null)
*/
public function travelBack()
{
Carbon::setTestNow();

return Carbon::now();
return Wormhole::back();
}
}
12 changes: 12 additions & 0 deletions src/Illuminate/Foundation/Testing/Wormhole.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ public function years($callback = null)
return $this->handleCallback($callback);
}

/**
* Travel back to the current time.
*
* @return \DateTimeInterface
*/
public static function back()
{
Carbon::setTestNow();

return Carbon::now();
}

/**
* Handle the given optional execution callback.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Foundation/Testing/WormholeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Illuminate\Tests\Foundation\Testing;

use Illuminate\Foundation\Testing\Wormhole;
use PHPUnit\Framework\TestCase;

class WormholeTest extends TestCase
{
public function testCanTravelBackToPresent()
{
// Preserve the timelines we want to compare the reality with..
$present = now();
$future = now()->addDays(10);

// Travel in time..
(new Wormhole(10))->days();

// Assert we are now in the future..
$this->assertEquals($future->format('Y-m-d'), now()->format('Y-m-d'));

// Assert we can go back to the present..
$this->assertEquals($present->format('Y-m-d'), Wormhole::back()->format('Y-m-d'));
}
}

0 comments on commit c35d76e

Please sign in to comment.