Skip to content

Commit

Permalink
chore(cache): add cache invalidation test
Browse files Browse the repository at this point in the history
  • Loading branch information
sidux committed Jul 8, 2024
1 parent a729ffb commit b8729a3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 23 deletions.
7 changes: 3 additions & 4 deletions src/Interceptor/Impl/CacheInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class CacheInterceptor extends AbstractInterceptor implements SuffixInterc
private const DEFAULT_POOL_NAME = 'default';

/**
* @var string[][]
* @var array<string, array<int, array{key: string, tags: array<int, string>}>>
*/
private static array $hits = [];

Expand All @@ -44,7 +44,7 @@ public function __construct(
}

/**
* @return array<int, string>
* @return array<int, array{key: string, tags: array<int, string>}>
*/
public static function getHits(?string $poolName = null): array
{
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 20 additions & 10 deletions src/Interceptor/Impl/CacheTagsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

trait CacheTagsTrait
{
private function normalizePrefixName(string $name): string
{
return str_replace(
['\\', 'SharedResponse', 'Embedded', '_Shared'],
['.', '', '', ''],
$name,
);
}

/**
* @return array<class-string>
*/
Expand All @@ -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()
);
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tests/Double/Stub/Cache/ClassWithInvalidateCacheAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function methodWithInvalidateCacheButNoTag(): ResponseStub
return new ResponseStub();
}

#[InvalidateCache(tags: ['"OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getId.12"'])]
#[InvalidateCache(tags: ['"OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.12"'])]
public function methodWithInvalidateCacheAndExplicitTag(): ResponseStub
{
return new ResponseStub();
Expand All @@ -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
{
}
}
14 changes: 14 additions & 0 deletions tests/Double/Stub/Cache/Request2Stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache;

use OpenClassrooms\ServiceProxy\Attribute\Cache\Tag;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\Cache\AutoTaggable;

class Request2Stub implements AutoTaggable
{
#[Tag(ResponseStub::class)]
public int $userId = 1111;
}
16 changes: 8 additions & 8 deletions tests/Interceptor/CacheInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public function testMethodCacheIsAutoTaggedFromResponse(): void
$this->assertNotEmpty($this->cacheInterceptor::getHits());
$this->assertEmpty($this->cacheInterceptor::getMisses());

$tagToInvalidate = str_replace('\\', '.', ResponseStub::class) . '.getId.' . ResponseStub::ID;
$tagToInvalidate = str_replace('\\', '.', ResponseStub::class) . '.id.' . ResponseStub::ID;

$this->cacheHandlerMock->invalidateTags('default', [$tagToInvalidate]);

Expand Down Expand Up @@ -784,13 +784,13 @@ public function testRequestAndTagAttribute(): void
$this->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());
}
}
15 changes: 15 additions & 0 deletions tests/Interceptor/InvalidateCacheInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}

0 comments on commit b8729a3

Please sign in to comment.