Skip to content

[PhpUnitBridge] Add strtotime() to ClockMock #60424

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add support for mocking the `strtotime()` function

7.3
---

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Bridge/PhpUnit/ClockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ public static function hrtime($asNumber = false)
return [(int) self::$now, (int) $ns];
}

/**
* @return false|int
*/
public static function strtotime(string $datetime, ?int $timestamp = null)
{
if (null === $timestamp) {
$timestamp = self::time();
}

return \strtotime($datetime, $timestamp);
}

public static function register($class): void
{
$self = static::class;
Expand Down Expand Up @@ -161,6 +173,11 @@ function hrtime(\$asNumber = false)
{
return \\$self::hrtime(\$asNumber);
}

function strtotime(\$datetime, \$timestamp = null)
{
return \\$self::strtotime(\$datetime, \$timestamp);
}
EOPHP
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public function testHrTimeAsNumber()
{
$this->assertSame(1234567890125000000, hrtime(true));
}

public function testStrToTime()
{
$this->assertSame(1234567890, strtotime('now'));
}
}
Loading