Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Change Wormhole to use the Date Factory #35421

Merged
merged 2 commits into from Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Illuminate/Foundation/Testing/Wormhole.php
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Foundation\Testing;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;

class Wormhole
{
Expand Down Expand Up @@ -32,7 +32,7 @@ public function __construct($value)
*/
public function milliseconds($callback = null)
{
Carbon::setTestNow(Carbon::now()->addMilliseconds($this->value));
Date::setTestNow(Date::now()->addMilliseconds($this->value));

return $this->handleCallback($callback);
}
Expand All @@ -45,7 +45,7 @@ public function milliseconds($callback = null)
*/
public function seconds($callback = null)
{
Carbon::setTestNow(Carbon::now()->addSeconds($this->value));
Date::setTestNow(Date::now()->addSeconds($this->value));

return $this->handleCallback($callback);
}
Expand All @@ -58,7 +58,7 @@ public function seconds($callback = null)
*/
public function minutes($callback = null)
{
Carbon::setTestNow(Carbon::now()->addMinutes($this->value));
Date::setTestNow(Date::now()->addMinutes($this->value));

return $this->handleCallback($callback);
}
Expand All @@ -71,7 +71,7 @@ public function minutes($callback = null)
*/
public function hours($callback = null)
{
Carbon::setTestNow(Carbon::now()->addHours($this->value));
Date::setTestNow(Date::now()->addHours($this->value));

return $this->handleCallback($callback);
}
Expand All @@ -84,7 +84,7 @@ public function hours($callback = null)
*/
public function days($callback = null)
{
Carbon::setTestNow(Carbon::now()->addDays($this->value));
Date::setTestNow(Date::now()->addDays($this->value));

return $this->handleCallback($callback);
}
Expand All @@ -97,7 +97,7 @@ public function days($callback = null)
*/
public function weeks($callback = null)
{
Carbon::setTestNow(Carbon::now()->addWeeks($this->value));
Date::setTestNow(Date::now()->addWeeks($this->value));

return $this->handleCallback($callback);
}
Expand All @@ -110,7 +110,7 @@ public function weeks($callback = null)
*/
public function years($callback = null)
{
Carbon::setTestNow(Carbon::now()->addYears($this->value));
Date::setTestNow(Date::now()->addYears($this->value));

return $this->handleCallback($callback);
}
Expand All @@ -122,9 +122,9 @@ public function years($callback = null)
*/
public static function back()
{
Carbon::setTestNow();
Date::setTestNow();

return Carbon::now();
return Date::now();
}

/**
Expand All @@ -137,7 +137,7 @@ protected function handleCallback($callback)
{
if ($callback) {
return tap($callback(), function () {
Carbon::setTestNow();
Date::setTestNow();
});
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Foundation/Testing/WormholeTest.php
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Tests\Foundation\Testing;

use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\Wormhole;
use Illuminate\Support\Facades\Date;
use PHPUnit\Framework\TestCase;

class WormholeTest extends TestCase
Expand All @@ -22,4 +24,26 @@ public function testCanTravelBackToPresent()
// Assert we can go back to the present..
$this->assertEquals($present->format('Y-m-d'), Wormhole::back()->format('Y-m-d'));
}

public function testCarbonImmutableCompatibility()
{
// Tell the Date Factory to use CarbonImmutable...
Date::use(CarbonImmutable::class);

// Record what time it is in 10 days...
$present = now();
$future = $present->addDays(10);

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

// Assert that the present time didn't get mutated...
$this->assertNotEquals($future->format('Y-m-d'), $present->format('Y-m-d'));

// Assert the time travel was successful...
$this->assertEquals($future->format('Y-m-d'), now()->format('Y-m-d'));

// Restore the default Date Factory...
Date::useDefault();
}
}