|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Middleware; |
| 4 | + |
| 5 | +use Fyennyi\AsyncCache\CacheOptions; |
| 6 | +use Fyennyi\AsyncCache\Core\CacheContext; |
| 7 | +use Fyennyi\AsyncCache\Enum\CacheStrategy; |
| 8 | +use Fyennyi\AsyncCache\Middleware\StrategyMiddleware; |
| 9 | +use Fyennyi\AsyncCache\Model\CachedItem; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Psr\EventDispatcher\EventDispatcherInterface; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | +use Symfony\Component\Clock\MockClock; |
| 15 | +use function React\Async\await; |
| 16 | +use function React\Promise\resolve; |
| 17 | + |
| 18 | +class StrategyMiddlewareTest extends TestCase |
| 19 | +{ |
| 20 | + private MockObject|LoggerInterface $logger; |
| 21 | + private MockObject|EventDispatcherInterface $dispatcher; |
| 22 | + private MockClock $clock; |
| 23 | + private StrategyMiddleware $middleware; |
| 24 | + |
| 25 | + protected function setUp() : void |
| 26 | + { |
| 27 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 28 | + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); |
| 29 | + $this->clock = new MockClock(); |
| 30 | + $this->middleware = new StrategyMiddleware($this->logger, $this->dispatcher); |
| 31 | + } |
| 32 | + |
| 33 | + public function testCallsNextOnMiss() : void |
| 34 | + { |
| 35 | + $context = new CacheContext('k', fn () => null, new CacheOptions(), $this->clock); |
| 36 | + $next = fn () => resolve('from_next'); |
| 37 | + |
| 38 | + $this->assertSame('from_next', await($this->middleware->handle($context, $next))); |
| 39 | + } |
| 40 | + |
| 41 | + public function testReturnsFreshHitImmediately() : void |
| 42 | + { |
| 43 | + $item = new CachedItem('data', $this->clock->now()->getTimestamp() + 100); |
| 44 | + $context = new CacheContext('k', fn () => null, new CacheOptions(), $this->clock); |
| 45 | + $context->stale_item = $item; |
| 46 | + $next = fn () => resolve('should_not_be_called'); |
| 47 | + |
| 48 | + $this->assertSame('data', await($this->middleware->handle($context, $next))); |
| 49 | + } |
| 50 | + |
| 51 | + public function testBackgroundStrategyReturnsStaleAndRefreshesInBackground() : void |
| 52 | + { |
| 53 | + $item = new CachedItem('stale_data', $this->clock->now()->getTimestamp() - 10); |
| 54 | + $context = new CacheContext('k', fn () => null, new CacheOptions(strategy: CacheStrategy::Background), $this->clock); |
| 55 | + $context->stale_item = $item; |
| 56 | + |
| 57 | + $nextCalled = false; |
| 58 | + $next = function () use (&$nextCalled) { |
| 59 | + $nextCalled = true; |
| 60 | + return resolve('refreshed_data'); |
| 61 | + }; |
| 62 | + |
| 63 | + $this->assertSame('stale_data', await($this->middleware->handle($context, $next))); |
| 64 | + $this->assertTrue($nextCalled); |
| 65 | + } |
| 66 | + |
| 67 | + public function testStrictStrategyWaitsForFreshData() : void |
| 68 | + { |
| 69 | + $item = new CachedItem('stale_data', $this->clock->now()->getTimestamp() - 10); |
| 70 | + $context = new CacheContext('k', fn () => null, new CacheOptions(strategy: CacheStrategy::Strict), $this->clock); |
| 71 | + $context->stale_item = $item; |
| 72 | + |
| 73 | + $next = fn () => resolve('fresh_data'); |
| 74 | + |
| 75 | + $this->assertSame('fresh_data', await($this->middleware->handle($context, $next))); |
| 76 | + } |
| 77 | +} |
0 commit comments