Skip to content

Commit

Permalink
[8.x] Ensure both Carbon and CarbonImmutable get the same test now (#…
Browse files Browse the repository at this point in the history
…36117)

* [8.x] Ensure both Carbon and CarbonImmutable get the same test now.

In project where we might use mixes of Carbon and CarbonImmutable you
need to remember to always setTestNow() for both scenario, this PR
solved it by ensuring both instance has the same date and time during
tests.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Refactor test

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone authored Feb 2, 2021
1 parent 6319d94 commit 046d119
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/Illuminate/Foundation/Testing/Wormhole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Foundation\Testing;

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

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

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

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

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

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

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

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

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

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

/**
Expand All @@ -137,7 +137,7 @@ protected function handleCallback($callback)
{
if ($callback) {
return tap($callback(), function () {
Date::setTestNow();
Carbon::setTestNow();
});
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/Illuminate/Support/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
namespace Illuminate\Support;

use Carbon\Carbon as BaseCarbon;
use Carbon\CarbonImmutable as BaseCarbonImmutable;

class Carbon extends BaseCarbon
{
//
/**
* {@inheritdoc}
*/
public static function setTestNow($testNow = null)
{
BaseCarbon::setTestNow($testNow);
BaseCarbonImmutable::setTestNow($testNow);
}
}
10 changes: 10 additions & 0 deletions tests/Support/SupportCarbonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BadMethodCallException;
use Carbon\Carbon as BaseCarbon;
use Carbon\CarbonImmutable as BaseCarbonImmutable;
use DateTime;
use DateTimeInterface;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -108,4 +109,13 @@ public function testDeserializationOccursCorrectly()

$this->assertInstanceOf(Carbon::class, $deserialized);
}

public function testSetTestNowWillPersistBetweenImmutableAndMutableInstance()
{
Carbon::setTestNow(new Carbon('2017-06-27 13:14:15.000000'));

$this->assertSame('2017-06-27 13:14:15', Carbon::now()->toDateTimeString());
$this->assertSame('2017-06-27 13:14:15', BaseCarbon::now()->toDateTimeString());
$this->assertSame('2017-06-27 13:14:15', BaseCarbonImmutable::now()->toDateTimeString());
}
}

0 comments on commit 046d119

Please sign in to comment.