Skip to content

Commit

Permalink
Splitting tests for better readability and debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
sadikoff committed Dec 5, 2019
1 parent 09fc38c commit 76c020b
Show file tree
Hide file tree
Showing 26 changed files with 1,649 additions and 1,444 deletions.
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/</directory>
<exclude>tests/Maker</exclude>
<exclude>tests/fixtures</exclude>
<exclude>tests/tmp</exclude>
</testsuite>
<testsuite name="Maker Test Suite">
<directory>tests/Maker</directory>
</testsuite>
</testsuites>

<filter>
Expand Down
14 changes: 12 additions & 2 deletions src/EventRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ public function __construct(EventDispatcherInterface $eventDispatcher)
foreach (self::$newEventsMap as $eventName => $newEventClass) {
//Check if the new event classes exist, if so replace the old one with the new.
if (isset(self::$eventsMap[$eventName]) && class_exists($newEventClass)) {
unset(self::$eventsMap[$eventName]);
self::$eventsMap[$newEventClass] = $newEventClass;
self::$eventsMap[$eventName] = $newEventClass;
// unset(self::$eventsMap[$eventName]);
// self::$eventsMap[Str::getShortClassName($newEventClass)] = $newEventClass;
}
}
}
Expand Down Expand Up @@ -160,4 +161,13 @@ public function getEventClassName(string $event)

return null;
}

public function listActiveEvents(array $events)
{
foreach ($events as &$event) {
$event .= sprintf(' (<fg=yellow>%s</>)', self::$eventsMap[$event]);
}

return $events;
}
}
2 changes: 1 addition & 1 deletion src/Maker/MakeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
$events = $this->eventRegistry->getAllActiveEvents();

$io->writeln(' <fg=green>Suggested Events:</>');
$io->listing($events);
$io->listing($this->eventRegistry->listActiveEvents($events));
$question = new Question(sprintf(' <fg=green>%s</>', $command->getDefinition()->getArgument('event')->getDescription()));
$question->setAutocompleterValues($events);
$question->setValidator([Validator::class, 'notBlank']);
Expand Down
29 changes: 28 additions & 1 deletion src/Test/MakerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@
namespace Symfony\Bundle\MakerBundle\Test;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Bundle\MakerBundle\Str;

class MakerTestCase extends TestCase
abstract class MakerTestCase extends TestCase
{
private $kernel;

/**
* @dataProvider getTestDetails
*/
public function testExecute(MakerTestDetails $makerTestDetails)
{
$this->executeMakerCommand($makerTestDetails);
}

abstract public function getTestDetails();

protected function executeMakerCommand(MakerTestDetails $testDetails)
{
if (!$testDetails->isSupportedByCurrentPhpVersion()) {
Expand Down Expand Up @@ -68,4 +82,17 @@ protected function assertContainsCount(string $needle, string $haystack, int $co
{
$this->assertEquals(1, substr_count($haystack, $needle), sprintf('Found more than %d occurrences of "%s" in "%s"', $count, $needle, $haystack));
}

protected function getMakerInstance(string $makerClass): MakerInterface
{
if (null === $this->kernel) {
$this->kernel = new MakerTestKernel('dev', true);
$this->kernel->boot();
}

// a cheap way to guess the service id
$serviceId = $serviceId ?? sprintf('maker.maker.%s', Str::asRouteName((new \ReflectionClass($makerClass))->getShortName()));

return $this->kernel->getContainer()->get($serviceId);
}
}
77 changes: 77 additions & 0 deletions src/Test/MakerTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Test;

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\MakeCommandRegistrationPass;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;

class MakerTestKernel extends Kernel implements CompilerPassInterface
{
use MicroKernelTrait;

private $testRootDir;

public function __construct(string $environment, bool $debug)
{
$this->testRootDir = sys_get_temp_dir().'/'.uniqid('sf_maker_', true);

parent::__construct($environment, $debug);
}

public function registerBundles()
{
return [
new FrameworkBundle(),
new MakerBundle(),
];
}

protected function configureRoutes(RouteCollectionBuilder $routes)
{
}

protected function configureRouting(RoutingConfigurator $routes)
{
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
$c->setParameter('kernel.secret', 123);
}

public function getProjectDir()
{
return $this->getRootDir();
}

public function getRootDir()
{
return $this->testRootDir;
}

public function process(ContainerBuilder $container)
{
// makes all makers public to help the tests
foreach ($container->findTaggedServiceIds(MakeCommandRegistrationPass::MAKER_TAG) as $id => $tags) {
$defn = $container->getDefinition($id);
$defn->setPublic(true);
}
}
}
3 changes: 2 additions & 1 deletion tests/EventRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class EventRegistryTest extends TestCase
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public function testGetNewEventClassNameFromStandardList()
->method('getListeners');

$registry = new EventRegistry($dispatcher);
$this->assertSame(ExceptionEvent::class, $registry->getEventClassName(ExceptionEvent::class));
$this->assertSame(ExceptionEvent::class, $registry->getEventClassName(KernelEvents::EXCEPTION));
}
}

Expand Down
Loading

0 comments on commit 76c020b

Please sign in to comment.