Skip to content

Commit

Permalink
Test compiler passes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Mar 11, 2024
1 parent 6ccf82c commit 9d6c96c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Fixtures\ConfigurationBundle\DependencyInjection\Compiler;

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

final class DeregisterSomethingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($container->hasDefinition('something')) {
$container->removeDefinition('something');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Fixtures\ConfigurationBundle\DependencyInjection\Compiler;

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

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

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

$container->setDefinition('something', $definition);
}
}
44 changes: 44 additions & 0 deletions tests/Functional/CompilerPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace Functional;

use Fixtures\ConfigurationBundle\DependencyInjection\Compiler\DeregisterSomethingPass;
use Fixtures\ConfigurationBundle\DependencyInjection\Compiler\RegisterSomethingPass;
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

final class CompilerPassTest extends ConfigurableKernelTestCase
{
/**
* @test
*/
public function compiler_pass_priority(): void
{
// Case 1: Compiler pass without priority - it should be prioritized by order of addition
self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestCompilerPass(new DeregisterSomethingPass());
$kernel->addTestCompilerPass(new RegisterSomethingPass());
}]);

$this->assertTrue(self::getContainer()->has('something'));

// Case 2: Compiler pass with priority - it should be prioritized by priority
self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestCompilerPass(new DeregisterSomethingPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -5);
$kernel->addTestCompilerPass(new RegisterSomethingPass());
}]);

$this->assertFalse(self::getContainer()->has('something'));

// Case 3: Compiler pass without priority - it should be prioritized by order of addition
self::bootKernel(['config' => function (TestKernel $kernel) {
// DeregisterSomethingPass is now added as second compiler pass
$kernel->addTestCompilerPass(new RegisterSomethingPass());
$kernel->addTestCompilerPass(new DeregisterSomethingPass());
}]);

$this->assertFalse(self::getContainer()->has('something'));
}
}

0 comments on commit 9d6c96c

Please sign in to comment.