|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\EventSourcingBundle\Tests\Unit\RequestListener; |
| 6 | + |
| 7 | +use Patchlevel\EventSourcing\Subscription\Engine\ProcessedResult; |
| 8 | +use Patchlevel\EventSourcing\Subscription\Engine\Result; |
| 9 | +use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngine; |
| 10 | +use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngineCriteria; |
| 11 | +use Patchlevel\EventSourcingBundle\RequestListener\SubscriptionRebuildAfterFileChangeListener; |
| 12 | +use Patchlevel\EventSourcingBundle\Tests\Fixtures\FromBeginningSubscriber; |
| 13 | +use Patchlevel\EventSourcingBundle\Tests\Fixtures\FromNowSubscriber; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Psr\Cache\CacheItemInterface; |
| 16 | +use Psr\Cache\CacheItemPoolInterface; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpKernel\Event\RequestEvent; |
| 19 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 20 | + |
| 21 | +/** @covers \Patchlevel\EventSourcingBundle\RequestListener\SubscriptionRebuildAfterFileChangeListener */ |
| 22 | +final class SubscriptionRebuildAfterFileChangeListenerTest extends TestCase |
| 23 | +{ |
| 24 | + public function testSkipSubRequest(): void |
| 25 | + { |
| 26 | + $subscriptionEngine = $this->createMock(SubscriptionEngine::class); |
| 27 | + $subscriptionEngine->expects($this->never())->method('remove'); |
| 28 | + $subscriptionEngine->expects($this->never())->method('setup'); |
| 29 | + $subscriptionEngine->expects($this->never())->method('boot'); |
| 30 | + |
| 31 | + $cache = $this->createMock(CacheItemPoolInterface::class); |
| 32 | + $cache->expects($this->never())->method('getItem'); |
| 33 | + |
| 34 | + $listener = new SubscriptionRebuildAfterFileChangeListener( |
| 35 | + $subscriptionEngine, |
| 36 | + [new FromBeginningSubscriber()], |
| 37 | + $cache, |
| 38 | + ); |
| 39 | + |
| 40 | + $listener->onKernelRequest($this->createRequestEvent('/app', HttpKernelInterface::SUB_REQUEST)); |
| 41 | + } |
| 42 | + |
| 43 | + public function testSkipExcludedUrl(): void |
| 44 | + { |
| 45 | + $subscriptionEngine = $this->createMock(SubscriptionEngine::class); |
| 46 | + $subscriptionEngine->expects($this->never())->method('remove'); |
| 47 | + $subscriptionEngine->expects($this->never())->method('setup'); |
| 48 | + $subscriptionEngine->expects($this->never())->method('boot'); |
| 49 | + |
| 50 | + $cache = $this->createMock(CacheItemPoolInterface::class); |
| 51 | + $cache->expects($this->never())->method('getItem'); |
| 52 | + |
| 53 | + $listener = new SubscriptionRebuildAfterFileChangeListener( |
| 54 | + $subscriptionEngine, |
| 55 | + [new FromBeginningSubscriber()], |
| 56 | + $cache, |
| 57 | + excludeUrl: '^/_wdt', |
| 58 | + ); |
| 59 | + |
| 60 | + $listener->onKernelRequest($this->createRequestEvent('/_wdt/abc')); |
| 61 | + } |
| 62 | + |
| 63 | + public function testRebuildChangedFromBeginningSubscriptionsOnly(): void |
| 64 | + { |
| 65 | + $item = $this->createMock(CacheItemInterface::class); |
| 66 | + $item->expects($this->once())->method('get')->willReturn(1); |
| 67 | + $item->expects($this->once())->method('set')->with($this->isType('int'))->willReturnSelf(); |
| 68 | + |
| 69 | + $cache = $this->createMock(CacheItemPoolInterface::class); |
| 70 | + $cache->expects($this->once())->method('getItem')->with('from-beginning')->willReturn($item); |
| 71 | + $cache->expects($this->once())->method('save')->with($item)->willReturn(true); |
| 72 | + |
| 73 | + $subscriptionEngine = $this->createMock(SubscriptionEngine::class); |
| 74 | + $criteriaMatcher = $this->callback(static fn (SubscriptionEngineCriteria|null $criteria): bool => $criteria instanceof SubscriptionEngineCriteria && $criteria->ids === ['from-beginning'] && $criteria->groups === null); |
| 75 | + |
| 76 | + $subscriptionEngine->expects($this->once())->method('remove')->with($criteriaMatcher)->willReturn(new Result()); |
| 77 | + $subscriptionEngine->expects($this->once())->method('setup')->with($criteriaMatcher)->willReturn(new Result()); |
| 78 | + $subscriptionEngine->expects($this->once())->method('boot')->with($criteriaMatcher)->willReturn(new ProcessedResult(0)); |
| 79 | + |
| 80 | + $listener = new SubscriptionRebuildAfterFileChangeListener( |
| 81 | + $subscriptionEngine, |
| 82 | + [new FromBeginningSubscriber(), new FromNowSubscriber()], |
| 83 | + $cache, |
| 84 | + ); |
| 85 | + |
| 86 | + $listener->onKernelRequest($this->createRequestEvent('/app')); |
| 87 | + } |
| 88 | + |
| 89 | + public function testNoRebuildWhenFileDidNotChange(): void |
| 90 | + { |
| 91 | + $subscriberFile = (new \ReflectionClass(FromBeginningSubscriber::class))->getFileName(); |
| 92 | + self::assertIsString($subscriberFile); |
| 93 | + |
| 94 | + $currentModified = filemtime($subscriberFile); |
| 95 | + self::assertIsInt($currentModified); |
| 96 | + |
| 97 | + $item = $this->createMock(CacheItemInterface::class); |
| 98 | + $item->expects($this->once())->method('get')->willReturn($currentModified); |
| 99 | + $item->expects($this->never())->method('set'); |
| 100 | + |
| 101 | + $cache = $this->createMock(CacheItemPoolInterface::class); |
| 102 | + $cache->expects($this->once())->method('getItem')->with('from-beginning')->willReturn($item); |
| 103 | + $cache->expects($this->never())->method('save'); |
| 104 | + |
| 105 | + $subscriptionEngine = $this->createMock(SubscriptionEngine::class); |
| 106 | + $emptyCriteriaMatcher = $this->callback(static fn (SubscriptionEngineCriteria|null $criteria): bool => $criteria instanceof SubscriptionEngineCriteria && $criteria->ids === []); |
| 107 | + |
| 108 | + $subscriptionEngine->expects($this->once())->method('remove')->with($emptyCriteriaMatcher)->willReturn(new Result()); |
| 109 | + $subscriptionEngine->expects($this->once())->method('setup')->with($emptyCriteriaMatcher)->willReturn(new Result()); |
| 110 | + $subscriptionEngine->expects($this->once())->method('boot')->with($emptyCriteriaMatcher)->willReturn(new ProcessedResult(0)); |
| 111 | + |
| 112 | + $listener = new SubscriptionRebuildAfterFileChangeListener( |
| 113 | + $subscriptionEngine, |
| 114 | + [new FromBeginningSubscriber()], |
| 115 | + $cache, |
| 116 | + ); |
| 117 | + |
| 118 | + $listener->onKernelRequest($this->createRequestEvent('/app')); |
| 119 | + } |
| 120 | + |
| 121 | + private function createRequestEvent( |
| 122 | + string $uri, |
| 123 | + int $requestType = HttpKernelInterface::MAIN_REQUEST, |
| 124 | + ): RequestEvent { |
| 125 | + return new RequestEvent( |
| 126 | + $this->createMock(HttpKernelInterface::class), |
| 127 | + Request::create($uri), |
| 128 | + $requestType, |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments