Skip to content

Commit

Permalink
Add priority to compiler passes in TestKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
chapterjason committed Dec 16, 2023
1 parent 017cee2 commit 74dce82
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
Expand Down Expand Up @@ -42,7 +43,7 @@ class TestKernel extends Kernel
private $testProjectDir;

/**
* @var CompilerPassInterface[]
* @var array{CompilerPassInterface, int}[]
*/
private $testCompilerPasses = [];

Expand Down Expand Up @@ -131,19 +132,19 @@ protected function buildContainer(): ContainerBuilder
{
$container = parent::buildContainer();

foreach ($this->testCompilerPasses as $pass) {
$container->addCompilerPass($pass);
foreach ($this->testCompilerPasses as $compilerPass) {
$container->addCompilerPass($compilerPass[0], PassConfig::TYPE_BEFORE_OPTIMIZATION, $compilerPass[1]);
}

return $container;
}

/**
* @param CompilerPassInterface $compilerPasses
* @param CompilerPassInterface $compilerPass
*/
public function addTestCompilerPass($compilerPasses): void
public function addTestCompilerPass($compilerPass, $priority = 0): void
{
$this->testCompilerPasses[] = $compilerPasses;
$this->testCompilerPasses[] = [$compilerPass, $priority];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DeRegisterSomethingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('something')) {
$container->removeDefinition('something');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class RegisterSomethingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('something')) {
return;
}

$definition = new Definition();
$definition->setClass(\stdClass::class);
$definition->setPublic(true);

$container->setDefinition('something', $definition);
}
}
39 changes: 39 additions & 0 deletions tests/Functional/BundleConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Nyholm\BundleTest\TestKernel;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\DeRegisterSomethingPass;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\RegisterSomethingPass;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down Expand Up @@ -61,4 +63,41 @@ public function testBundleWithDifferentConfigurationFormats($config): void
$this->assertEquals('val1', $container->getParameter('app.foo'));
$this->assertEquals(['val2', 'val3'], $container->getParameter('app.bar'));
}

public function testAddCompilerPassPriority(): void
{
// CASE 1: Compiler pass without priority, should be prioritized by order of addition
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
$kernel->addTestCompilerPass(new DeRegisterSomethingPass());
$kernel->addTestCompilerPass(new RegisterSomethingPass());
}]);

$container = $kernel->getContainer();

$this->assertTrue($container->has('something'));

// CASE 2: Compiler pass with priority, should be prioritized by priority
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
$kernel->addTestCompilerPass(new DeRegisterSomethingPass(), -5);
$kernel->addTestCompilerPass(new RegisterSomethingPass());
}]);

$container = $kernel->getContainer();

$this->assertFalse($container->has('something'));

// CASE 3: Compiler pass without priority, should be prioritized by order of addition
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
// DeRegisterSomethingPass is now added as second compiler pass
$kernel->addTestCompilerPass(new RegisterSomethingPass());
$kernel->addTestCompilerPass(new DeRegisterSomethingPass());
}]);

$container = $kernel->getContainer();

$this->assertFalse($container->has('something'));
}
}

0 comments on commit 74dce82

Please sign in to comment.