|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of the Nexus framework. |
| 7 | + * |
| 8 | + * (c) John Paul E. Balandan, CPA <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Nexus\Tests\Clock; |
| 15 | + |
| 16 | +use Nexus\Clock\SystemClock; |
| 17 | +use Nexus\PHPUnit\Tachycardia\Attribute\TimeLimit; |
| 18 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[CoversClass(SystemClock::class)] |
| 26 | +#[Group('unit')] |
| 27 | +final class SystemClockTest extends TestCase |
| 28 | +{ |
| 29 | + public function testTimezoneOfSystemClock(): void |
| 30 | + { |
| 31 | + $clock = new SystemClock('UTC'); |
| 32 | + self::assertSame('UTC', $clock->now()->getTimezone()->getName()); |
| 33 | + |
| 34 | + $clock = new SystemClock(date_default_timezone_get()); |
| 35 | + self::assertSame(date_default_timezone_get(), $clock->now()->getTimezone()->getName()); |
| 36 | + |
| 37 | + $clock = new SystemClock(new \DateTimeZone('Asia/Manila')); |
| 38 | + self::assertSame('Asia/Manila', $clock->now()->getTimezone()->getName()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testNowMovesWithTime(): void |
| 42 | + { |
| 43 | + $clock = new SystemClock('UTC'); |
| 44 | + $before = new \DateTimeImmutable(); |
| 45 | + usleep(10); |
| 46 | + $now = $clock->now(); |
| 47 | + usleep(10); |
| 48 | + $after = new \DateTimeImmutable(); |
| 49 | + |
| 50 | + self::assertGreaterThan($before, $now); |
| 51 | + self::assertLessThan($after, $now); |
| 52 | + } |
| 53 | + |
| 54 | + #[TimeLimit(2.10)] |
| 55 | + public function testClockSleeps(): void |
| 56 | + { |
| 57 | + $clock = new SystemClock('UTC'); |
| 58 | + $tz = $clock->now()->getTimezone()->getName(); |
| 59 | + |
| 60 | + $before = (float) $clock->now()->format('U.u'); |
| 61 | + $clock->sleep(2.0); |
| 62 | + $now = (float) $clock->now()->format('U.u'); |
| 63 | + $clock->sleep(0.0001); |
| 64 | + $after = (float) $clock->now()->format('U.u'); |
| 65 | + |
| 66 | + self::assertEqualsWithDelta(2.0, $now - $before, 0.05); |
| 67 | + self::assertLessThan($after, $now); |
| 68 | + self::assertSame($tz, $clock->now()->getTimezone()->getName()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testClockDoesNotSleepOnNegativeOrZeroSeconds(): void |
| 72 | + { |
| 73 | + $clock = new SystemClock('UTC'); |
| 74 | + $tz = $clock->now()->getTimezone()->getName(); |
| 75 | + |
| 76 | + $before = (float) $clock->now()->format('U.u'); |
| 77 | + $clock->sleep(-2.0); |
| 78 | + $now = (float) $clock->now()->format('U.u'); |
| 79 | + $clock->sleep(0.0); |
| 80 | + $after = (float) $clock->now()->format('U.u'); |
| 81 | + |
| 82 | + // account for latency in execution time |
| 83 | + self::assertEqualsWithDelta(0.0, $now - $before, 0.01); |
| 84 | + self::assertEqualsWithDelta(0.0, $after - $now, 0.01); |
| 85 | + self::assertSame($tz, $clock->now()->getTimezone()->getName()); |
| 86 | + } |
| 87 | +} |
0 commit comments