Skip to content

Commit

Permalink
Merge pull request #12 from bailey-spencer/feature_strftime
Browse files Browse the repository at this point in the history
  • Loading branch information
asprega committed Apr 12, 2022
2 parents 3fd4e0e + d059159 commit 816d5c4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ Note that, as this is not a tool intended for production, it should be required
- date_create_immutable()
- getdate()
- gmdate()
- gmstrftime()
- idate()
- localtime()
- microtime()
- strftime()
- strtotime()
- time()
- DateTime::__construct
Expand All @@ -51,9 +53,7 @@ Note that, as this is not a tool intended for production, it should be required
- date_create_immutable_from_format()
- gettimeofday()
- gmmktime()
- gmstrftime()
- mktime()
- strftime()
- unixtojd()
- DateTime::createFromFormat
- DateTimeImmutable::createFromFormat
Expand Down
30 changes: 29 additions & 1 deletion src/ClockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public static function reset(): void
uopz_unset_return('date_create_immutable');
uopz_unset_return('getdate');
uopz_unset_return('gmdate');
uopz_unset_return('gmstrftime');
uopz_unset_return('idate');
uopz_unset_return('localtime');
uopz_unset_return('microtime');
uopz_unset_return('strftime');
uopz_unset_return('strtotime');
uopz_unset_return('time');

Expand All @@ -79,10 +81,12 @@ private static function activateMocksIfNeeded(): void
uopz_set_return('date_create_immutable', self::mock_date_create_immutable(), true);
uopz_set_return('getdate', self::mock_getdate(), true);
uopz_set_return('gmdate', self::mock_gmdate(), true);
uopz_set_return('gmstrftime', self::mock_gmstrftime(), true);
uopz_set_return('idate', self::mock_idate(), true);
uopz_set_return('localtime', self::mock_localtime(), true);
uopz_set_return('microtime', self::mock_microtime(), true);
uopz_set_return('strtotime', self::mock_strtotime(), true,);
uopz_set_return('strftime', self::mock_strftime(), true);
uopz_set_return('strtotime', self::mock_strtotime(), true);
uopz_set_return('time', self::mock_time(), true);

uopz_set_mock(\DateTime::class, DateTimeMock::class);
Expand Down Expand Up @@ -144,6 +148,18 @@ private static function mock_gmdate(): callable
return fn (string $format, ?int $timestamp = null) => $gmdate_mock($format, $timestamp);
}

/**
* @see https://www.php.net/manual/en/function.gmstrftime.php
*/
private static function mock_gmstrftime(): callable
{
$gmstrftime_mock = function (string $format, ?int $timestamp) {
return gmstrftime($format, $timestamp ?? self::$frozenDateTime->getTimestamp());
};

return fn (string $format, ?int $timestamp = null) => $gmstrftime_mock($format, $timestamp);
}

/**
* @see https://www.php.net/manual/en/function.idate.php
*/
Expand Down Expand Up @@ -184,6 +200,18 @@ private static function mock_microtime(): callable
return fn (bool $as_float = false) => $microtime_mock($as_float);
}

/**
* @see https://www.php.net/manual/en/function.strftime.php
*/
private static function mock_strftime(): callable
{
$strftime_mock = function (string $format, ?int $timestamp) {
return strftime($format, $timestamp ?? self::$frozenDateTime->getTimestamp());
};

return fn (string $format, ?int $timestamp = null) => $strftime_mock($format, $timestamp);
}

/**
* @see https://www.php.net/manual/en/function.strtotime.php
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/ClockMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public function test_gmdate()
$this->assertEquals('2010-05-22', gmdate('Y-m-d', (new \DateTime('2010-05-22'))->getTimestamp()));
}

public function test_gmstrftime()
{
ClockMock::freeze($fakeNow = new \DateTime('2022-04-04 14:26:29', new \DateTimeZone('Europe/Kiev')));

$this->assertEquals('2022-04-04 11:26:29', gmstrftime('%F %T'));
}

public function test_idate()
{
ClockMock::freeze(new \DateTime('1986-06-05'));
Expand Down Expand Up @@ -185,6 +192,13 @@ public function test_microtime()
$this->assertSame(1619000631.123456, microtime(true));
}

public function test_strftime()
{
ClockMock::freeze($fakeNow = new \DateTimeImmutable('2022-04-04 14:26:29'));

$this->assertEquals('2022-04-04 14:26:29', strftime('%F %T'));
}

public function test_strtotime()
{
ClockMock::freeze($fakeNow = new \DateTimeImmutable('1986-06-05'));
Expand Down

0 comments on commit 816d5c4

Please sign in to comment.