Add support for class resolvers
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
"behat/mink": "^1.7",
|
||||
"behat/mink-browserkit-driver": "^1.3",
|
||||
"behat/mink-extension": "^2.2",
|
||||
"friends-of-behat/service-container-extension": "^1.0",
|
||||
"phpstan/phpstan-shim": "^0.11",
|
||||
"sylius-labs/coding-standard": "^3.0",
|
||||
"symfony/framework-bundle": "^3.4|^4.1",
|
||||
|
||||
65
features/sanity_checks/class_resolvers_compatibility.feature
Normal file
65
features/sanity_checks/class_resolvers_compatibility.feature
Normal file
@@ -0,0 +1,65 @@
|
||||
Feature: Class resolvers compatibility
|
||||
|
||||
Scenario: Using class resolvers while handling context environment
|
||||
Given a working Symfony application with SymfonyExtension configured
|
||||
And a Behat configuration containing:
|
||||
"""
|
||||
default:
|
||||
extensions:
|
||||
FriendsOfBehat\ServiceContainerExtension:
|
||||
imports:
|
||||
- "tests/class_resolver.yml"
|
||||
|
||||
suites:
|
||||
default:
|
||||
contexts:
|
||||
- class:resolved:context
|
||||
"""
|
||||
And a Behat services definition file "tests/class_resolver.yml" containing:
|
||||
"""
|
||||
services:
|
||||
App\Tests\CustomClassResolver:
|
||||
tags: ["context.class_resolver"]
|
||||
"""
|
||||
And a Behat service implementation file "tests/CustomClassResolver.php" containing:
|
||||
"""
|
||||
<?php
|
||||
|
||||
namespace App\Tests;
|
||||
|
||||
use Behat\Behat\Context\ContextClass\ClassResolver;
|
||||
|
||||
final class CustomClassResolver implements ClassResolver
|
||||
{
|
||||
public function supportsClass($contextClass): bool
|
||||
{
|
||||
return $contextClass === 'class:resolved:context';
|
||||
}
|
||||
|
||||
public function resolveClass($contextClass): string
|
||||
{
|
||||
return 'App\Tests\SomeContext';
|
||||
}
|
||||
}
|
||||
"""
|
||||
And a feature file containing:
|
||||
"""
|
||||
Feature:
|
||||
Scenario:
|
||||
Then it should pass
|
||||
"""
|
||||
And a context file "tests/SomeContext.php" containing:
|
||||
"""
|
||||
<?php
|
||||
|
||||
namespace App\Tests;
|
||||
|
||||
use Behat\Behat\Context\Context;
|
||||
|
||||
final class SomeContext implements Context {
|
||||
/** @Then it should pass */
|
||||
public function itShouldPass(): void {}
|
||||
}
|
||||
"""
|
||||
When I run Behat
|
||||
Then it should pass
|
||||
@@ -14,6 +14,7 @@ declare(strict_types=1);
|
||||
namespace FriendsOfBehat\SymfonyExtension\Context\Environment\Handler;
|
||||
|
||||
use Behat\Behat\Context\Context;
|
||||
use Behat\Behat\Context\ContextClass\ClassResolver;
|
||||
use Behat\Behat\Context\Initializer\ContextInitializer;
|
||||
use Behat\Testwork\Environment\Environment;
|
||||
use Behat\Testwork\Environment\Exception\EnvironmentIsolationException;
|
||||
@@ -34,6 +35,9 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
||||
/** @var ContextInitializer[] */
|
||||
private $contextInitializers = [];
|
||||
|
||||
/** @var ClassResolver[] */
|
||||
private $classResolvers = [];
|
||||
|
||||
public function __construct(KernelInterface $symfonyKernel)
|
||||
{
|
||||
$this->symfonyKernel = $symfonyKernel;
|
||||
@@ -79,9 +83,14 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
||||
return $environment;
|
||||
}
|
||||
|
||||
public function registerContextInitializer(ContextInitializer $initializer): void
|
||||
public function registerContextInitializer(ContextInitializer $contextInitializer): void
|
||||
{
|
||||
$this->contextInitializers[] = $initializer;
|
||||
$this->contextInitializers[] = $contextInitializer;
|
||||
}
|
||||
|
||||
public function registerClassResolver(ClassResolver $classResolver): void
|
||||
{
|
||||
$this->classResolvers[] = $classResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,8 +134,21 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveContextId(string $contextId): string
|
||||
{
|
||||
foreach ($this->classResolvers as $resolver) {
|
||||
if ($resolver->supportsClass($contextId)) {
|
||||
return $resolver->resolveClass($contextId);
|
||||
}
|
||||
}
|
||||
|
||||
return $contextId;
|
||||
}
|
||||
|
||||
private function getContextClass(string $contextId): string
|
||||
{
|
||||
$contextId = $this->resolveContextId($contextId);
|
||||
|
||||
if ($this->getContainer()->has($contextId)) {
|
||||
return get_class($this->getContainer()->get($contextId));
|
||||
}
|
||||
@@ -142,6 +164,8 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
||||
|
||||
private function getContext(string $contextId): Context
|
||||
{
|
||||
$contextId = $this->resolveContextId($contextId);
|
||||
|
||||
$class = '\\' . ltrim($contextId, '\\');
|
||||
|
||||
if ($this->getContainer()->has($contextId)) {
|
||||
|
||||
@@ -253,5 +253,9 @@ final class SymfonyExtension implements Extension
|
||||
foreach ($container->findTaggedServiceIds(ContextExtension::INITIALIZER_TAG) as $serviceId => $tags) {
|
||||
$definition->addMethodCall('registerContextInitializer', [$container->getDefinition($serviceId)]);
|
||||
}
|
||||
|
||||
foreach ($container->findTaggedServiceIds(ContextExtension::CLASS_RESOLVER_TAG) as $serviceId => $tags) {
|
||||
$definition->addMethodCall('registerClassResolver', [$container->getDefinition($serviceId)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user