|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Cryptography\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Tempest\Clock\Clock; |
| 7 | +use Tempest\Clock\GenericClock; |
| 8 | +use Tempest\Clock\MockClock; |
| 9 | +use Tempest\Cryptography\Timelock; |
| 10 | +use Tempest\DateTime\Duration; |
| 11 | + |
| 12 | +final class TimelockTest extends TestCase |
| 13 | +{ |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + if (! interface_exists(Clock::class)) { |
| 19 | + $this->markTestSkipped('The Clock interface is not available. This test requires the `tempest/clock` package.'); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public function test_callback_is_executed(): void |
| 24 | + { |
| 25 | + $clock = new GenericClock(); |
| 26 | + $result = new Timelock($clock)->invoke(fn () => 'hello', Duration::zero()); |
| 27 | + |
| 28 | + $this->assertSame('hello', $result); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_locks_for_duration(): void |
| 32 | + { |
| 33 | + $clock = new GenericClock(); |
| 34 | + $start = microtime(true); |
| 35 | + |
| 36 | + $timelock = new Timelock($clock); |
| 37 | + $timelock->invoke(fn () => null, Duration::milliseconds(100)); |
| 38 | + |
| 39 | + $elapsed = microtime(true) - $start; |
| 40 | + |
| 41 | + $this->assertGreaterThanOrEqual(0.1, $elapsed, 'The timelock did not wait for the specified duration.'); |
| 42 | + $this->assertLessThan(0.2, $elapsed, 'The timelock waited for too long.'); |
| 43 | + } |
| 44 | + |
| 45 | + public function test_return_early(): void |
| 46 | + { |
| 47 | + $clock = new GenericClock(); |
| 48 | + $timelock = new Timelock($clock); |
| 49 | + |
| 50 | + $start = microtime(true); |
| 51 | + $timelock->invoke( |
| 52 | + callback: fn (Timelock $lock) => $lock->canReturnEarly = true, |
| 53 | + duration: Duration::milliseconds(100), |
| 54 | + ); |
| 55 | + $elapsed = microtime(true) - $start; |
| 56 | + |
| 57 | + $this->assertLessThan(0.1, $elapsed, 'The timelock did not return early as expected.'); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_throws_exception_after_delay(): void |
| 61 | + { |
| 62 | + $clock = new GenericClock(); |
| 63 | + $timelock = new Timelock($clock); |
| 64 | + |
| 65 | + $start = microtime(true); |
| 66 | + |
| 67 | + try { |
| 68 | + $timelock->invoke( |
| 69 | + callback: fn () => throw new \RuntimeException('This is an error.'), |
| 70 | + duration: Duration::milliseconds(100), |
| 71 | + ); |
| 72 | + } catch (\RuntimeException) { |
| 73 | + $elapsed = microtime(true) - $start; |
| 74 | + $this->assertGreaterThanOrEqual(0.1, $elapsed, 'The exception was thrown before the timelock duration elapsed.'); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function test_uses_clock_to_sleep(): void |
| 79 | + { |
| 80 | + $clock = new MockClock(); |
| 81 | + $timelock = new Timelock($clock); |
| 82 | + |
| 83 | + $ms = $clock->timestamp()->getMilliseconds(); |
| 84 | + |
| 85 | + $timelock->invoke( |
| 86 | + callback: fn () => null, |
| 87 | + duration: Duration::milliseconds(300), |
| 88 | + ); |
| 89 | + |
| 90 | + $elapsed = $clock->timestamp()->getMilliseconds() - $ms; |
| 91 | + |
| 92 | + $this->assertSame(300, $elapsed); |
| 93 | + } |
| 94 | +} |
0 commit comments