-
Notifications
You must be signed in to change notification settings - Fork 105
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Feature: Sharing Behat's kernel with the application if config stipulates | ||
|
||
In order to be able to re-use in-memory services between scenario steps | ||
As a Symfony feature tester | ||
I need to be able to share Behat's DI container with the app | ||
|
||
Scenario: Fails to retain service when shared kernel set to false | ||
Given I have not configured behat to use shared kernel | ||
When I run "behat -s web -p no-shared-kernel --no-colors '@BehatSf2DemoBundle/services.feature'" | ||
Then it should fail | ||
And the output should contain: | ||
""" | ||
-'Jim' | ||
""" | ||
|
||
Scenario: Does retain service when shared kernel set to true | ||
Given I have configured behat to use shared kernel | ||
When I run "behat -s web -p shared-kernel --no-colors '@BehatSf2DemoBundle/services.feature'" | ||
Then it should pass with: | ||
""" | ||
1 scenario (1 passed) | ||
""" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Behat\Symfony2Extension\Driver; | ||
|
||
use Behat\Mink\Driver\BrowserKitDriver; | ||
use Symfony\Component\HttpKernel\Client; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
/** | ||
|
@@ -21,8 +22,12 @@ | |
*/ | ||
class KernelDriver extends BrowserKitDriver | ||
{ | ||
public function __construct(KernelInterface $kernel, $baseUrl = null) | ||
public function __construct(KernelInterface $kernel, $baseUrl = null, $shared = false) | ||
{ | ||
parent::__construct($kernel->getContainer()->get('test.client'), $baseUrl); | ||
$client = (true === $shared) ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a one-liner |
||
new Client($kernel) : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$kernel->getContainer()->get('test.client'); | ||
|
||
parent::__construct($client, $baseUrl); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,12 @@ public function configure(ArrayNodeDefinition $builder) | |
->end() | ||
->defaultTrue() | ||
->end() | ||
->booleanNode('shared') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, it's the |
||
->beforeNormalization() | ||
->ifString()->then($boolFilter) | ||
->end() | ||
->defaultFalse() | ||
->end() | ||
->end() | ||
->end() | ||
->arrayNode('context') | ||
|
@@ -180,6 +186,7 @@ private function loadKernel(ContainerBuilder $container, array $config) | |
$container->setDefinition(self::KERNEL_ID, $definition); | ||
$container->setParameter(self::KERNEL_ID . '.path', $config['path']); | ||
$container->setParameter(self::KERNEL_ID . '.bootstrap', $config['bootstrap']); | ||
$container->setParameter(self::KERNEL_ID . '.shared', $config['shared']); | ||
} | ||
|
||
private function loadSuiteGenerator(ContainerBuilder $container, array $config) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
imports: | ||
- { resource: services.yml } | ||
|
||
framework: | ||
secret: test_blah | ||
router: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
test: | ||
path: / | ||
defaults: { _controller: BehatSf2DemoBundle:Test:index } | ||
|
||
test-service: | ||
path: /set-name/{name} | ||
defaults: { _controller: BehatSf2DemoBundle:Test:setName } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
services: | ||
name.service: | ||
class: Behat\Sf2DemoBundle\Service\NameService |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Feature: Retaining container between requests | ||
|
||
@shared-kernel | ||
Scenario: Service retains value between request and assertion for duration of scenario | ||
Given I have a service set to "Jack" | ||
And I am on "/" | ||
When make a request setting name to "Jim" | ||
Then the service value should have changed to "Jim" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Behat\Sf2DemoBundle\Service; | ||
|
||
class NameService | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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)