diff --git a/src/Interceptor/Impl/CacheInterceptor.php b/src/Interceptor/Impl/CacheInterceptor.php index 32f5f96..a0a8f3f 100644 --- a/src/Interceptor/Impl/CacheInterceptor.php +++ b/src/Interceptor/Impl/CacheInterceptor.php @@ -23,7 +23,7 @@ final class CacheInterceptor extends AbstractInterceptor implements SuffixInterc private const DEFAULT_POOL_NAME = 'default'; /** - * @var string[][] + * @var array}>> */ private static array $hits = []; @@ -44,7 +44,7 @@ public function __construct( } /** - * @return array + * @return array}> */ public static function getHits(?string $poolName = null): array { @@ -103,12 +103,11 @@ public function prefix(Instance $instance): Response continue; } - $data = $data->get(); $tags = $this->getTags($instance, $attribute, $data); self::$hits[$pool] = self::$hits[$pool] ?? []; self::$hits[$pool][] = [ - 'key' => $cacheKey, + 'key' => $cacheKey, 'tags' => $tags, ]; foreach ($missedPools as $missedPool) { diff --git a/src/Interceptor/Impl/CacheTagsTrait.php b/src/Interceptor/Impl/CacheTagsTrait.php index db32072..3a3d743 100644 --- a/src/Interceptor/Impl/CacheTagsTrait.php +++ b/src/Interceptor/Impl/CacheTagsTrait.php @@ -13,6 +13,15 @@ trait CacheTagsTrait { + private function normalizePrefixName(string $name): string + { + return str_replace( + ['\\', 'SharedResponse', 'Embedded', '_Shared'], + ['.', '', '', ''], + $name, + ); + } + /** * @return array */ @@ -34,15 +43,9 @@ private function getTags(Instance $instance, Cache|InvalidateCache $attribute, m /** @noinspection PhpConditionCheckedByNextConditionInspection */ if ($response !== null && \is_object($response)) { - $prefix = str_replace( - ['\\', 'SharedResponse', 'Embedded', '_Shared'], - ['.', '', '', ''], - \get_class($response) - ); + $prefix = $this->normalizePrefixName(\get_class($response)); } else { - $prefix = str_replace( - '\\', - '.', + $prefix = $this->normalizePrefixName( $instance->getReflection()->getName() . $instance->getMethod()->getName() ); } @@ -199,11 +202,18 @@ private function buildTagName( ? $member->getValue($object) : $member->invoke($object, []); + $memberPrefix = str_replace( + ['get', 'has', 'is'], + ['', '', ''], + mb_strtolower($member->getName()) + ); if ($tagAttribute?->newInstance()?->prefix !== null) { - return $tagAttribute->newInstance()->prefix . '.' . $value; + return $this->normalizePrefixName( + $tagAttribute->newInstance()->prefix + ) . '.' . $memberPrefix . '.' . $value; } - return $prefix . '.' . $member->getName() . '.' . $value; + return $prefix . '.' . $memberPrefix . '.' . $value; } /** diff --git a/tests/Double/Stub/Cache/ClassWithInvalidateCacheAttributes.php b/tests/Double/Stub/Cache/ClassWithInvalidateCacheAttributes.php index c710817..bd91831 100644 --- a/tests/Double/Stub/Cache/ClassWithInvalidateCacheAttributes.php +++ b/tests/Double/Stub/Cache/ClassWithInvalidateCacheAttributes.php @@ -64,4 +64,15 @@ public function methodInvalidatingSubResource(): EmbeddedResponseStub { return new EmbeddedResponseStub(); } + + #[Cache] + public function methodWithTaggedRequest(Request1Stub $request1Stub): ResponseStub + { + return new ResponseStub(); + } + + #[InvalidateCache] + public function methodWithInvalidateCacheButNoTagForRequest(Request2Stub $request2Stub): void + { + } } diff --git a/tests/Double/Stub/Cache/Request2Stub.php b/tests/Double/Stub/Cache/Request2Stub.php new file mode 100644 index 0000000..46072c7 --- /dev/null +++ b/tests/Double/Stub/Cache/Request2Stub.php @@ -0,0 +1,14 @@ +assertEquals(new ResponseStub(), $result); $this->assertNotEmpty(CacheInterceptor::getHits()); $this->assertEquals([ - 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getId.12', - 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getName.test', - 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getUserId.1111', - 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.age.10', - 'prefix.paris', - 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.1', - ], CacheInterceptor::getHits()[0]['tags']); + 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.12', + 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.name.test', + 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.userid.1111', + 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.age.10', + 'prefix.city.paris', + 'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.1', + ], CacheInterceptor::getHits()[0]['tags']); $this->assertEmpty(CacheInterceptor::getMisses()); } } diff --git a/tests/Interceptor/InvalidateCacheInterceptorTest.php b/tests/Interceptor/InvalidateCacheInterceptorTest.php index 58d0d3f..c7eea2a 100644 --- a/tests/Interceptor/InvalidateCacheInterceptorTest.php +++ b/tests/Interceptor/InvalidateCacheInterceptorTest.php @@ -11,6 +11,8 @@ use OpenClassrooms\ServiceProxy\Tests\CacheTestTrait; use OpenClassrooms\ServiceProxy\Tests\Double\Mock\Cache\CacheHandlerMock; use OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache\ClassWithInvalidateCacheAttributes; +use OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache\Request1Stub; +use OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache\Request2Stub; use OpenClassrooms\ServiceProxy\Tests\ProxyTestTrait; use PHPUnit\Framework\TestCase; @@ -129,4 +131,17 @@ public function testCacheInvalidationWithTagsFromSubResources(): void $this->proxy->methodWithCachedEmbeddedResponse(); $this->assertEmpty($this->cacheInterceptor->getHits()); } + + public function testCacheInvalidationWithRequest(): void + { + $this->proxy->methodWithTaggedRequest(new Request1Stub()); + $this->assertEmpty(CacheInterceptor::getHits()); + + $this->proxy->methodWithTaggedRequest(new Request1Stub()); + $this->assertNotEmpty(CacheInterceptor::getHits()); + + $this->proxy->methodWithInvalidateCacheButNoTagForRequest(new Request2Stub()); + $this->proxy->methodWithTaggedRequest(new Request1Stub()); + $this->assertEmpty(CacheInterceptor::getHits()); + } }