Skip to content

Commit

Permalink
Merge pull request #174 from cakephp/clone-testnow
Browse files Browse the repository at this point in the history
use a copy of  date/time for testNow
  • Loading branch information
markstory authored Jun 23, 2018
2 parents a0293d2 + c505236 commit 243bb7b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function __construct($time = 'now', $tz = null)
return;
}

$testNow = clone $testNow;
if ($relative) {
$testNow = $testNow->modify($time);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ public function __construct($time = 'now')
return;
}

$testNow = clone $testNow;
if ($relative) {
$testNow = $testNow->modify($time);
$time = $this->stripRelativeTime($time);
if (strlen($time) > 0) {
$testNow = $testNow->modify($time);
}
}

if ($tz !== $testNow->getTimezone()) {
Expand Down
5 changes: 4 additions & 1 deletion src/MutableDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ public function __construct($time = 'now')

$testNow = clone $testNow;
if ($relative) {
$testNow = $testNow->modify($time);
$time = $this->stripRelativeTime($time);
if (strlen($time) > 0) {
$testNow = $testNow->modify($time);
}
}

if ($tz !== $testNow->getTimezone()) {
Expand Down
11 changes: 11 additions & 0 deletions src/Traits/FrozenTimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ protected function stripTime($time)
return preg_replace('/\d{1,2}:\d{1,2}:\d{1,2}(?:\.\d+)?/', '00:00:00', $time);
}

/**
* Remove time components from strtotime relative strings.
*
* @param string $time The input expression
* @return string The output expression with no time modifiers.
*/
protected function stripRelativeTime($time)
{
return preg_replace('/([-+]\s*\d+\s(?:minutes|seconds|hours|microseconds))/', '', $time);
}

/**
* Modify the time on the Date.
*
Expand Down
66 changes: 61 additions & 5 deletions tests/DateTime/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testTestingAidsWithTestNowSet($class)
$class::setTestNow($notNow);

$this->assertTrue($class::hasTestNow());
$this->assertSame($notNow, $class::getTestNow());
$this->assertEquals($notNow, $class::getTestNow());
}

/**
Expand Down Expand Up @@ -86,6 +86,62 @@ public function testNowWithTestValueSet($class)
$this->assertEquals($notNow, $class::now());
}

/**
* Ensure that using test now doesn't mutate test now.
*
* @dataProvider classNameProvider
* @return void
*/
public function testNowNoMutateDateTime($class)
{
$value = '2018-06-21 10:11:12';
$notNow = new MutableDateTime($value);
$class::setTestNow($notNow);

$instance = new $class('-10 minutes');
$this->assertSame('10:01:12', $instance->format('H:i:s'));

$instance = new $class('-10 minutes');
$this->assertSame('10:01:12', $instance->format('H:i:s'));
}

/**
* Ensure that using test now doesn't mutate test now.
*
* @dataProvider dateClassProvider
* @return void
*/
public function testNowNoMutateDate($class)
{
$value = '2018-06-21 10:11:12';
$notNow = new MutableDateTime($value);
$class::setTestNow($notNow);

$instance = new $class('-1 day');
$this->assertSame('2018-06-20 00:00:00', $instance->format('Y-m-d H:i:s'));

$instance = new $class('-1 day');
$this->assertSame('2018-06-20 00:00:00', $instance->format('Y-m-d H:i:s'));
}

/**
* Ensure that setting a datetime into test now doesn't violate date semantics
*
* Modifying date instances by hours should not change the date.
*
* @dataProvider dateClassProvider
* @return void
*/
public function testNowTestDateTimeConstraints($class)
{
$value = '2018-06-21 10:11:12';
$notNow = new MutableDateTime($value);
$class::setTestNow($notNow);

$instance = new $class('-23 hours');
$this->assertSame('2018-06-21 00:00:00', $instance->format('Y-m-d H:i:s'));
}

/**
* @dataProvider classNameProvider
* @return void
Expand Down Expand Up @@ -203,9 +259,9 @@ public function testSetTestNowSingular($class)
$c = new $class('2016-01-03 00:00:00', 'Europe/Copenhagen');
$class::setTestNow($c);

$this->assertSame($c, MutableDate::getTestNow());
$this->assertSame($c, Date::getTestNow());
$this->assertSame($c, Chronos::getTestNow());
$this->assertSame($c, MutableDateTime::getTestNow());
$this->assertEquals($c, MutableDate::getTestNow());
$this->assertEquals($c, Date::getTestNow());
$this->assertEquals($c, Chronos::getTestNow());
$this->assertEquals($c, MutableDateTime::getTestNow());
}
}

0 comments on commit 243bb7b

Please sign in to comment.