Skip to content

Commit 2d77187

Browse files
committed
chore(cache): add cache invalidation test
1 parent a729ffb commit 2d77187

6 files changed

Lines changed: 72 additions & 23 deletions

File tree

src/Interceptor/Impl/CacheInterceptor.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class CacheInterceptor extends AbstractInterceptor implements SuffixInterc
2323
private const DEFAULT_POOL_NAME = 'default';
2424

2525
/**
26-
* @var string[][]
26+
* @var array<string, array<int, array{key: string, tags: array<int, string>}>>
2727
*/
2828
private static array $hits = [];
2929

@@ -44,7 +44,7 @@ public function __construct(
4444
}
4545

4646
/**
47-
* @return array<int, string>
47+
* @return array<int, array{key: string, tags: array<int, string>}>
4848
*/
4949
public static function getHits(?string $poolName = null): array
5050
{
@@ -103,12 +103,11 @@ public function prefix(Instance $instance): Response
103103
continue;
104104
}
105105

106-
107106
$data = $data->get();
108107
$tags = $this->getTags($instance, $attribute, $data);
109108
self::$hits[$pool] = self::$hits[$pool] ?? [];
110109
self::$hits[$pool][] = [
111-
'key' => $cacheKey,
110+
'key' => $cacheKey,
112111
'tags' => $tags,
113112
];
114113
foreach ($missedPools as $missedPool) {

src/Interceptor/Impl/CacheTagsTrait.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
trait CacheTagsTrait
1515
{
16+
private function normalizePrefixName(string $name): string
17+
{
18+
return str_replace(
19+
['\\', 'SharedResponse', 'Embedded', '_Shared'],
20+
['.', '', '', ''],
21+
$name,
22+
);
23+
}
24+
1625
/**
1726
* @return array<class-string>
1827
*/
@@ -34,15 +43,9 @@ private function getTags(Instance $instance, Cache|InvalidateCache $attribute, m
3443

3544
/** @noinspection PhpConditionCheckedByNextConditionInspection */
3645
if ($response !== null && \is_object($response)) {
37-
$prefix = str_replace(
38-
['\\', 'SharedResponse', 'Embedded', '_Shared'],
39-
['.', '', '', ''],
40-
\get_class($response)
41-
);
46+
$prefix = $this->normalizePrefixName(\get_class($response));
4247
} else {
43-
$prefix = str_replace(
44-
'\\',
45-
'.',
48+
$prefix = $this->normalizePrefixName(
4649
$instance->getReflection()->getName() . $instance->getMethod()->getName()
4750
);
4851
}
@@ -199,11 +202,18 @@ private function buildTagName(
199202
? $member->getValue($object)
200203
: $member->invoke($object, []);
201204

205+
$memberPrefix = str_replace(
206+
['get', 'has', 'is'],
207+
['', '', ''],
208+
mb_strtolower($member->getName())
209+
);
202210
if ($tagAttribute?->newInstance()?->prefix !== null) {
203-
return $tagAttribute->newInstance()->prefix . '.' . $value;
211+
return $this->normalizePrefixName(
212+
$tagAttribute->newInstance()->prefix
213+
) . '.' . $memberPrefix . '.' . $value;
204214
}
205215

206-
return $prefix . '.' . $member->getName() . '.' . $value;
216+
return $prefix . '.' . $memberPrefix . '.' . $value;
207217
}
208218

209219
/**

tests/Double/Stub/Cache/ClassWithInvalidateCacheAttributes.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function methodWithInvalidateCacheButNoTag(): ResponseStub
4747
return new ResponseStub();
4848
}
4949

50-
#[InvalidateCache(tags: ['"OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getId.12"'])]
50+
#[InvalidateCache(tags: ['"OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.12"'])]
5151
public function methodWithInvalidateCacheAndExplicitTag(): ResponseStub
5252
{
5353
return new ResponseStub();
@@ -64,4 +64,15 @@ public function methodInvalidatingSubResource(): EmbeddedResponseStub
6464
{
6565
return new EmbeddedResponseStub();
6666
}
67+
68+
#[Cache]
69+
public function methodWithTaggedRequest(Request1Stub $request1Stub): ResponseStub
70+
{
71+
return new ResponseStub();
72+
}
73+
74+
#[InvalidateCache]
75+
public function methodWithInvalidateCacheButNoTagForRequest(Request2Stub $request2Stub): void
76+
{
77+
}
6778
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache;
6+
7+
use OpenClassrooms\ServiceProxy\Attribute\Cache\Tag;
8+
use OpenClassrooms\ServiceProxy\Interceptor\Contract\Cache\AutoTaggable;
9+
10+
class Request2Stub implements AutoTaggable
11+
{
12+
#[Tag(prefix: ResponseStub::class)]
13+
public int $userId = 1111;
14+
}

tests/Interceptor/CacheInterceptorTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function testMethodCacheIsAutoTaggedFromResponse(): void
240240
$this->assertNotEmpty($this->cacheInterceptor::getHits());
241241
$this->assertEmpty($this->cacheInterceptor::getMisses());
242242

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

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

@@ -784,13 +784,13 @@ public function testRequestAndTagAttribute(): void
784784
$this->assertEquals(new ResponseStub(), $result);
785785
$this->assertNotEmpty(CacheInterceptor::getHits());
786786
$this->assertEquals([
787-
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getId.12',
788-
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getName.test',
789-
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.getUserId.1111',
790-
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.age.10',
791-
'prefix.paris',
792-
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.1',
793-
], CacheInterceptor::getHits()[0]['tags']);
787+
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.12',
788+
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.name.test',
789+
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.userid.1111',
790+
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.age.10',
791+
'prefix.city.paris',
792+
'OpenClassrooms.ServiceProxy.Tests.Double.Stub.Cache.ResponseStub.id.1',
793+
], CacheInterceptor::getHits()[0]['tags']);
794794
$this->assertEmpty(CacheInterceptor::getMisses());
795795
}
796796
}

tests/Interceptor/InvalidateCacheInterceptorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use OpenClassrooms\ServiceProxy\Tests\CacheTestTrait;
1212
use OpenClassrooms\ServiceProxy\Tests\Double\Mock\Cache\CacheHandlerMock;
1313
use OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache\ClassWithInvalidateCacheAttributes;
14+
use OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache\Request1Stub;
15+
use OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache\Request2Stub;
1416
use OpenClassrooms\ServiceProxy\Tests\ProxyTestTrait;
1517
use PHPUnit\Framework\TestCase;
1618

@@ -129,4 +131,17 @@ public function testCacheInvalidationWithTagsFromSubResources(): void
129131
$this->proxy->methodWithCachedEmbeddedResponse();
130132
$this->assertEmpty($this->cacheInterceptor->getHits());
131133
}
134+
135+
public function testCacheInvalidationWithRequest(): void
136+
{
137+
$this->proxy->methodWithTaggedRequest(new Request1Stub());
138+
$this->assertEmpty(CacheInterceptor::getHits());
139+
140+
$this->proxy->methodWithTaggedRequest(new Request1Stub());
141+
$this->assertNotEmpty(CacheInterceptor::getHits());
142+
143+
$this->proxy->methodWithInvalidateCacheButNoTagForRequest(new Request2Stub());
144+
$this->proxy->methodWithTaggedRequest(new Request1Stub());
145+
$this->assertEmpty(CacheInterceptor::getHits());
146+
}
132147
}

0 commit comments

Comments
 (0)