8000 [Tests] Migrate tests to static data providers · symfony/web-profiler-bundle@ab75733 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab75733

Browse files
[Tests] Migrate tests to static data providers
1 parent f649564 commit ab75733

File tree

1 file changed

+94
-52
lines changed

1 file changed

+94
-52
lines changed

Tests/Controller/ProfilerControllerTest.php

Lines changed: 94 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\WebProfilerBundle\Tests\Controller;
1313

14+
use PHPUnit\Framework\MockObject\MockObject;
1415
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1516
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1617
use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
@@ -366,6 +367,99 @@ public function provideCspVariants()
366367
* @dataProvider defaultPanelProvider
367368
*/
368369
public function testDefaultPanel(string $expectedPanel, Profile $profile)
370+
{
371+
$this->assertDefaultPanel($expectedPanel, $profile);
372+
}
373+
374+
public static function defaultPanelProvider(): \Generator
375+
{
376+
// Test default behavior
377+
$profile = new Profile('xxxxxx');
378+
$profile->addCollector($requestDataCollector = new RequestDataCollector());
379+
yield [$requestDataCollector->getName(), $profile];
380+
381+
// Test exception
382+
$profile = new Profile('xxxxxx');
383+
$profile->addCollector($exceptionDataCollector = new ExceptionDataCollector());
384+
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());
385+
yield [$exceptionDataCollector->getName(), $profile];
386+
}
387+
388+
private function createController($profiler, $twig, $withCSP, array $templates = []): ProfilerController
389+
{
390+
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
391+
392+
if ($withCSP) {
393+
$nonceGenerator = $this->createMock(NonceGenerator::class);
394+
$nonceGenerator->method('generate')->willReturn('dummy_nonce');
395+
396+
return new ProfilerController($urlGenerator, $profiler, $twig, $templates, new ContentSecurityPolicyHandler($nonceGenerator));
397+
}
398+
399+
return new ProfilerController($urlGenerator, $profiler, $twig, $templates);
400+
}
401+
402+
public function testDumpPanelExceptionPriority()
403+
{
404+
$exceptionDataCollector = new ExceptionDataCollector();
405+
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());
406+
407+
$dumpDataCollector = $this->createDumpDataCollector();
408+
409+
$profile = new Profile('xxxxxx');
410+
$profile->setCollectors([$exceptionDataCollector, $dumpDataCollector]);
411+
412+
$this->assertDefaultPanel($exceptionDataCollector->getName(), $profile);
413+
}
414+
415+
public function testDumpPanelWhenDefinedAfterwards()
416+
{
417+
$exceptionDataCollector = new ExceptionDataCollector();
418+
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());
419+
420+
$dumpDataCollector = $this->createDumpDataCollector();
421+
$dumpDataCollector
422+
->expects($this->atLeastOnce())
423+
->method('getDumpsCount')
424+
->willReturn(1)
425+
;
426+
427+
$profile = new Profile('xxxxxx');
428+
$profile->setCollectors([$dumpDataCollector, $exceptionDataCollector]);
429+
430+
$this->assertDefaultPanel($exceptionDataCollector->getName(), $profile);
431+
}
432+
433+
public function testDumpPanel()
434+
{
435+
$dumpDataCollector = $this->createDumpDataCollector();
436+
$dumpDataCollector
437+
->expects($this->atLeastOnce())
438+
->method('getDumpsCount')
439+
->willReturn(1)
440+
;
441+
442+
$profile = new Profile('xxxxxx');
443+
$profile->addCollector($dumpDataCollector);
444+
445+
$this->assertDefaultPanel($dumpDataCollector->getName(), $profile);
446+
}
447+
448+
/**
449+
* @return MockObject<DumpDataCollector>
450+
*/
451+
private function createDumpDataCollector(): MockObject
452+
{
453+
$dumpDataCollector = $this->createMock(DumpDataCollector::class);
454+
$dumpDataCollector
455+
->expects($this->atLeastOnce())
456+
->method('getName')
457+
->willReturn('dump');
458+
459+
return $dumpDataCollector;
460+
}
461+
462+
private function assertDefaultPanel(string $expectedPanel, Profile $profile)
369463
{
370464
$profiler = $this->createMock(Profiler::class);
371465
$profiler
@@ -415,56 +509,4 @@ public function testDefaultPanel(string $expectedPanel, Profile $profile)
415509
}, $collectorsNames))
416510
->panelAction(new Request(), $profile->getToken());
417511
}
418-
419-
public function defaultPanelProvider(): \Generator
420-
{
421-
// Test default behavior
422-
$profile = new Profile('xxxxxx');
423-
$profile->addCollector($requestDataCollector = new RequestDataCollector());
424-
yield [$requestDataCollector->getName(), $profile];
425-
426-
// Test exception
427-
$profile = new Profile('xxxxxx');
428-
$profile->addCollector($exceptionDataCollector = new ExceptionDataCollector());
429-
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());
430-
yield [$exceptionDataCollector->getName(), $profile];
431-
432-
// Test exception priority
433-
$dumpDataCollector = $this->createMock(DumpDataCollector::class);
434-
$dumpDataCollector
435-
->expects($this->atLeastOnce())
436-
->method('getName')
437-
->willReturn('dump');
438-
$dumpDataCollector
439-
->expects($this->atLeastOnce())
440-
->method('getDumpsCount')
441-
->willReturn(1);
442-
$profile = new Profile('xxxxxx');
443-
$profile->setCollectors([$exceptionDataCollector, $dumpDataCollector]);
444-
yield [$exceptionDataCollector->getName(), $profile];
445-
446-
// Test exception priority when defined afterwards
447-
$profile = new Profile('xxxxxx');
448-
$profile->setCollectors([$dumpDataCollector, $exceptionDataCollector]);
449-
yield [$exceptionDataCollector->getName(), $profile];
450-
451-
// Test dump
452-
$profile = new Profile('xxxxxx');
453-
$profile->addCollector($dumpDataCollector);
454-
yield [$dumpDataCollector->getName(), $profile];
455-
}
456-
457-
private function createController($profiler, $twig, $withCSP, array $templates = []): ProfilerController
458-
{
459-
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
460-
461-
if ($withCSP) {
462-
$nonceGenerator = $this->createMock(NonceGenerator::class);
463-
$nonceGenerator->method('generate')->willReturn('dummy_nonce');
464-
465-
return new ProfilerController($urlGenerator, $profiler, $twig, $templates, new ContentSecurityPolicyHandler($nonceGenerator));
466-
}
467-
468-
return new ProfilerController($urlGenerator, $profiler, $twig, $templates);
469-
}
470512
}

0 commit comments

Comments
 (0)
0