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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ public function prepareProcess()
$this->process = new Process(null);
}


/**
* @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)

}

/**
* @Given I have configured behat to use shared kernel
*/
public function iHaveConfiguredBehatToUseSharedKernel()
{
}

/**
* Runs behat command with provided parameters
*
Expand Down
22 changes: 22 additions & 0 deletions features/shared_kernel.feature
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)
"""
9 changes: 7 additions & 2 deletions src/Behat/Symfony2Extension/Driver/KernelDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Behat\Symfony2Extension\Driver;

use Behat\Mink\Driver\BrowserKitDriver;
use Symfony\Component\HttpKernel\Client;
use Symfony\Component\HttpKernel\KernelInterface;

/**
Expand All @@ -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) ?
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

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

$kernel->getContainer()->get('test.client');

parent::__construct($client, $baseUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function buildDriver(array $config)
return new Definition('Behat\Symfony2Extension\Driver\KernelDriver', array(
new Reference(Symfony2Extension::KERNEL_ID),
'%mink.base_url%',
'%symfony2_extension.kernel.shared%'
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.

->beforeNormalization()
->ifString()->then($boolFilter)
->end()
->defaultFalse()
->end()
->end()
->end()
->arrayNode('context')
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions testapp/app/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
imports:
- { resource: services.yml }

framework:
secret: test_blah
router:
Expand Down
4 changes: 4 additions & 0 deletions testapp/app/config/routing.yml
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 }
3 changes: 3 additions & 0 deletions testapp/app/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
name.service:
class: Behat\Sf2DemoBundle\Service\NameService
26 changes: 24 additions & 2 deletions testapp/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default:
type: symfony_bundle
bundle: 'BehatSf2DemoBundle'
filters:
tags: '~@web'
tags: '~@web && ~@shared-kernel'
web:
type: symfony_bundle
contexts:
Expand All @@ -30,6 +30,28 @@ default:
- "%%kernel.environment%%"
- "%%kernel.debug%%"
- "%%kernel.name%%"
nameService: '@name.service'

bundle: 'BehatSf2DemoBundle'
filters:
tags: '@web'
tags: '@web && ~@shared-kernel'

shared-kernel:
extensions:
Behat\Symfony2Extension:
kernel:
shared: true
suites:
web:
filters:
tags: '@shared-kernel'

no-shared-kernel:
extensions:
Behat\Symfony2Extension:
kernel:
shared: false
suites:
web:
filters:
tags: '@shared-kernel'
7 changes: 7 additions & 0 deletions testapp/src/Behat/Sf2DemoBundle/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public function indexAction(Request $request)
RESPONSE
);
}

public function setNameAction($name)
{
$this->container->get('name.service')->setName($name);

return new Response();
}
}
35 changes: 34 additions & 1 deletion testapp/src/Behat/Sf2DemoBundle/Features/Context/WebContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
namespace Behat\Sf2DemoBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Sf2DemoBundle\Service\NameService;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\KernelInterface;

class WebContext extends MinkContext implements KernelAwareContext
{
private $kernel;

/**
* @var NameService
*/
private $nameService;

public function __construct(Session $session, $simpleParameter, $simpleArg, array $services, array $params)
public function __construct(Session $session, $simpleParameter, $simpleArg, array $services, array $params, NameService $nameService)
{
$this->nameService = $nameService;
}

/**
Expand All @@ -25,4 +33,29 @@ public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}

/**
* @Given I have a service set to :name
*/
public function iHaveANameService($name)
{
$this->nameService->setName($name);
}

/**
* @When make a request setting name to :name
*/
public function makeARequestSettingNameTo($name)
{
$this->visit('set-name/'.$name);
}

/**
* @Then the service value should have remained :name
* @Then the service value should have changed to :name
*/
public function theServiceValueShouldRemain($name)
{
Assert::assertEquals($name, $this->nameService->getName());
}
}
8 changes: 8 additions & 0 deletions testapp/src/Behat/Sf2DemoBundle/Features/services.feature
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"
27 changes: 27 additions & 0 deletions testapp/src/Behat/Sf2DemoBundle/Service/NameService.php
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;
}
}