From e75c7a57803f60f0c4b5d431288847aa4d7a5ee2 Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Mon, 16 Jan 2017 21:16:55 +0100 Subject: [PATCH] Always Carbon::setTestNow --- tests/AbstractTestCase.php | 10 ++++++++++ tests/Carbon/ConstructTest.php | 2 ++ tests/Carbon/CreateTest.php | 3 +-- tests/Carbon/SettersTest.php | 1 - tests/Carbon/TestingAidsTest.php | 21 +++++++++------------ 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index fddfdc379f..74c58b30a6 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -18,6 +18,14 @@ abstract class AbstractTestCase extends PHPUnit_Framework_TestCase { + /** + * @var \Carbon\Carbon + */ + protected $now; + + /** + * @var string + */ private $saveTz; protected function setUp() @@ -26,6 +34,8 @@ protected function setUp() $this->saveTz = date_default_timezone_get(); date_default_timezone_set('America/Toronto'); + + Carbon::setTestNow($this->now = Carbon::now()); } protected function tearDown() diff --git a/tests/Carbon/ConstructTest.php b/tests/Carbon/ConstructTest.php index c241870b70..a894f146f5 100644 --- a/tests/Carbon/ConstructTest.php +++ b/tests/Carbon/ConstructTest.php @@ -38,12 +38,14 @@ public function testParseCreatesAnInstanceDefaultToNow() public function testWithFancyString() { + Carbon::setTestNow(Carbon::today()); $c = new Carbon('first day of January 2008'); $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0); } public function testParseWithFancyString() { + Carbon::setTestNow(Carbon::today()); $c = Carbon::parse('first day of January 2008'); $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0); } diff --git a/tests/Carbon/CreateTest.php b/tests/Carbon/CreateTest.php index 7fdebc71bd..52d60d39b6 100644 --- a/tests/Carbon/CreateTest.php +++ b/tests/Carbon/CreateTest.php @@ -25,9 +25,8 @@ public function testCreateReturnsDatingInstance() public function testCreateWithDefaults() { - Carbon::setTestNow($now = Carbon::now()); $d = Carbon::create(); - $this->assertSame($d->getTimestamp(), $now->getTimestamp()); + $this->assertSame($d->getTimestamp(), Carbon::now()->getTimestamp()); } public function testCreateWithYear() diff --git a/tests/Carbon/SettersTest.php b/tests/Carbon/SettersTest.php index c886e64863..810c750d2d 100644 --- a/tests/Carbon/SettersTest.php +++ b/tests/Carbon/SettersTest.php @@ -291,7 +291,6 @@ public function testSetTimeFromTimeString($hour, $minute, $second, $time) Carbon::setTestNow(Carbon::create(2016, 2, 12, 1, 2, 3)); $d = Carbon::now()->setTimeFromTimeString($time); $this->assertCarbon($d, 2016, 2, 12, $hour, $minute, $second); - Carbon::setTestNow(); } public function dataProviderTestSetTimeFromTimeString() diff --git a/tests/Carbon/TestingAidsTest.php b/tests/Carbon/TestingAidsTest.php index b5b815cf8b..59db8ec0d8 100644 --- a/tests/Carbon/TestingAidsTest.php +++ b/tests/Carbon/TestingAidsTest.php @@ -26,11 +26,10 @@ public function testTestingAidsWithTestNowNotSet() public function testTestingAidsWithTestNowSet() { - $testNow = Carbon::yesterday(); - Carbon::setTestNow($testNow); + Carbon::setTestNow($yesterday = Carbon::yesterday()); $this->assertTrue(Carbon::hasTestNow()); - $this->assertSame($testNow, Carbon::getTestNow()); + $this->assertSame($yesterday, Carbon::getTestNow()); } public function testTestingAidsWithTestNowSetToString() @@ -42,21 +41,19 @@ public function testTestingAidsWithTestNowSetToString() public function testConstructorWithTestValueSet() { - $testNow = Carbon::yesterday(); - Carbon::setTestNow($testNow); + Carbon::setTestNow($yesterday = Carbon::yesterday()); - $this->assertEquals($testNow, new Carbon()); - $this->assertEquals($testNow, new Carbon(null)); - $this->assertEquals($testNow, new Carbon('')); - $this->assertEquals($testNow, new Carbon('now')); + $this->assertEquals($yesterday, new Carbon()); + $this->assertEquals($yesterday, new Carbon(null)); + $this->assertEquals($yesterday, new Carbon('')); + $this->assertEquals($yesterday, new Carbon('now')); } public function testNowWithTestValueSet() { - $testNow = Carbon::yesterday(); - Carbon::setTestNow($testNow); + Carbon::setTestNow($yesterday = Carbon::yesterday()); - $this->assertEquals($testNow, Carbon::now()); + $this->assertEquals($yesterday, Carbon::now()); } public function testParseWithTestValueSet()