SymfonyExtension v2.0: proof of concept
This commit is contained in:
24
src/Context/Environment/ContextServiceEnvironment.php
Normal file
24
src/Context/Environment/ContextServiceEnvironment.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the SymfonyExtension package.
|
||||
*
|
||||
* (c) Kamil Kokot <kamil@kokot.me>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FriendsOfBehat\SymfonyExtension\Context\Environment;
|
||||
|
||||
use Behat\Behat\Context\Environment\ContextEnvironment;
|
||||
use FriendsOfBehat\SymfonyExtension\Context\Environment\Handler\ContextServiceEnvironmentHandler;
|
||||
|
||||
/**
|
||||
* @see ContextServiceEnvironmentHandler
|
||||
*/
|
||||
interface ContextServiceEnvironment extends ContextEnvironment
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the SymfonyExtension package.
|
||||
*
|
||||
* (c) Kamil Kokot <kamil@kokot.me>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FriendsOfBehat\SymfonyExtension\Context\Environment\Handler;
|
||||
|
||||
use Behat\Behat\Context\Context;
|
||||
use Behat\Behat\Context\Initializer\ContextInitializer;
|
||||
use Behat\Testwork\Environment\Environment;
|
||||
use Behat\Testwork\Environment\Exception\EnvironmentIsolationException;
|
||||
use Behat\Testwork\Environment\Handler\EnvironmentHandler;
|
||||
use Behat\Testwork\Suite\Exception\SuiteConfigurationException;
|
||||
use Behat\Testwork\Suite\Suite;
|
||||
use FriendsOfBehat\SymfonyExtension\Context\Environment\InitialisedContextServiceEnvironment;
|
||||
use FriendsOfBehat\SymfonyExtension\Context\Environment\UninitialisedContextServiceEnvironment;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
final class ContextServiceEnvironmentHandler implements EnvironmentHandler
|
||||
{
|
||||
/** @var KernelInterface */
|
||||
private $symfonyKernel;
|
||||
|
||||
/** @var ContextInitializer[] */
|
||||
private $contextInitializers = [];
|
||||
|
||||
public function __construct(KernelInterface $symfonyKernel)
|
||||
{
|
||||
$this->symfonyKernel = $symfonyKernel;
|
||||
}
|
||||
|
||||
public function supportsSuite(Suite $suite): bool
|
||||
{
|
||||
return $suite->hasSetting('contexts');
|
||||
}
|
||||
|
||||
public function buildEnvironment(Suite $suite): Environment
|
||||
{
|
||||
$environment = new UninitialisedContextServiceEnvironment($suite);
|
||||
foreach ($this->getSuiteContextsServices($suite) as $contextId) {
|
||||
$environment->registerContextService($contextId, $this->getContextClass($contextId));
|
||||
}
|
||||
|
||||
return $environment;
|
||||
}
|
||||
|
||||
public function supportsEnvironmentAndSubject(Environment $environment, $testSubject = null): bool
|
||||
{
|
||||
return $environment instanceof UninitialisedContextServiceEnvironment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws EnvironmentIsolationException
|
||||
*/
|
||||
public function isolateEnvironment(Environment $uninitializedEnvironment, $testSubject = null): Environment
|
||||
{
|
||||
/** @var UninitialisedContextServiceEnvironment $uninitializedEnvironment */
|
||||
$this->assertEnvironmentCanBeIsolated($uninitializedEnvironment, $testSubject);
|
||||
|
||||
$environment = new InitialisedContextServiceEnvironment($uninitializedEnvironment->getSuite());
|
||||
foreach ($uninitializedEnvironment->getContextServices() as $contextId) {
|
||||
/** @var Context $context */
|
||||
$context = $this->getContext($contextId);
|
||||
$this->initializeInstance($context);
|
||||
$environment->registerContext($context);
|
||||
}
|
||||
|
||||
return $environment;
|
||||
}
|
||||
|
||||
public function registerContextInitializer(ContextInitializer $initializer): void
|
||||
{
|
||||
$this->contextInitializers[] = $initializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*
|
||||
* @throws SuiteConfigurationException If "contexts" setting is not an array
|
||||
*/
|
||||
private function getSuiteContextsServices(Suite $suite): array
|
||||
{
|
||||
$contextsServices = $suite->getSetting('contexts');
|
||||
|
||||
if (!is_array($contextsServices)) {
|
||||
throw new SuiteConfigurationException(sprintf(
|
||||
'"contexts" setting of the "%s" suite is expected to be an array, %s given.',
|
||||
$suite->getName(),
|
||||
gettype($contextsServices)
|
||||
), $suite->getName());
|
||||
}
|
||||
|
||||
return $contextsServices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws EnvironmentIsolationException
|
||||
*/
|
||||
private function assertEnvironmentCanBeIsolated(Environment $uninitializedEnvironment, $testSubject): void
|
||||
{
|
||||
if (!$this->supportsEnvironmentAndSubject($uninitializedEnvironment, $testSubject)) {
|
||||
throw new EnvironmentIsolationException(sprintf(
|
||||
'"%s" does not support isolation of "%s" environment.',
|
||||
static::class,
|
||||
get_class($uninitializedEnvironment)
|
||||
), $uninitializedEnvironment);
|
||||
}
|
||||
}
|
||||
|
||||
private function initializeInstance(Context $context): void
|
||||
{
|
||||
foreach ($this->contextInitializers as $initializer) {
|
||||
$initializer->initializeContext($context);
|
||||
}
|
||||
}
|
||||
|
||||
private function getContextClass(string $contextId): string
|
||||
{
|
||||
if ($this->getContainer()->has($contextId)) {
|
||||
return get_class($this->getContainer()->get($contextId));
|
||||
}
|
||||
|
||||
$class = '\\' . ltrim($contextId, '\\');
|
||||
|
||||
if (class_exists($class)) {
|
||||
return $class;
|
||||
}
|
||||
|
||||
throw new \Exception('wtf?');
|
||||
}
|
||||
|
||||
private function getContext(string $contextId): Context
|
||||
{
|
||||
if ($this->getContainer()->has($contextId)) {
|
||||
return $this->getContainer()->get($contextId);
|
||||
}
|
||||
|
||||
$class = '\\' . ltrim($contextId, '\\');
|
||||
|
||||
if (class_exists($class)) {
|
||||
return new $class();
|
||||
}
|
||||
|
||||
throw new \Exception('wtf?');
|
||||
}
|
||||
|
||||
private function getContainer(): ContainerInterface
|
||||
{
|
||||
return $this->symfonyKernel->getContainer();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the SymfonyExtension package.
|
||||
*
|
||||
* (c) Kamil Kokot <kamil@kokot.me>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FriendsOfBehat\SymfonyExtension\Context\Environment;
|
||||
|
||||
use Behat\Behat\Context\Context;
|
||||
use Behat\Behat\Context\Exception\ContextNotFoundException;
|
||||
use Behat\Testwork\Call\Callee;
|
||||
use Behat\Testwork\Suite\Suite;
|
||||
use FriendsOfBehat\SymfonyExtension\Context\Environment\Handler\ContextServiceEnvironmentHandler;
|
||||
|
||||
/**
|
||||
* @see ContextServiceEnvironmentHandler
|
||||
*/
|
||||
final class InitialisedContextServiceEnvironment implements ContextServiceEnvironment
|
||||
{
|
||||
/** @var Suite */
|
||||
private $suite;
|
||||
|
||||
/** @var Context[] */
|
||||
private $contexts = [];
|
||||
|
||||
public function __construct(Suite $suite)
|
||||
{
|
||||
$this->suite = $suite;
|
||||
}
|
||||
|
||||
public function registerContext(Context $context): void
|
||||
{
|
||||
$this->contexts[get_class($context)] = $context;
|
||||
}
|
||||
|
||||
public function getSuite(): Suite
|
||||
{
|
||||
return $this->suite;
|
||||
}
|
||||
|
||||
public function bindCallee(Callee $callee): callable
|
||||
{
|
||||
$callable = $callee->getCallable();
|
||||
|
||||
if ($callee->isAnInstanceMethod()) {
|
||||
return [$this->getContext($callable[0]), $callable[1]];
|
||||
}
|
||||
|
||||
return $callable;
|
||||
}
|
||||
|
||||
public function hasContexts(): bool
|
||||
{
|
||||
return count($this->contexts) > 0;
|
||||
}
|
||||
|
||||
public function getContextClasses(): array
|
||||
{
|
||||
return array_keys($this->contexts);
|
||||
}
|
||||
|
||||
public function hasContextClass($class): bool
|
||||
{
|
||||
return isset($this->contexts[$class]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContextNotFoundException
|
||||
*/
|
||||
private function getContext(string $class): Context
|
||||
{
|
||||
if (!isset($this->contexts[$class])) {
|
||||
throw new ContextNotFoundException(sprintf(
|
||||
'`%s` context is not found in the suite environment. Have you registered it?',
|
||||
$class
|
||||
), $class);
|
||||
}
|
||||
|
||||
return $this->contexts[$class];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the SymfonyExtension package.
|
||||
*
|
||||
* (c) Kamil Kokot <kamil@kokot.me>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FriendsOfBehat\SymfonyExtension\Context\Environment;
|
||||
|
||||
use Behat\Testwork\Environment\StaticEnvironment;
|
||||
use FriendsOfBehat\SymfonyExtension\Context\Environment\Handler\ContextServiceEnvironmentHandler;
|
||||
|
||||
/**
|
||||
* @see ContextServiceEnvironmentHandler
|
||||
*/
|
||||
final class UninitialisedContextServiceEnvironment extends StaticEnvironment implements ContextServiceEnvironment
|
||||
{
|
||||
/** @var string[] */
|
||||
private $contextServices = [];
|
||||
|
||||
public function registerContextService(string $serviceId, string $serviceClass): void
|
||||
{
|
||||
$this->contextServices[$serviceId] = $serviceClass;
|
||||
}
|
||||
|
||||
public function getContextServices(): array
|
||||
{
|
||||
return array_keys($this->contextServices);
|
||||
}
|
||||
|
||||
public function hasContexts(): bool
|
||||
{
|
||||
return count($this->contextServices) > 0;
|
||||
}
|
||||
|
||||
public function getContextClasses(): array
|
||||
{
|
||||
return array_values($this->contextServices);
|
||||
}
|
||||
|
||||
public function hasContextClass($class): bool
|
||||
{
|
||||
return in_array($class, $this->contextServices, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user