|
15 | 15 | use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
|
16 | 16 | use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
|
17 | 17 | use Symfony\Component\HttpFoundation\Request;
|
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | +use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector; |
| 20 | +use Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector; |
| 21 | +use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector; |
18 | 22 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
19 | 23 | use Symfony\Component\HttpKernel\Profiler\Profile;
|
| 24 | +use Symfony\Component\HttpKernel\Profiler\Profiler; |
| 25 | +use Twig\Environment; |
| 26 | +use Twig\Loader\LoaderInterface; |
| 27 | +use Twig\Loader\SourceContextLoaderInterface; |
20 | 28 |
|
21 | 29 | class ProfilerControllerTest extends TestCase
|
22 | 30 | {
|
@@ -185,17 +193,105 @@ public function provideCspVariants()
|
185 | 193 | ];
|
186 | 194 | }
|
187 | 195 |
|
188 |
| - private function createController($profiler, $twig, $withCSP): ProfilerController |
| 196 | + /** |
| 197 | + * @dataProvider defaultPanelProvider |
| 198 | + */ |
| 199 | + public function testDefaultPanel(string $expectedPanel, Profile $profile) |
| 200 | + { |
| 201 | + $profiler = $this->createMock(Profiler::class); |
| 202 | + $profiler |
| 203 | + ->expects($this->atLeastOnce()) |
| 204 | + ->method('loadProfile') |
| 205 | + ->with($profile->getToken()) |
| 206 | + ->willReturn($profile); |
| 207 | + |
| 208 | + $profiler |
| 209 | + ->expects($this->atLeastOnce()) |
| 210 | + ->method('has') |
| 211 | + ->with($this->logicalXor($collectorsNames = array_keys($profile->getCollectors()))) |
| 212 | + ->willReturn(true); |
| 213 | + |
| 214 | + if (Environment::MAJOR_VERSION > 1) { |
| 215 | + $loader = $this->createMock(LoaderInterface::class); |
| 216 | + $loader |
| 217 | + ->expects($this->atLeastOnce()) |
| 218 | + ->method('exists') |
| 219 | + ->with($this->logicalXor($expectedTemplate = 'expected_template.html.twig', 'other_template.html.twig')) |
| 220 | + ->willReturn(true); |
| 221 | + } else { |
| 222 | + $loader = $this->createMock(SourceContextLoaderInterface::class); |
| 223 | + } |
| 224 | + |
| 225 | + $twig = $this->createMock(Environment::class); |
| 226 | + $twig |
| 227 | + ->expects($this->atLeastOnce()) |
| 228 | + ->method('getLoader') |
| 229 | + ->willReturn($loader); |
| 230 | + $twig |
| 231 | + ->expects($this->once()) |
| 232 | + ->method('render') |
| 233 | + ->with($expectedTemplate); |
| 234 | + |
| 235 | + $this |
| 236 | + ->createController($profiler, $twig, false, array_map(function (string $collectorName) use ($expectedPanel, $expectedTemplate): array { |
| 237 | + if ($collectorName === $expectedPanel) { |
| 238 | + return [$expectedPanel, $expectedTemplate]; |
| 239 | + } |
| 240 | + |
| 241 | + return [$collectorName, 'other_template.html.twig']; |
| 242 | + }, $collectorsNames)) |
| 243 | + ->panelAction(new Request(), $profile->getToken()); |
| 244 | + } |
| 245 | + |
| 246 | + public function defaultPanelProvider(): \Generator |
| 247 | + { |
| 248 | + // Test default behavior |
| 249 | + $profile = new Profile('xxxxxx'); |
| 250 | + $profile->addCollector($requestDataCollector = new RequestDataCollector()); |
| 251 | + yield [$requestDataCollector->getName(), $profile]; |
| 252 | + |
| 253 | + // Test exception |
| 254 | + $profile = new Profile('xxxxxx'); |
| 255 | + $profile->addCollector($exceptionDataCollector = new ExceptionDataCollector()); |
| 256 | + $exceptionDataCollector->collect(new Request(), new Response(), new \DomainException()); |
| 257 | + yield [$exceptionDataCollector->getName(), $profile]; |
| 258 | + |
| 259 | + // Test exception priority |
| 260 | + $dumpDataCollector = $this->createMock(DumpDataCollector::class); |
| 261 | + $dumpDataCollector |
| 262 | + ->expects($this->atLeastOnce()) |
| 263 | + ->method('getName') |
| 264 | + ->willReturn('dump'); |
| 265 | + $dumpDataCollector |
| 266 | + ->expects($this->atLeastOnce()) |
| 267 | + ->method('getDumpsCount') |
| 268 | + ->willReturn(1); |
| 269 | + $profile = new Profile('xxxxxx'); |
| 270 | + $profile->setCollectors([$exceptionDataCollector, $dumpDataCollector]); |
| 271 | + yield [$exceptionDataCollector->getName(), $profile]; |
| 272 | + |
| 273 | + // Test exception priority when defined afterwards |
| 274 | + $profile = new Profile('xxxxxx'); |
| 275 | + $profile->setCollectors([$dumpDataCollector, $exceptionDataCollector]); |
| 276 | + yield [$exceptionDataCollector->getName(), $profile]; |
| 277 | + |
| 278 | + // Test dump |
| 279 | + $profile = new Profile('xxxxxx'); |
| 280 | + $profile->addCollector($dumpDataCollector); |
| 281 | + yield [$dumpDataCollector->getName(), $profile]; |
| 282 | + } |
| 283 | + |
| 284 | + private function createController($profiler, $twig, $withCSP, array $templates = []): ProfilerController |
189 | 285 | {
|
190 | 286 | $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
|
191 | 287 |
|
192 | 288 | if ($withCSP) {
|
193 | 289 | $nonceGenerator = $this->getMockBuilder('Symfony\Bundle\WebProfilerBundle\Csp\NonceGenerator')->getMock();
|
194 | 290 | $nonceGenerator->method('generate')->willReturn('dummy_nonce');
|
195 | 291 |
|
196 |
| - return new ProfilerController($urlGenerator, $profiler, $twig, [], new ContentSecurityPolicyHandler($nonceGenerator)); |
| 292 | + return new ProfilerController($urlGenerator, $profiler, $twig, $templates, new ContentSecurityPolicyHandler($nonceGenerator)); |
197 | 293 | }
|
198 | 294 |
|
199 |
| - return new ProfilerController($urlGenerator, $profiler, $twig, []); |
| 295 | + return new ProfilerController($urlGenerator, $profiler, $twig, $templates); |
200 | 296 | }
|
201 | 297 | }
|
0 commit comments