-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\Clock\Adapter; | ||
|
||
use DateTimeImmutable; | ||
use Orisai\Clock\Clock; | ||
use Symfony\Component\Clock\ClockInterface; | ||
use function func_get_args; | ||
use function trigger_error; | ||
use const E_USER_WARNING; | ||
|
||
final class SymfonyToOrisaiClockAdapter implements Clock | ||
{ | ||
|
||
private ClockInterface $clock; | ||
|
||
public function __construct(ClockInterface $clock) | ||
{ | ||
$this->clock = $clock; | ||
} | ||
|
||
public function now(): DateTimeImmutable | ||
{ | ||
return $this->clock->now(); | ||
} | ||
|
||
/** | ||
* @infection-ignore-all | ||
*/ | ||
public function sleep(int $seconds = 0, int $milliseconds = 0, int $microseconds = 0): void | ||
{ | ||
if (func_get_args() === []) { | ||
trigger_error( | ||
'Arguments must be passed to method sleep(), otherwise it does not do anything.', | ||
E_USER_WARNING, | ||
); | ||
} | ||
|
||
$this->clock->sleep( | ||
$seconds | ||
+ ($milliseconds / 1_000) | ||
+ ($microseconds / 1_000_000), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Clock\Unit; | ||
|
||
use DateTimeImmutable; | ||
use Orisai\Clock\Adapter\SymfonyToOrisaiClockAdapter; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Clock\ClockInterface; | ||
use Symfony\Component\Clock\MockClock; | ||
use Symfony\Component\Clock\NativeClock; | ||
use function error_get_last; | ||
use function interface_exists; | ||
use function microtime; | ||
use function usleep; | ||
|
||
final class SymfonyToOrisaiClockAdapterTest extends TestCase | ||
{ | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
// Workaround for minimal PHP version - Symfony requires PHP 8.1, we only 7.4 | ||
if (!interface_exists(ClockInterface::class)) { | ||
self::markTestSkipped('symfony/clock is not installed.'); | ||
} | ||
} | ||
|
||
public function testCurrentTime(): void | ||
{ | ||
$clock = new SymfonyToOrisaiClockAdapter(new NativeClock()); | ||
|
||
$start = microtime(true); | ||
usleep(1); | ||
|
||
$now = $clock->now(); | ||
|
||
usleep(1); | ||
$stop = microtime(true); | ||
|
||
self::assertGreaterThan($start, (float) $now->format('U.u')); | ||
self::assertLessThan($stop, (float) $now->format('U.u')); | ||
} | ||
|
||
public function testSleep(): void | ||
{ | ||
$clock = new SymfonyToOrisaiClockAdapter( | ||
new MockClock(DateTimeImmutable::createFromFormat('U', '10')), | ||
); | ||
|
||
self::assertSame( | ||
'10.000000', | ||
$clock->now()->format('U.u'), | ||
); | ||
|
||
$clock->sleep(1, 2, 3); | ||
self::assertSame( | ||
'11.002003', | ||
$clock->now()->format('U.u'), | ||
); | ||
} | ||
|
||
public function testSleepNoArgs(): void | ||
{ | ||
$clock = new SymfonyToOrisaiClockAdapter( | ||
new MockClock(DateTimeImmutable::createFromFormat('U', '0')), | ||
); | ||
|
||
@$clock->sleep(); | ||
|
||
self::assertSame( | ||
'Arguments must be passed to method sleep(), otherwise it does not do anything.', | ||
error_get_last()['message'] ?? null, | ||
); | ||
self::assertSame( | ||
'0.000000', | ||
$clock->now()->format('U.u'), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters