Skip to content

[DependencyInjection] Cross-kernel service usage leads to hard to debug error #31233

@ostrolucky

Description

@ostrolucky

Symfony version(s) affected: 4.2.4

Description
This situation can happen accidentally, because it might not be obvious when second kernel is being created. In our situation, we were creating objects in setUp method and saving them in properties, which is usual. In test cases itself we use self::createClient().

If we attempt to use one of these objects we saved earlier, this leads to error
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException : The "kernel" service is synthetic, it needs to be set at boot time before it can be used.

At this point it's really hard to find out what's wrong if you don't realize you booted kernel twice and took a guess that this is what's causing the problem.

How to reproduce

public function testIndex()
{
    $router = static::bootKernel()->getContainer()->get('router');
    $client = static::createClient();
    $router->getRouteCollection();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDXDX = Developer eXperience (anything that improves the experience of using Symfony)FrameworkBundleStatus: Needs Review

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions