Skip to content

Commit

Permalink
Introduce a service in the test application that exposes driver's ser…
Browse files Browse the repository at this point in the history
…vice container
  • Loading branch information
pamil committed Apr 4, 2020
1 parent 776af6c commit b053048
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ Feature: Accessing driver's service container
final class SomeContext implements Context {
private $mink;
private $container;
private $driverContainer;
public function __construct(Mink $mink, ContainerInterface $container)
public function __construct(Mink $mink, ContainerInterface $driverContainer)
{
$this->mink = $mink;
$this->container = $container;
$this->driverContainer = $driverContainer;
}
/** @Given the counter service is zeroed */
Expand All @@ -67,31 +67,25 @@ Feature: Accessing driver's service container
private function getCounterService(): Counter
{
return $this->container
->get('behat.service_container')
->get('fob_symfony.driver_kernel')
->getContainer()
->get('test.service_container')
->get('App\Counter')
;
return $this->driverContainer->get('App\Counter');
}
}
"""

Scenario: Injecting Mink serivce
Scenario: Accessing a service from driver's service container (manually injected dependencies)
Given a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- '@behat.mink'
- '@service_container'
- '@behat.driver.service_container'
"""
When I run Behat
Then it should pass

Scenario: Autowiring and autoconfiguring Mink service
Scenario: Accessing a service from driver's service container (autowired & autoconfigured dependencies)
Given a YAML services file containing:
"""
services:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
use Behat\Mink\Mink;
use Behat\Mink\Session;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use FriendsOfBehat\SymfonyExtension\ServiceContainer\SymfonyExtension;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;

final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements CompilerPassInterface
{
public function load(array $configs, ContainerBuilder $container): void
{
$this->provideMinkIntegration($container);
$this->registerBehatContainer($container);
$this->registerDriverBehatContainer($container);

$container->registerForAutoconfiguration(Context::class)->addTag('fob.context');
}
Expand All @@ -47,6 +51,27 @@ private function registerBehatContainer(ContainerBuilder $container): void
$container->setDefinition('behat.service_container', $behatServiceContainerDefinition);
}

private function registerDriverBehatContainer(ContainerBuilder $container): void
{
$driverKernelDefinition = new Definition(KernelInterface::class, [SymfonyExtension::DRIVER_KERNEL_ID]);
$driverKernelDefinition->setFactory([new Reference('behat.service_container'), 'get']);
$driverKernelDefinition->setPublic(true);
$driverKernelDefinition->setLazy(true);

$driverServiceContainerDefinition = new Definition(ContainerInterface::class);
$driverServiceContainerDefinition->setFactory([$driverKernelDefinition, 'getContainer']);
$driverServiceContainerDefinition->setPublic(true);
$driverServiceContainerDefinition->setLazy(true);

$driverTestServiceContainerDefinition = new Definition(ContainerInterface::class, ['test.service_container']);
$driverTestServiceContainerDefinition->setFactory([$driverServiceContainerDefinition, 'get']);
$driverTestServiceContainerDefinition->setPublic(true);
$driverTestServiceContainerDefinition->setLazy(true);

$container->setDefinition('behat.driver.service_container', $driverTestServiceContainerDefinition);
$container->registerAliasForArgument('behat.driver.service_container', ContainerInterface::class, 'driver container');
}

private function provideBrowserKitIntegration(ContainerBuilder $container): void
{
if (!class_exists(Client::class) || !$container->has('test.client')) {
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceContainer/SymfonyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class SymfonyExtension implements Extension
* Kernel used by Symfony driver to isolate web container from contexts' container.
* Container is rebuilt before every request.
*/
private const DRIVER_KERNEL_ID = 'fob_symfony.driver_kernel';
public const DRIVER_KERNEL_ID = 'fob_symfony.driver_kernel';

/** @var bool */
private $minkExtensionFound = false;
Expand Down

0 comments on commit b053048

Please sign in to comment.