Skip to content

Commit c38ce58

Browse files
committed
minor #511 Tests structure optimization (sadikoff)
This PR was squashed before being merged into the 1.0-dev branch (closes #511). Discussion ---------- Tests structure optimization Hi!!! It's again me with some crazy test stuff 😄 Hope it will improve debugging! ping @romaricdrigon and @weaverryan closes #498 Commits ------- 6f9d569 remove reference and garbage 6f1787a restore another lost changes 6016f1e restore lost changes 76c020b Splitting tests for better readability and debugging
2 parents 1c9c2eb + 6f9d569 commit c38ce58

25 files changed

+1659
-1440
lines changed

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
<testsuites>
1818
<testsuite name="Project Test Suite">
1919
<directory>tests/</directory>
20+
<exclude>tests/Maker</exclude>
2021
<exclude>tests/fixtures</exclude>
2122
<exclude>tests/tmp</exclude>
2223
</testsuite>
24+
<testsuite name="Maker Test Suite">
25+
<directory>tests/Maker</directory>
26+
</testsuite>
2327
</testsuites>
2428

2529
<filter>

src/EventRegistry.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public function __construct(EventDispatcherInterface $eventDispatcher)
8181
foreach (self::$newEventsMap as $eventName => $newEventClass) {
8282
//Check if the new event classes exist, if so replace the old one with the new.
8383
if (isset(self::$eventsMap[$eventName]) && class_exists($newEventClass)) {
84-
unset(self::$eventsMap[$eventName]);
85-
self::$eventsMap[$newEventClass] = $newEventClass;
84+
self::$eventsMap[$eventName] = $newEventClass;
8685
}
8786
}
8887
}
@@ -160,4 +159,13 @@ public function getEventClassName(string $event)
160159

161160
return null;
162161
}
162+
163+
public function listActiveEvents(array $events)
164+
{
165+
foreach ($events as $key => $event) {
166+
$events[$key] = sprintf('%s (<fg=yellow>%s</>)', $event, self::$eventsMap[$event]);
167+
}
168+
169+
return $events;
170+
}
163171
}

src/Maker/MakeSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
5959
$events = $this->eventRegistry->getAllActiveEvents();
6060

6161
$io->writeln(' <fg=green>Suggested Events:</>');
62-
$io->listing($events);
62+
$io->listing($this->eventRegistry->listActiveEvents($events));
6363
$question = new Question(sprintf(' <fg=green>%s</>', $command->getDefinition()->getArgument('event')->getDescription()));
6464
$question->setAutocompleterValues($events);
6565
$question->setValidator([Validator::class, 'notBlank']);

src/Test/MakerTestCase.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,23 @@
1212
namespace Symfony\Bundle\MakerBundle\Test;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\MakerBundle\MakerInterface;
16+
use Symfony\Bundle\MakerBundle\Str;
1517

16-
class MakerTestCase extends TestCase
18+
abstract class MakerTestCase extends TestCase
1719
{
20+
private $kernel;
21+
22+
/**
23+
* @dataProvider getTestDetails
24+
*/
25+
public function testExecute(MakerTestDetails $makerTestDetails)
26+
{
27+
$this->executeMakerCommand($makerTestDetails);
28+
}
29+
30+
abstract public function getTestDetails();
31+
1832
protected function executeMakerCommand(MakerTestDetails $testDetails)
1933
{
2034
if (!$testDetails->isSupportedByCurrentPhpVersion()) {
@@ -68,4 +82,17 @@ protected function assertContainsCount(string $needle, string $haystack, int $co
6882
{
6983
$this->assertEquals(1, substr_count($haystack, $needle), sprintf('Found more than %d occurrences of "%s" in "%s"', $count, $needle, $haystack));
7084
}
85+
86+
protected function getMakerInstance(string $makerClass): MakerInterface
87+
{
88+
if (null === $this->kernel) {
89+
$this->kernel = new MakerTestKernel('dev', true);
90+
$this->kernel->boot();
91+
}
92+
93+
// a cheap way to guess the service id
94+
$serviceId = $serviceId ?? sprintf('maker.maker.%s', Str::asRouteName((new \ReflectionClass($makerClass))->getShortName()));
95+
96+
return $this->kernel->getContainer()->get($serviceId);
97+
}
7198
}

src/Test/MakerTestKernel.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Test;
13+
14+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
15+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
16+
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\MakeCommandRegistrationPass;
17+
use Symfony\Bundle\MakerBundle\MakerBundle;
18+
use Symfony\Component\Config\Loader\LoaderInterface;
19+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20+
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\HttpKernel\Kernel;
22+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
23+
use Symfony\Component\Routing\RouteCollectionBuilder;
24+
25+
class MakerTestKernel extends Kernel implements CompilerPassInterface
26+
{
27+
use MicroKernelTrait;
28+
29+
private $testRootDir;
30+
31+
public function __construct(string $environment, bool $debug)
32+
{
33+
$this->testRootDir = sys_get_temp_dir().'/'.uniqid('sf_maker_', true);
34+
35+
parent::__construct($environment, $debug);
36+
}
37+
38+
public function registerBundles()
39+
{
40+
return [
41+
new FrameworkBundle(),
42+
new MakerBundle(),
43+
];
44+
}
45+
46+
protected function configureRoutes(RouteCollectionBuilder $routes)
47+
{
48+
}
49+
50+
protected function configureRouting(RoutingConfigurator $routes)
51+
{
52+
}
53+
54+
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
55+
{
56+
$c->setParameter('kernel.secret', 123);
57+
}
58+
59+
public function getProjectDir()
60+
{
61+
return $this->getRootDir();
62+
}
63+
64+
public function getRootDir()
65+
{
66+
return $this->testRootDir;
67+
}
68+
69+
public function process(ContainerBuilder $container)
70+
{
71+
// makes all makers public to help the tests
72+
foreach ($container->findTaggedServiceIds(MakeCommandRegistrationPass::MAKER_TAG) as $id => $tags) {
73+
$defn = $container->getDefinition($id);
74+
$defn->setPublic(true);
75+
}
76+
}
77+
}

tests/EventRegistryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Event\ConsoleCommandEvent;
1717
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1818
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
19+
use Symfony\Component\HttpKernel\KernelEvents;
1920

2021
class EventRegistryTest extends TestCase
2122
{
@@ -81,7 +82,7 @@ public function testGetNewEventClassNameFromStandardList()
8182
->method('getListeners');
8283

8384
$registry = new EventRegistry($dispatcher);
84-
$this->assertSame(ExceptionEvent::class, $registry->getEventClassName(ExceptionEvent::class));
85+
$this->assertSame(ExceptionEvent::class, $registry->getEventClassName(KernelEvents::EXCEPTION));
8586
}
8687
}
8788

0 commit comments

Comments
 (0)