|
11 | 11 | use Sentry\State\HubInterface;
|
12 | 12 | use Symfony\Component\Cache\Adapter\AdapterInterface;
|
13 | 13 | use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
|
| 14 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
14 | 15 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
15 | 16 | use Symfony\Component\DependencyInjection\Definition;
|
16 | 17 | use Symfony\Component\DependencyInjection\Reference;
|
17 | 18 |
|
18 | 19 | final class CacheTracingPassTest extends TestCase
|
19 | 20 | {
|
20 |
| - public function testProcess(): void |
| 21 | + /** |
| 22 | + * @dataProvider processDataProvider |
| 23 | + * |
| 24 | + * @param array<string, Definition> $definitions |
| 25 | + */ |
| 26 | + public function testProcess(array $definitions, string $expectedDefinitionClass, string $expectedInnerDefinitionClass): void |
| 27 | + { |
| 28 | + $container = $this->createContainerBuilder(true); |
| 29 | + $container->addDefinitions($definitions); |
| 30 | + $container->compile(); |
| 31 | + |
| 32 | + $cacheTraceableDefinition = $container->findDefinition('app.cache'); |
| 33 | + |
| 34 | + $this->assertSame($expectedDefinitionClass, $cacheTraceableDefinition->getClass()); |
| 35 | + $this->assertInstanceOf(Definition::class, $cacheTraceableDefinition->getArgument(1)); |
| 36 | + $this->assertSame($expectedInnerDefinitionClass, $cacheTraceableDefinition->getArgument(1)->getClass()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return \Generator<mixed> |
| 41 | + */ |
| 42 | + public function processDataProvider(): \Generator |
21 | 43 | {
|
22 | 44 | $cacheAdapter = $this->createMock(AdapterInterface::class);
|
23 | 45 | $tagAwareCacheAdapter = $this->createMock(TagAwareAdapterInterface::class);
|
24 |
| - $container = $this->createContainerBuilder(true); |
25 | 46 |
|
26 |
| - $container->register('app.cache.foo', \get_class($tagAwareCacheAdapter)) |
27 |
| - ->setPublic(true) |
28 |
| - ->addTag('cache.pool'); |
| 47 | + yield 'Cache pool adapter service' => [ |
| 48 | + [ |
| 49 | + 'app.cache' => (new Definition(\get_class($cacheAdapter))) |
| 50 | + ->setPublic(true) |
| 51 | + ->addTag('cache.pool'), |
| 52 | + ], |
| 53 | + TraceableCacheAdapter::class, |
| 54 | + \get_class($cacheAdapter), |
| 55 | + ]; |
| 56 | + |
| 57 | + yield 'Tag-aware cache adapter service' => [ |
| 58 | + [ |
| 59 | + 'app.cache' => (new Definition(\get_class($tagAwareCacheAdapter))) |
| 60 | + ->setPublic(true) |
| 61 | + ->addTag('cache.pool'), |
| 62 | + ], |
| 63 | + TraceableTagAwareCacheAdapter::class, |
| 64 | + \get_class($tagAwareCacheAdapter), |
| 65 | + ]; |
| 66 | + |
| 67 | + yield 'Cache pool adapter service inheriting parent service' => [ |
| 68 | + [ |
| 69 | + 'app.cache.parent' => new Definition(\get_class($cacheAdapter)), |
| 70 | + 'app.cache' => (new ChildDefinition('app.cache.parent')) |
| 71 | + ->setPublic(true) |
| 72 | + ->addTag('cache.pool'), |
| 73 | + ], |
| 74 | + TraceableCacheAdapter::class, |
| 75 | + \get_class($cacheAdapter), |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'Tag-aware cache pool adapter service inheriting parent service and overriding class' => [ |
| 79 | + [ |
| 80 | + 'app.cache.parent' => new Definition(\get_class($cacheAdapter)), |
| 81 | + 'app.cache' => (new ChildDefinition('app.cache.parent')) |
| 82 | + ->setClass(\get_class($tagAwareCacheAdapter)) |
| 83 | + ->setPublic(true) |
| 84 | + ->addTag('cache.pool'), |
| 85 | + ], |
| 86 | + TraceableTagAwareCacheAdapter::class, |
| 87 | + \get_class($tagAwareCacheAdapter), |
| 88 | + ]; |
| 89 | + |
| 90 | + yield 'Tag-aware cache pool adapter service inheriting multiple parent services' => [ |
| 91 | + [ |
| 92 | + 'app.cache.parent_1' => new Definition(\get_class($cacheAdapter)), |
| 93 | + 'app.cache.parent_2' => (new ChildDefinition('app.cache.parent_1')) |
| 94 | + ->setClass(\get_class($tagAwareCacheAdapter)), |
| 95 | + 'app.cache' => (new ChildDefinition('app.cache.parent_2')) |
| 96 | + ->setPublic(true) |
| 97 | + ->addTag('cache.pool'), |
| 98 | + ], |
| 99 | + TraceableTagAwareCacheAdapter::class, |
| 100 | + \get_class($tagAwareCacheAdapter), |
| 101 | + ]; |
| 102 | + |
| 103 | + yield 'Tag-aware cache pool adapter service inheriting parent service' => [ |
| 104 | + [ |
| 105 | + 'app.cache.parent' => new Definition(\get_class($tagAwareCacheAdapter)), |
| 106 | + 'app.cache' => (new ChildDefinition('app.cache.parent')) |
| 107 | + ->setPublic(true) |
| 108 | + ->addTag('cache.pool'), |
| 109 | + ], |
| 110 | + TraceableTagAwareCacheAdapter::class, |
| 111 | + \get_class($tagAwareCacheAdapter), |
| 112 | + ]; |
| 113 | + } |
29 | 114 |
|
30 |
| - $container->register('app.cache.bar', \get_class($cacheAdapter)) |
31 |
| - ->setPublic(true) |
32 |
| - ->addTag('cache.pool'); |
| 115 | + public function testProcessDoesNothingIfCachePoolServiceDefinitionIsAbstract(): void |
| 116 | + { |
| 117 | + $cacheAdapter = $this->createMock(AdapterInterface::class); |
| 118 | + $container = $this->createContainerBuilder(true); |
33 | 119 |
|
34 |
| - $container->register('app.cache.baz') |
| 120 | + $container->register('app.cache', \get_class($cacheAdapter)) |
35 | 121 | ->setPublic(true)
|
36 | 122 | ->setAbstract(true)
|
37 | 123 | ->addTag('cache.pool');
|
38 | 124 |
|
39 | 125 | $container->compile();
|
40 | 126 |
|
41 |
| - $cacheTraceableDefinition = $container->findDefinition('app.cache.foo'); |
42 |
| - |
43 |
| - $this->assertSame(TraceableTagAwareCacheAdapter::class, $cacheTraceableDefinition->getClass()); |
44 |
| - $this->assertInstanceOf(Definition::class, $cacheTraceableDefinition->getArgument(1)); |
45 |
| - $this->assertSame(\get_class($tagAwareCacheAdapter), $cacheTraceableDefinition->getArgument(1)->getClass()); |
46 |
| - |
47 |
| - $cacheTraceableDefinition = $container->findDefinition('app.cache.bar'); |
48 |
| - |
49 |
| - $this->assertSame(TraceableCacheAdapter::class, $cacheTraceableDefinition->getClass()); |
50 |
| - $this->assertInstanceOf(Definition::class, $cacheTraceableDefinition->getArgument(1)); |
51 |
| - $this->assertSame(\get_class($cacheAdapter), $cacheTraceableDefinition->getArgument(1)->getClass()); |
52 |
| - |
53 |
| - $this->assertFalse($container->hasDefinition('app.cache.baz')); |
| 127 | + $this->assertFalse($container->hasDefinition('app.cache')); |
54 | 128 | }
|
55 | 129 |
|
56 | 130 | public function testProcessDoesNothingIfConditionsForEnablingTracingAreMissing(): void
|
|
0 commit comments