Skip to content

Commit 2bbd597

Browse files
authored
Allow to set type and priority for addTestCompilerPass (#88)
1 parent ae94a00 commit 2bbd597

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
88

99
- Support for Symfony 6.2, 6.3 and 6.4
1010
- Support for PHP 8.2 and 8.3
11+
- Allow to set type and priority for compiler passes in `TestKernel::addTestCompilerPass`
1112

1213
## 2.0.0
1314

src/TestKernel.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
88
use Symfony\Component\Config\Loader\LoaderInterface;
99
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1011
use Symfony\Component\DependencyInjection\ContainerBuilder;
1112
use Symfony\Component\Filesystem\Filesystem;
1213
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
@@ -42,7 +43,7 @@ class TestKernel extends Kernel
4243
private $testProjectDir;
4344

4445
/**
45-
* @var CompilerPassInterface[]
46+
* @var array{CompilerPassInterface, string, int}[]
4647
*/
4748
private $testCompilerPasses = [];
4849

@@ -131,19 +132,23 @@ protected function buildContainer(): ContainerBuilder
131132
{
132133
$container = parent::buildContainer();
133134

134-
foreach ($this->testCompilerPasses as $pass) {
135-
$container->addCompilerPass($pass);
135+
foreach ($this->testCompilerPasses as $compilerPass) {
136+
$container->addCompilerPass($compilerPass[0], $compilerPass[1], $compilerPass[2]);
136137
}
137138

138139
return $container;
139140
}
140141

141142
/**
142-
* @param CompilerPassInterface $compilerPasses
143+
* @param CompilerPassInterface $compilerPass
144+
* @param string $type
145+
* @param int $priority
146+
*
147+
* @psalm-param PassConfig::TYPE_* $type
143148
*/
144-
public function addTestCompilerPass($compilerPasses): void
149+
public function addTestCompilerPass($compilerPass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, $priority = 0): void
145150
{
146-
$this->testCompilerPasses[] = $compilerPasses;
151+
$this->testCompilerPasses[] = [$compilerPass, $type, $priority];
147152
}
148153

149154
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
class DeRegisterSomethingPass implements CompilerPassInterface
9+
{
10+
public function process(ContainerBuilder $container)
11+
{
12+
if ($container->hasDefinition('something')) {
13+
$container->removeDefinition('something');
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Definition;
8+
9+
class RegisterSomethingPass implements CompilerPassInterface
10+
{
11+
public function process(ContainerBuilder $container)
12+
{
13+
if ($container->hasDefinition('something')) {
14+
return;
15+
}
16+
17+
$definition = new Definition();
18+
$definition->setClass(\stdClass::class);
19+
$definition->setPublic(true);
20+
21+
$container->setDefinition('something', $definition);
22+
}
23+
}

tests/Functional/BundleConfigurationTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use Nyholm\BundleTest\TestKernel;
66
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
7+
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\DeRegisterSomethingPass;
8+
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\RegisterSomethingPass;
79
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
10+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
811
use Symfony\Component\DependencyInjection\ContainerBuilder;
912
use Symfony\Component\HttpKernel\KernelInterface;
1013

@@ -61,4 +64,41 @@ public function testBundleWithDifferentConfigurationFormats($config): void
6164
$this->assertEquals('val1', $container->getParameter('app.foo'));
6265
$this->assertEquals(['val2', 'val3'], $container->getParameter('app.bar'));
6366
}
67+
68+
public function testAddCompilerPassPriority(): void
69+
{
70+
// CASE 1: Compiler pass without priority, should be prioritized by order of addition
71+
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
72+
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
73+
$kernel->addTestCompilerPass(new DeRegisterSomethingPass());
74+
$kernel->addTestCompilerPass(new RegisterSomethingPass());
75+
}]);
76+
77+
$container = $kernel->getContainer();
78+
79+
$this->assertTrue($container->has('something'));
80+
81+
// CASE 2: Compiler pass with priority, should be prioritized by priority
82+
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
83+
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
84+
$kernel->addTestCompilerPass(new DeRegisterSomethingPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -5);
85+
$kernel->addTestCompilerPass(new RegisterSomethingPass());
86+
}]);
87+
88+
$container = $kernel->getContainer();
89+
90+
$this->assertFalse($container->has('something'));
91+
92+
// CASE 3: Compiler pass without priority, should be prioritized by order of addition
93+
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
94+
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
95+
// DeRegisterSomethingPass is now added as second compiler pass
96+
$kernel->addTestCompilerPass(new RegisterSomethingPass());
97+
$kernel->addTestCompilerPass(new DeRegisterSomethingPass());
98+
}]);
99+
100+
$container = $kernel->getContainer();
101+
102+
$this->assertFalse($container->has('something'));
103+
}
64104
}

0 commit comments

Comments
 (0)