Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
70 / 70 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use Phexium\Plugin\Clock\Adapter\FrozenClock; |
| 11 | use Phexium\Plugin\Clock\Adapter\OffsetClock; |
| 12 | |
| 13 | test('Should return inner clock time without offset', function (): void { |
| 14 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 15 | $clock = new OffsetClock($inner); |
| 16 | |
| 17 | $result = $clock->now(); |
| 18 | |
| 19 | expect($result->format(DateTimeInterface::ATOM))->toBe('2025-01-01T12:00:00+00:00'); |
| 20 | }); |
| 21 | |
| 22 | test('advanceSeconds advances the time', function (): void { |
| 23 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 24 | $clock = new OffsetClock($inner); |
| 25 | |
| 26 | $clock->advanceSeconds(60); |
| 27 | |
| 28 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T12:01:00+00:00'); |
| 29 | }); |
| 30 | |
| 31 | test('advance with DateInterval advances the time', function (): void { |
| 32 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 33 | $clock = new OffsetClock($inner); |
| 34 | |
| 35 | $clock->advance(new DateInterval('PT2H30M')); |
| 36 | |
| 37 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T14:30:00+00:00'); |
| 38 | }); |
| 39 | |
| 40 | test('Should accumulate multiple advances', function (): void { |
| 41 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 42 | $clock = new OffsetClock($inner); |
| 43 | |
| 44 | $clock->advanceSeconds(30); |
| 45 | $clock->advanceSeconds(30); |
| 46 | |
| 47 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T12:01:00+00:00'); |
| 48 | }); |
| 49 | |
| 50 | test('rewindSeconds rewinds the time', function (): void { |
| 51 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 52 | $clock = new OffsetClock($inner); |
| 53 | |
| 54 | $clock->rewindSeconds(60); |
| 55 | |
| 56 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T11:59:00+00:00'); |
| 57 | }); |
| 58 | |
| 59 | test('rewind with DateInterval rewinds the time', function (): void { |
| 60 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 61 | $clock = new OffsetClock($inner); |
| 62 | |
| 63 | $clock->rewind(new DateInterval('PT1H')); |
| 64 | |
| 65 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T11:00:00+00:00'); |
| 66 | }); |
| 67 | |
| 68 | test('Should accumulate multiple rewinds', function (): void { |
| 69 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 70 | $clock = new OffsetClock($inner); |
| 71 | |
| 72 | $clock->rewindSeconds(30); |
| 73 | $clock->rewindSeconds(30); |
| 74 | |
| 75 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T11:59:00+00:00'); |
| 76 | }); |
| 77 | |
| 78 | test('Should combine advance and rewind', function (): void { |
| 79 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 80 | $clock = new OffsetClock($inner); |
| 81 | |
| 82 | $clock->advanceSeconds(120); |
| 83 | $clock->rewindSeconds(60); |
| 84 | |
| 85 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T12:01:00+00:00'); |
| 86 | }); |
| 87 | |
| 88 | test('reset resets offset to zero', function (): void { |
| 89 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 90 | $clock = new OffsetClock($inner); |
| 91 | |
| 92 | $clock->advanceSeconds(3600); |
| 93 | $clock->reset(); |
| 94 | |
| 95 | expect($clock->now()->format(DateTimeInterface::ATOM))->toBe('2025-01-01T12:00:00+00:00'); |
| 96 | }); |
| 97 | |
| 98 | test('Should advance by large amounts', function (): void { |
| 99 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 100 | $clock = new OffsetClock($inner); |
| 101 | |
| 102 | $clock->advanceSeconds(24 * 60 * 60); |
| 103 | |
| 104 | expect($clock->now()->format('Y-m-d'))->toBe('2025-01-02'); |
| 105 | }); |
| 106 | |
| 107 | test('advance with days in DateInterval', function (): void { |
| 108 | $inner = new FrozenClock('2025-01-01T12:00:00+00:00'); |
| 109 | $clock = new OffsetClock($inner); |
| 110 | |
| 111 | $clock->advance(new DateInterval('P7D')); |
| 112 | |
| 113 | expect($clock->now()->format('Y-m-d'))->toBe('2025-01-08'); |
| 114 | }); |