Skip to content

Commit 6def8a8

Browse files
committed
feat(lock): improve tests, fix priority
1 parent 47821ec commit 6def8a8

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/Interceptor/Impl/CacheInterceptor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ final class CacheInterceptor extends AbstractInterceptor implements SuffixInterc
3939
*/
4040
private static array $reservedCharacters = ['{', '}', '(', ')', '/', '@', ':'];
4141

42+
private CacheInterceptorConfig $config;
43+
4244
public function __construct(
43-
private readonly CacheInterceptorConfig $config,
44-
iterable $handlers = [],
45+
?CacheInterceptorConfig $config = null,
46+
iterable $handlers = [],
4547
) {
4648
parent::__construct($handlers);
4749

50+
$this->config = $config ?? new CacheInterceptorConfig();
4851
$this->typesExtractor = new TypesExtractor();
4952
}
5053

src/Interceptor/Impl/LockInterceptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333

3434
public function getPrefixPriority(): int
3535
{
36-
return -10;
36+
return 1;
3737
}
3838

3939
public function getSuffixPriority(): int

tests/Handler/Impl/Lock/SymfonyLockHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public function test2(): void
3737
$this->assertTrue($this->handler2->isAcquired('key2'));
3838
$this->handler1->release('key1');
3939
$this->assertFalse($this->handler1->isAcquired('key1'));
40+
$this->handler2->release('key2');
41+
$this->assertFalse($this->handler2->isAcquired('key2'));
4042
}
4143

4244
public function test3(): void

tests/ProxyFactoryTest.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44

55
namespace OpenClassrooms\ServiceProxy\Tests;
66

7-
use OpenClassrooms\ServiceProxy\Interceptor\Config\CacheInterceptorConfig;
87
use OpenClassrooms\ServiceProxy\Interceptor\Contract\PrefixInterceptor;
98
use OpenClassrooms\ServiceProxy\Interceptor\Contract\SuffixInterceptor;
109
use OpenClassrooms\ServiceProxy\Interceptor\Impl\CacheInterceptor;
1110
use OpenClassrooms\ServiceProxy\Interceptor\Impl\EventInterceptor;
1211
use OpenClassrooms\ServiceProxy\Interceptor\Impl\InvalidateCacheInterceptor;
12+
use OpenClassrooms\ServiceProxy\Interceptor\Impl\LegacyCacheInterceptor;
13+
use OpenClassrooms\ServiceProxy\Interceptor\Impl\LockInterceptor;
1314
use OpenClassrooms\ServiceProxy\Interceptor\Impl\SecurityInterceptor;
1415
use OpenClassrooms\ServiceProxy\Interceptor\Impl\TransactionInterceptor;
1516
use OpenClassrooms\ServiceProxy\ProxyFactory;
16-
use OpenClassrooms\ServiceProxy\Tests\Double\Mock\Cache\CacheHandlerMock;
17-
use OpenClassrooms\ServiceProxy\Tests\Double\Mock\Event\EventHandlerMock;
18-
use OpenClassrooms\ServiceProxy\Tests\Double\Mock\Security\SecurityHandlerMock;
19-
use OpenClassrooms\ServiceProxy\Tests\Double\Mock\Transaction\TransactionHandlerMock;
2017
use OpenClassrooms\ServiceProxy\Tests\Double\Stub\Cache\ClassWithCacheAttributes;
2118
use OpenClassrooms\ServiceProxy\Tests\Double\Stub\WithConstructorAnnotationClass;
2219
use OpenClassrooms\ServiceProxy\Tests\Double\Stub\WithoutAnnotationClass;
@@ -30,15 +27,22 @@ final class ProxyFactoryTest extends TestCase
3027

3128
protected function setUp(): void
3229
{
33-
$this->factory = $this->getProxyFactory(
34-
[
35-
new CacheInterceptor(new CacheInterceptorConfig(), [new CacheHandlerMock()]),
36-
new EventInterceptor([new EventHandlerMock()]),
37-
new TransactionInterceptor([new TransactionHandlerMock()]),
38-
new SecurityInterceptor([new SecurityHandlerMock()]),
39-
new InvalidateCacheInterceptor(),
40-
]
41-
);
30+
//auto load all interceptors using glob from folder
31+
$interceptorsFiles = glob(__DIR__ . '/../src/Interceptor/Impl/*Interceptor.php');
32+
$interceptors = [];
33+
foreach ($interceptorsFiles as $interceptorFile) {
34+
require_once $interceptorFile;
35+
}
36+
37+
$classes = get_declared_classes();
38+
39+
foreach ($classes as $class) {
40+
if (is_subclass_of($class, PrefixInterceptor::class) || is_subclass_of($class, SuffixInterceptor::class)) {
41+
$interceptors[] = new $class();
42+
}
43+
}
44+
45+
$this->factory = $this->getProxyFactory($interceptors);
4246
}
4347

4448
public function testWithoutAnnotationReturnServiceProxyInterface(): void
@@ -88,15 +92,19 @@ public function testCheckInterceptorsOrders(): void
8892
SecurityInterceptor::class,
8993
EventInterceptor::class,
9094
CacheInterceptor::class,
95+
LegacyCacheInterceptor::class,
96+
LockInterceptor::class,
9197
TransactionInterceptor::class,
9298
],
9399
$prefixInterceptorsClasses
94100
);
95101
$this->assertEquals(
96102
[
97103
TransactionInterceptor::class,
104+
LockInterceptor::class,
98105
InvalidateCacheInterceptor::class,
99106
CacheInterceptor::class,
107+
LegacyCacheInterceptor::class,
100108
EventInterceptor::class,
101109
],
102110
$suffixInterceptorsClasses

0 commit comments

Comments
 (0)