Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Test] Be able to configure the container between two request in test env #49930

Copy link
Copy link
Open
@lyrixx

Description

@lyrixx
Issue body actions

Description

Hello,

There is a little (annoying) piece of code in symfony, and I would like to enhance of it

if ($this->hasPerformedRequest && $this->reboot) {
$this->kernel->boot();
$this->kernel->shutdown();
} else {

If you want to configure the container before the first request, it's possible.
But if you already have issued a request, it's not possible anymore.

I know it's possible to disable the reboot() of the container, but it has drawbacks (that's why we added the reboot). And in my case I do want a restart!

Some code to understand:

class HomepageTest extends WebTestCase
{
    public function testSomething(): void
    {
        $client = static::createClient();

        // $client->disableReboot(); // it fix this issue, but it's a bad idea.

        $client->request('GET', '/');

        $this->assertResponseIsSuccessful();
        $this->assertSelectorTextContains('h1', 'Hello World');

        static::getContainer()->set(You::class, new class implements HelloInterface {
            public function hello(): string
            {
                return 'Hello mock';
            }
        });

        $client->request('GET', '/you');

        $this->assertResponseIsSuccessful();
        $this->assertSelectorTextContains('h1', 'Hello mock');
    }
}

This test does not work. Since the container is restarted just before issuing the second request, our mock doesn't exist anymore.


I would like to introduce something like this:

// test.php
$client->configureContainer(function ($container) {
    $container->set(You::class, new class implements HelloInterface {
        public function hello(): string
        {
            return 'Hello mock';
        }
    });
});

And this code will be execute after the reboot, but before the request is issued.

WDTY?

If accepted, I'll do the PR!


Side note: I tried so many things, like static::ensureKernelIsShutDown, or self::$kernel->shutdown() and nothing works! It's even worse : there is a miss-match between the kernel in the client and the one in the test (because everything is static)

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.