Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing some service in container + validate form #107

Closed
nikophil opened this issue Aug 31, 2022 · 3 comments
Closed

Replacing some service in container + validate form #107

nikophil opened this issue Aug 31, 2022 · 3 comments

Comments

@nikophil
Copy link
Member

Hello,

I have troubles mocking some HttpClient instance with the browser after validating a form.

Here is the code:

        $this->browser()
            ->use(function () {
                self::getContainer()->set('test.http_client', new MockHttpClient(new MockResponse()));
            })
            ->visit('/') // some http calls are made here, using the mock client
            ->assertSuccessful()
            ->fillField('form[date]', '2021-11-10') // fill some fields
            ->click('form_search') // this click redirects us on the same page
            ->assertSuccessful() // :boom: 500 => the http_client is not mocked

Adding some dumps give me some clues

            ->use(function () {
                // prints Symfony\Component\HttpClient\MockHttpClient
                dump(self::getContainer()->get('test.http_client')::class);
            })
            ->click('form_search')
            ->use(function () {
                // prints Symfony\Component\HttpClient\RetryableHttpClient
                dump(self::getContainer()->get('test.http_client')::class);
            })

any ideas?

I saw this issue which is somehow related, but I don't even know where I could reuse the same "mocked container" or something else...

thanks for your help!

@kbond
Copy link
Member

kbond commented Aug 31, 2022

I think https://github.com/Happyr/service-mocking could help here. Or what about $browser->disableReboot()?

@kbond
Copy link
Member

kbond commented Aug 31, 2022

BTW, the built container should be considered immutable - calling set() could have unintended consequences I believe. I think the service mocking library is your best bet. It allows you to swap/mock services and these swapped/mocked services will be used for the remainder of your test - even between kernel reboots/container resets.

@nikophil
Copy link
Member Author

nikophil commented Sep 1, 2022

hi @kbond

thanks for your quick answer, $browser->disableReboot() resovled the problem. I know it's not the best solution but it's a quick win.

🙏

@nikophil nikophil closed this as completed Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants