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

A Configuration Option for being able to retain container between requests #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jon-acker
Copy link

In the simplest case where you make only one request to your app - access to the services in the container from Behat context file works, but once you make another request then Symfony will reboot the kernel and you will lose access to the container from the Behat context files.

If I am running a scenario where I set the value of a symfony service in the Given step (for example an in-memory repository), make a request to load a form and then make another request to press the form button (which will have an effect of setting a value in this service), I will no longer have access to the original instance of the service from Behat.

Using this option, I construct the a new "test.client" (instead of the existing one) using an instance of Behat's kernel. Effectively, the kernel is only rebooted once every scenario rather than every request.

I find the possibility of this very useful when running scenarios against in-memory repositories, though I suppose it should be made clear that this option should it potentially dangerous as it changes the way symfony would normally work, and means that if you had stateful services then there is no guarantee what state they will be in after the first request.

@jon-acker
Copy link
Author

@everzet what do you think of this?

@ciaranmcnulty
Copy link
Contributor

Haven't reviewed the code but like it in theory - good for apps that can handle multiple requests and have truly stateless services

Copy link
Contributor

@sroze sroze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea!

{
parent::__construct($kernel->getContainer()->get('test.client'), $baseUrl);
$client = (true === $shared) ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a one-liner

@@ -80,6 +80,12 @@ public function configure(ArrayNodeDefinition $builder)
->end()
->defaultTrue()
->end()
->booleanNode('shared')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shared_container ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, it's the kernel that's being shared, which has a reference to the container.

* @Given I have not configured behat to use shared kernel
*/
public function iHaveNotConfiguredBehatToUseSharedKernel()
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you simply assert that the symfony2_extension.kernel.shared parameter has the correct value?

Copy link
Author

@jon-acker jon-acker Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no :( because of course the application is only started on the @when step, so I would either remove this step def, or have it write the config file with that value (I actually wanted to avoid that, which is why I used different profiles)

@ghostika
Copy link

Is there a solution here already?

coyc pushed a commit to coyc/Symfony2Extension that referenced this pull request Jan 22, 2019
devdotweb pushed a commit to campanda/Symfony2Extension that referenced this pull request Mar 5, 2019
devdotweb pushed a commit to campanda/Symfony2Extension that referenced this pull request Mar 5, 2019
devdotweb pushed a commit to campanda/Symfony2Extension that referenced this pull request Mar 5, 2019
devdotweb pushed a commit to campanda/Symfony2Extension that referenced this pull request Mar 5, 2019
@prodigeris
Copy link

Any news?

@holyspecter
Copy link

Would be nice to have it merged. For now this one worked for me: #90 (comment).
Thank you @jon-acker!

{
parent::__construct($kernel->getContainer()->get('test.client'), $baseUrl);
$client = (true === $shared) ?
new Client($kernel) :
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client is now deprecated. Should be replaced with Symfony\Component\HttpKernel\HttpKernelBrowser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants