Skip to content

Commit

Permalink
Always Carbon::setTestNow
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmichot committed Jan 16, 2017
1 parent 7a3d0fa commit e75c7a5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
10 changes: 10 additions & 0 deletions tests/AbstractTestCase.php
Expand Up @@ -18,6 +18,14 @@

abstract class AbstractTestCase extends PHPUnit_Framework_TestCase
{
/**
* @var \Carbon\Carbon
*/
protected $now;

/**
* @var string
*/
private $saveTz;

protected function setUp()
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions tests/Carbon/ConstructTest.php
Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Carbon/CreateTest.php
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion tests/Carbon/SettersTest.php
Expand Up @@ -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()
Expand Down
21 changes: 9 additions & 12 deletions tests/Carbon/TestingAidsTest.php
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit e75c7a5

Please sign in to comment.