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

Change Carbon::setTestNow() to be able to parse a string. #847

Merged
merged 5 commits into from
Jan 13, 2017
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
7 changes: 4 additions & 3 deletions src/Carbon/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,18 +1021,19 @@ public static function setWeekendDays($days)
* - A call to the static now() method, ex. Carbon::now()
* - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null)
* - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now')
* - When a string containing the desired time is passed to Carbon::parse().
*
* Note the timezone parameter was left out of the examples above and
* has no affect as the mock value will be returned regardless of its value.
*
* To clear the test instance call this method using the default
* parameter of null.
*
* @param \Carbon\Carbon|null $testNow
* @param \Carbon\Carbon|string|null $testNow
*/
public static function setTestNow(Carbon $testNow = null)
public static function setTestNow($testNow = null)
{
static::$testNow = $testNow;
static::$testNow = is_string($testNow) ? static::parse($testNow) : $testNow;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Carbon/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function testTestingAidsWithTestNowSet()
$this->assertSame($testNow, Carbon::getTestNow());
}

public function testTestingAidsWithTestNowSetToString()
{
Carbon::setTestNow('2016-11-23');
$this->assertTrue(Carbon::hasTestNow());
$this->assertEquals(Carbon::getTestNow(), Carbon::parse('2016-11-23'));
}

public function testConstructorWithTestValueSet()
{
$testNow = Carbon::yesterday();
Expand Down