Skip to content

Commit

Permalink
Add mock for gettimeofday
Browse files Browse the repository at this point in the history
  • Loading branch information
bailey-spencer committed Apr 10, 2022
1 parent 3fd4e0e commit d3f26e3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Note that, as this is not a tool intended for production, it should be required
- date_create()
- date_create_immutable()
- getdate()
- gettimeofday()
- gmdate()
- idate()
- localtime()
Expand All @@ -49,7 +50,6 @@ Note that, as this is not a tool intended for production, it should be required

- date_create_from_format()
- date_create_immutable_from_format()
- gettimeofday()
- gmmktime()
- gmstrftime()
- mktime()
Expand Down
22 changes: 22 additions & 0 deletions src/ClockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static function reset(): void
uopz_unset_return('date_create');
uopz_unset_return('date_create_immutable');
uopz_unset_return('getdate');
uopz_unset_return('gettimeofday');
uopz_unset_return('gmdate');
uopz_unset_return('idate');
uopz_unset_return('localtime');
Expand All @@ -78,6 +79,7 @@ private static function activateMocksIfNeeded(): void
uopz_set_return('date_create', self::mock_date_create(), true);
uopz_set_return('date_create_immutable', self::mock_date_create_immutable(), true);
uopz_set_return('getdate', self::mock_getdate(), true);
uopz_set_return('gettimeofday', self::mock_gettimeofday(), true);
uopz_set_return('gmdate', self::mock_gmdate(), true);
uopz_set_return('idate', self::mock_idate(), true);
uopz_set_return('localtime', self::mock_localtime(), true);
Expand Down Expand Up @@ -132,6 +134,26 @@ private static function mock_getdate(): callable
return fn (?int $timestamp = null) => $getdate_mock($timestamp);
}

/**
* @see https://www.php.net/manual/en/function.gettimeofday.php
*/
private static function mock_gettimeofday(): callable
{
$gettimeofday_mock = function (bool $as_float) {
if ($as_float) {
return (float) self::$frozenDateTime->format('U.u');
}
return [
'sec' => self::$frozenDateTime->getTimestamp(),
'usec' => (int) self::$frozenDateTime->format('u'),
'minuteswest' => (int) self::$frozenDateTime->format('Z') / -60,
'dsttime' => (int) self::$frozenDateTime->format('I'),
];
};

return fn (bool $as_float = false) => $gettimeofday_mock($as_float);
}

/**
* @see https://www.php.net/manual/en/function.gmdate.php
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/ClockMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ public function test_getdate()
);
}

public function dataProvider_gettimeofday(): array
{
return [
['2022-04-04 14:26:29.123456', 'UTC', [1649082389, 123456, 0, 0]],
['2022-03-04 14:26:29.123456', 'Europe/Kiev', [1646396789, 123456, -120, 0]],
['2022-04-04 14:26:29.123456', 'Europe/Kiev', [1649071589, 123456, -180, 1]],
];
}

/**
* @dataProvider dataProvider_gettimeofday
*/
public function test_gettimeofday(string $freezeDateTime, string $freezeTimeZone, array $expectedResult)
{
ClockMock::freeze(new \DateTime($freezeDateTime, new \DateTimeZone($freezeTimeZone)));

$this->assertEquals(array_combine(['sec', 'usec', 'minuteswest', 'dsttime'], $expectedResult), gettimeofday());
}

public function test_gettimeofday_as_float()
{
ClockMock::freeze(new \DateTime('2022-04-04 14:26:29.123456')); // UTC, +00:00

$this->assertEquals(1649082389.123456, gettimeofday(true));
}

public function test_gmdate()
{
ClockMock::freeze(new \DateTime('1986-06-05'));
Expand Down

0 comments on commit d3f26e3

Please sign in to comment.