Initialize contexts registered as services
This commit is contained in:
59
features/mink_integration/mink_context_integration.feature
Normal file
59
features/mink_integration/mink_context_integration.feature
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
Feature: Mink context integration
|
||||||
|
|
||||||
|
Background:
|
||||||
|
Given a working Symfony application with SymfonyExtension configured
|
||||||
|
And a Behat configuration containing:
|
||||||
|
"""
|
||||||
|
default:
|
||||||
|
extensions:
|
||||||
|
Behat\MinkExtension:
|
||||||
|
base_url: "http://localhost:8080/"
|
||||||
|
default_session: symfony
|
||||||
|
sessions:
|
||||||
|
symfony:
|
||||||
|
symfony: ~
|
||||||
|
|
||||||
|
suites:
|
||||||
|
default:
|
||||||
|
contexts:
|
||||||
|
- App\Tests\SomeContext
|
||||||
|
"""
|
||||||
|
And a feature file containing:
|
||||||
|
"""
|
||||||
|
Feature:
|
||||||
|
Scenario:
|
||||||
|
Then I should have Mink injected
|
||||||
|
"""
|
||||||
|
And a context file "tests/SomeContext.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests;
|
||||||
|
|
||||||
|
use Behat\Behat\Context\Context;
|
||||||
|
use Behat\Mink\Mink;
|
||||||
|
use Behat\Mink\Session;
|
||||||
|
use Behat\MinkExtension\Context\MinkContext;
|
||||||
|
|
||||||
|
final class SomeContext extends MinkContext implements Context {
|
||||||
|
/** @Then I should have Mink injected */
|
||||||
|
public function shouldHaveMinkInjected(): void
|
||||||
|
{
|
||||||
|
assert($this->getMink() instanceof Mink);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: Mink context integration with vanilla context
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
|
|
||||||
|
Scenario: Mink context integration with context as a service
|
||||||
|
Given a YAML services file containing:
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
App\Tests\SomeContext:
|
||||||
|
public: true
|
||||||
|
"""
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
@@ -52,8 +52,6 @@ Feature: Switching Mink sessions
|
|||||||
/** @Then I should use Mink session with :driver as a driver*/
|
/** @Then I should use Mink session with :driver as a driver*/
|
||||||
public function shouldUseDriver(string $driverClass): void
|
public function shouldUseDriver(string $driverClass): void
|
||||||
{
|
{
|
||||||
var_dump(get_class($this->session));
|
|
||||||
var_dump(get_class($this->session->getDriver()));
|
|
||||||
assert($this->session->getDriver() instanceof $driverClass);
|
assert($this->session->getDriver() instanceof $driverClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
Feature: Context initializer compatibility
|
Feature: Context initializer compatibility
|
||||||
|
|
||||||
Scenario: Using class resolvers while handling context environment
|
Background:
|
||||||
Given a working Symfony application with SymfonyExtension configured
|
Given a working Symfony application with SymfonyExtension configured
|
||||||
And a Behat configuration containing:
|
And a Behat configuration containing:
|
||||||
"""
|
"""
|
||||||
@@ -67,5 +67,17 @@ Feature: Context initializer compatibility
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
Scenario: Using context initializers while handling context environment for vanilla contexts
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
|
|
||||||
|
Scenario: Using context initializers while handling context environment for contexts as a service
|
||||||
|
Given a YAML services file containing:
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
App\Tests\SomeContext:
|
||||||
|
public: true
|
||||||
|
"""
|
||||||
When I run Behat
|
When I run Behat
|
||||||
Then it should pass
|
Then it should pass
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace FriendsOfBehat\SymfonyExtension\Context\Environment\Handler;
|
|||||||
use Behat\Behat\Context\Context;
|
use Behat\Behat\Context\Context;
|
||||||
use Behat\Behat\Context\Environment\ContextEnvironment;
|
use Behat\Behat\Context\Environment\ContextEnvironment;
|
||||||
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
|
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
|
||||||
|
use Behat\Behat\Context\Initializer\ContextInitializer;
|
||||||
use Behat\Testwork\Environment\Environment;
|
use Behat\Testwork\Environment\Environment;
|
||||||
use Behat\Testwork\Environment\Exception\EnvironmentIsolationException;
|
use Behat\Testwork\Environment\Exception\EnvironmentIsolationException;
|
||||||
use Behat\Testwork\Environment\Handler\EnvironmentHandler;
|
use Behat\Testwork\Environment\Handler\EnvironmentHandler;
|
||||||
@@ -36,12 +37,20 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
|||||||
/** @var EnvironmentHandler */
|
/** @var EnvironmentHandler */
|
||||||
private $decoratedEnvironmentHandler;
|
private $decoratedEnvironmentHandler;
|
||||||
|
|
||||||
|
/** @var ContextInitializer[] */
|
||||||
|
private $contextInitializers = [];
|
||||||
|
|
||||||
public function __construct(KernelInterface $symfonyKernel, EnvironmentHandler $decoratedEnvironmentHandler)
|
public function __construct(KernelInterface $symfonyKernel, EnvironmentHandler $decoratedEnvironmentHandler)
|
||||||
{
|
{
|
||||||
$this->symfonyKernel = $symfonyKernel;
|
$this->symfonyKernel = $symfonyKernel;
|
||||||
$this->decoratedEnvironmentHandler = $decoratedEnvironmentHandler;
|
$this->decoratedEnvironmentHandler = $decoratedEnvironmentHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function registerContextInitializer(ContextInitializer $contextInitializer): void
|
||||||
|
{
|
||||||
|
$this->contextInitializers[] = $contextInitializer;
|
||||||
|
}
|
||||||
|
|
||||||
public function supportsSuite(Suite $suite): bool
|
public function supportsSuite(Suite $suite): bool
|
||||||
{
|
{
|
||||||
return $suite->hasSetting('contexts');
|
return $suite->hasSetting('contexts');
|
||||||
@@ -87,6 +96,8 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
|||||||
/** @var Context $context */
|
/** @var Context $context */
|
||||||
$context = $this->getContainer()->get($serviceId);
|
$context = $this->getContainer()->get($serviceId);
|
||||||
|
|
||||||
|
$this->initializeContext($context);
|
||||||
|
|
||||||
$environment->registerContext($context);
|
$environment->registerContext($context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,6 +111,13 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
|||||||
return $environment;
|
return $environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function initializeContext(Context $context): void
|
||||||
|
{
|
||||||
|
foreach ($this->contextInitializers as $contextInitializer) {
|
||||||
|
$contextInitializer->initializeContext($context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ final class SymfonyExtension implements Extension
|
|||||||
|
|
||||||
public function process(ContainerBuilder $container): void
|
public function process(ContainerBuilder $container): void
|
||||||
{
|
{
|
||||||
|
$this->processEnvironmentHandler($container);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function registerMinkDriver(ExtensionManager $extensionManager): void
|
private function registerMinkDriver(ExtensionManager $extensionManager): void
|
||||||
@@ -177,6 +178,14 @@ final class SymfonyExtension implements Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function processEnvironmentHandler(ContainerBuilder $container): void
|
||||||
|
{
|
||||||
|
$definition = $container->findDefinition('fob_symfony.environment_handler.context_service');
|
||||||
|
foreach ($container->findTaggedServiceIds(ContextExtension::INITIALIZER_TAG) as $serviceId => $tags) {
|
||||||
|
$definition->addMethodCall('registerContextInitializer', [$container->getDefinition($serviceId)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function autodiscoverKernelConfiguration(array $config): array
|
private function autodiscoverKernelConfiguration(array $config): array
|
||||||
{
|
{
|
||||||
if ($config['class'] !== null) {
|
if ($config['class'] !== null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user