Apply suggestions from code review
Co-Authored-By: pamil <kamil@kokot.me>
This commit is contained in:
committed by
Kamil Kokot
parent
c54c581e74
commit
8a972b5b48
@@ -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 InitializedSymfonyExtensionEnvironment implements SymfonyExtensionEnvironment
|
||||
{
|
||||
/** @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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user