Last active
February 24, 2026 11:06
-
-
Save lyrixx/0adb8fd414451596557871d2d9af5695 to your computer and use it in GitHub Desktop.
Test applications services can boot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests\Integration; | |
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | |
| use Symfony\Bundle\FrameworkBundle\Test\TestContainer; | |
| use Symfony\Component\DependencyInjection\ContainerBuilder; | |
| use Symfony\Component\DependencyInjection\Definition; | |
| class ContainerTest extends KernelTestCase | |
| { | |
| public function testContainer() | |
| { | |
| static::bootKernel(['debug' => true]); | |
| /** @var TestContainer $container */ | |
| $container = static::getContainer(); | |
| /** @var string $projectDir */ | |
| $projectDir = $container->getParameter('kernel.project_dir'); | |
| /** @var string $containerDumpFile */ | |
| $containerDumpFile = $container->getParameter('debug.container.dump'); | |
| /** @var ContainerBuilder $dumpedContainer */ | |
| $dumpedContainer = unserialize(file_get_contents(substr_replace($containerDumpFile, '.ser', -4))); | |
| $count = 0; | |
| foreach ($dumpedContainer->getDefinitions() as $id => $service) { | |
| if ($this->isSkipped($id, $service, $dumpedContainer, $projectDir)) { | |
| continue; | |
| } | |
| try { | |
| $container->get($id); | |
| } catch (\Throwable $e) { | |
| $this->fail($e->getMessage()); | |
| } | |
| ++$count; | |
| } | |
| $this->addToAssertionCount($count); | |
| } | |
| private function getFilterList(): array | |
| { | |
| return [ | |
| \App\Foo\Bar::class, | |
| ]; | |
| } | |
| private function isSkipped(string $id, Definition $service, ContainerBuilder $builder, string $projectDir): bool | |
| { | |
| if (\in_array($id, $this->getFilterList(), true)) { | |
| return true; | |
| } | |
| if (str_starts_with($id, '.')) { | |
| return true; // Symfony internal stuff | |
| } | |
| if ($service->isAbstract()) { | |
| return true; // Symfony internal stuff | |
| } | |
| $class = $service->getClass(); | |
| if (!$class) { | |
| return true; // kernel, or alias, or abstract | |
| } | |
| $rc = $builder->getReflectionClass($class, false); | |
| if (!$rc) { | |
| return true; | |
| } | |
| $filename = $rc->getFileName(); | |
| if (!str_starts_with($filename, "{$projectDir}/src")) { | |
| return true; | |
| } | |
| if ($rc->isAbstract()) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| } |
Author
we have more than 20.000 services and 4000 services tested, memory consumption ends at ~600 Mo just for this test 😞
Now that XmlFileLoader is deprecated, what's the best way to resolve this deprecation here? :)
Author
Hello, I haven't tried it yet. However, I think you could take inspiration from this: https://github.com/symfony/symfony/blob/8.1/src/Symfony/Bundle/FrameworkBundle/Command/BuildDebugContainerTrait.php#L33
Thanks, that works well
Author
For the record, I have updated the gist for Symfony 7.4+
Important
for older symfony version, please check older revisions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nikophil
On project I'm working ATM:
So it looks like it does not consume too much memory
Maybe you have a memory leak somewhere...