|
31 | 31 | #[UsesClass(RedisClientFactory::class)] |
32 | 32 | class InvalidatorServiceTest extends TestCase |
33 | 33 | { |
| 34 | + public function testInvalidateEverything(): void |
| 35 | + { |
| 36 | + $redisMock = $this->createMock(Redis::class); |
| 37 | + $redisMock->method('isConnected')->willReturn(true); |
| 38 | + $redisMock->method('scan')->willReturnOnConsecutiveCalls( |
| 39 | + ['key:1', 'key:2'], |
| 40 | + [] |
| 41 | + ); |
| 42 | + $redisMock |
| 43 | + ->expects($this->atLeastOnce()) |
| 44 | + ->method('del'); |
| 45 | + |
| 46 | + $directRedisCacheService = new DirectRedisCacheService(redisClientProvider: RedisClientFactoryTest::wrapClient($redisMock)); |
| 47 | + |
| 48 | + $multiLevelCacheFactoryStub = $this->createStub(MultiLevelCacheFactory::class); |
| 49 | + $multiLevelCacheFactoryStub->method('getImplementationRedisWithPrefix') |
| 50 | + ->willReturn($directRedisCacheService); |
| 51 | + |
| 52 | + $invalidatorService = new InvalidatorService($multiLevelCacheFactoryStub); |
| 53 | + $result = $invalidatorService->invalidateEverything(); |
| 54 | + $this->assertTrue($result); |
| 55 | + } |
| 56 | + |
| 57 | + public function testInvalidateEverythingWithError(): void |
| 58 | + { |
| 59 | + $redisMock = $this->createMock(Redis::class); |
| 60 | + $redisMock->method('isConnected')->willReturn(true); |
| 61 | + $redisMock->method('scan')->willReturnOnConsecutiveCalls( |
| 62 | + ['key:1', 'key:2'], |
| 63 | + [] |
| 64 | + ); |
| 65 | + $redisMock |
| 66 | + ->expects($this->atLeastOnce()) |
| 67 | + ->method('del') |
| 68 | + ->willThrowException(new Exception('random error')); |
| 69 | + |
| 70 | + $directRedisCacheService = new DirectRedisCacheService(redisClientProvider: RedisClientFactoryTest::wrapClient($redisMock)); |
| 71 | + |
| 72 | + $multiLevelCacheFactoryStub = $this->createStub(MultiLevelCacheFactory::class); |
| 73 | + $multiLevelCacheFactoryStub->method('getImplementationRedisWithPrefix') |
| 74 | + ->willReturn($directRedisCacheService); |
| 75 | + |
| 76 | + $invalidatorService = new InvalidatorService($multiLevelCacheFactoryStub); |
| 77 | + $result = $invalidatorService->invalidateEverything(); |
| 78 | + $this->assertFalse($result); |
| 79 | + } |
| 80 | + |
34 | 81 | public function testClassInvalidation(): void |
35 | 82 | { |
36 | 83 | $redisStub = $this->createStub(Redis::class); |
@@ -100,4 +147,47 @@ public function testMethodInvalidationError(): void |
100 | 147 | $result = $invalidatorService->invalidateCacheForMethod(TestServiceA::class, 'testMethod'); |
101 | 148 | $this->assertFalse($result); |
102 | 149 | } |
| 150 | + |
| 151 | + public function testNamespaceInvalidation(): void |
| 152 | + { |
| 153 | + $redisMock = $this->createMock(Redis::class); |
| 154 | + $redisMock->method('isConnected')->willReturn(true); |
| 155 | + $redisMock->method('scan')->willReturnOnConsecutiveCalls( |
| 156 | + ['key:1', 'key:2'], |
| 157 | + [] |
| 158 | + ); |
| 159 | + $redisMock->expects($this->atLeastOnce()) |
| 160 | + ->method('del'); |
| 161 | + $directRedisCacheService = new DirectRedisCacheService(redisClientProvider: RedisClientFactoryTest::wrapClient($redisMock)); |
| 162 | + |
| 163 | + $multiLevelCacheFactoryStub = $this->createStub(MultiLevelCacheFactory::class); |
| 164 | + $multiLevelCacheFactoryStub->method('getImplementationRedisWithPrefix') |
| 165 | + ->willReturn($directRedisCacheService); |
| 166 | + |
| 167 | + $invalidatorService = new InvalidatorService($multiLevelCacheFactoryStub); |
| 168 | + $result = $invalidatorService->invalidateCacheForNamespace(TestServiceA::class); |
| 169 | + $this->assertTrue($result); |
| 170 | + } |
| 171 | + |
| 172 | + public function testNamespaceInvalidationWithError(): void |
| 173 | + { |
| 174 | + $redisMock = $this->createMock(Redis::class); |
| 175 | + $redisMock->method('isConnected')->willReturn(true); |
| 176 | + $redisMock->method('scan')->willReturnOnConsecutiveCalls( |
| 177 | + ['key:1', 'key:2'], |
| 178 | + [] |
| 179 | + ); |
| 180 | + $redisMock->expects($this->atLeastOnce()) |
| 181 | + ->method('del') |
| 182 | + ->willThrowException(new Exception('random error')); |
| 183 | + $directRedisCacheService = new DirectRedisCacheService(redisClientProvider: RedisClientFactoryTest::wrapClient($redisMock)); |
| 184 | + |
| 185 | + $multiLevelCacheFactoryStub = $this->createStub(MultiLevelCacheFactory::class); |
| 186 | + $multiLevelCacheFactoryStub->method('getImplementationRedisWithPrefix') |
| 187 | + ->willReturn($directRedisCacheService); |
| 188 | + |
| 189 | + $invalidatorService = new InvalidatorService($multiLevelCacheFactoryStub); |
| 190 | + $result = $invalidatorService->invalidateCacheForNamespace(TestServiceA::class); |
| 191 | + $this->assertFalse($result); |
| 192 | + } |
103 | 193 | } |
0 commit comments