|
12 | 12 | namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector; |
| 16 | +use Symfony\Bundle\FrameworkBundle\DataCollector\TemplateAwareDataCollectorInterface; |
15 | 17 | use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
|
| 18 | +use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass; |
| 19 | +use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass; |
16 | 20 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\HttpFoundation\Response; |
| 23 | +use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; |
17 | 24 |
|
18 | 25 | class ProfilerPassTest extends TestCase
|
19 | 26 | {
|
@@ -54,4 +61,64 @@ public function testValidCollector()
|
54 | 61 | $this->assertCount(1, $methodCalls);
|
55 | 62 | $this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call
|
56 | 63 | }
|
| 64 | + |
| 65 | + public function provideValidCollectorWithTemplateUsingAutoconfigure(): \Generator |
| 66 | + { |
| 67 | + yield [new class() implements TemplateAwareDataCollectorInterface { |
| 68 | + public function collect(Request $request, Response $response, \Throwable $exception = null) |
| 69 | + { |
| 70 | + } |
| 71 | + |
| 72 | + public function getName(): string |
| 73 | + { |
| 74 | + return static::class; |
| 75 | + } |
| 76 | + |
| 77 | + public function reset() |
| 78 | + { |
| 79 | + } |
| 80 | + |
| 81 | + public static function getTemplate(): string |
| 82 | + { |
| 83 | + return 'foo'; |
| 84 | + } |
| 85 | + }]; |
| 86 | + |
| 87 | + yield [new class() extends AbstractDataCollector { |
| 88 | + public function collect(Request $request, Response $response, \Throwable $exception = null) |
| 89 | + { |
| 90 | + } |
| 91 | + |
| 92 | + public static function getTemplate(): string |
| 93 | + { |
| 94 | + return 'foo'; |
| 95 | + } |
| 96 | + }]; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @dataProvider provideValidCollectorWithTemplateUsingAutoconfigure |
| 101 | + */ |
| 102 | + public function testValidCollectorWithTemplateUsingAutoconfigure(TemplateAwareDataCollectorInterface $dataCollector) |
| 103 | + { |
| 104 | + $container = new ContainerBuilder(); |
| 105 | + $profilerDefinition = $container->register('profiler', 'ProfilerClass'); |
| 106 | + |
| 107 | + $container->registerForAutoconfiguration(DataCollectorInterface::class)->addTag('data_collector'); |
| 108 | + $container->register('mydatacollector', \get_class($dataCollector))->setAutoconfigured(true); |
| 109 | + |
| 110 | + (new ResolveInstanceofConditionalsPass())->process($container); |
| 111 | + (new ProfilerPass())->process($container); |
| 112 | + |
| 113 | + $idForTemplate = \get_class($dataCollector); |
| 114 | + $this->assertSame(['mydatacollector' => [$idForTemplate, 'foo']], $container->getParameter('data_collector.templates')); |
| 115 | + |
| 116 | + // grab the method calls off of the "profiler" definition |
| 117 | + $methodCalls = $profilerDefinition->getMethodCalls(); |
| 118 | + $this->assertCount(1, $methodCalls); |
| 119 | + $this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call |
| 120 | + |
| 121 | + (new ResolveChildDefinitionsPass())->process($container); |
| 122 | + $this->assertSame($idForTemplate, $container->get('mydatacollector')->getName()); |
| 123 | + } |
57 | 124 | }
|
0 commit comments