From 9d6c96c2b475feaacb452df561521908c9608efd Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Mon, 11 Mar 2024 16:21:13 +0100 Subject: [PATCH] Test compiler passes --- .../Compiler/DeregisterSomethingPass.php | 17 +++++++ .../Compiler/RegisterSomethingPass.php | 24 ++++++++++ tests/Functional/CompilerPassTest.php | 44 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/DeregisterSomethingPass.php create mode 100644 tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/RegisterSomethingPass.php create mode 100644 tests/Functional/CompilerPassTest.php diff --git a/tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/DeregisterSomethingPass.php b/tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/DeregisterSomethingPass.php new file mode 100644 index 0000000..250f8dd --- /dev/null +++ b/tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/DeregisterSomethingPass.php @@ -0,0 +1,17 @@ +hasDefinition('something')) { + $container->removeDefinition('something'); + } + } +} diff --git a/tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/RegisterSomethingPass.php b/tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/RegisterSomethingPass.php new file mode 100644 index 0000000..25bf8b2 --- /dev/null +++ b/tests/Fixtures/ConfigurationBundle/DependencyInjection/Compiler/RegisterSomethingPass.php @@ -0,0 +1,24 @@ +hasDefinition('something')) { + return; + } + + $definition = new Definition(); + $definition->setClass(\stdClass::class); + $definition->setPublic(true); + + $container->setDefinition('something', $definition); + } +} diff --git a/tests/Functional/CompilerPassTest.php b/tests/Functional/CompilerPassTest.php new file mode 100644 index 0000000..a0e8d29 --- /dev/null +++ b/tests/Functional/CompilerPassTest.php @@ -0,0 +1,44 @@ + 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')); + } +}