1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Behat\MultiContainerExtension\Context\Environment\Handler; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Behat\Behat\Context\Environment\InitializedContextEnvironment; |
16
|
|
|
use Behat\Testwork\Environment\Environment; |
17
|
|
|
use Behat\Testwork\Environment\Exception\EnvironmentIsolationException; |
18
|
|
|
use Behat\Testwork\Environment\Handler\EnvironmentHandler; |
19
|
|
|
use Behat\Testwork\Suite\Exception\SuiteConfigurationException; |
20
|
|
|
use Behat\Testwork\Suite\Suite; |
21
|
|
|
use Sylius\Behat\MultiContainerExtension\Context\Environment\UninitializedContextServiceEnvironment; |
22
|
|
|
use Sylius\Behat\MultiContainerExtension\ContextRegistry; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Kamil Kokot <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class ContextServiceEnvironmentHandler implements EnvironmentHandler |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ContextRegistry |
32
|
|
|
*/ |
33
|
|
|
private $contextRegistry; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ContainerInterface |
37
|
|
|
*/ |
38
|
|
|
private $container; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ContextRegistry $contextRegistry |
42
|
|
|
* @param ContainerInterface $container |
43
|
|
|
*/ |
44
|
|
|
public function __construct(ContextRegistry $contextRegistry, ContainerInterface $container) |
45
|
|
|
{ |
46
|
|
|
$this->contextRegistry = $contextRegistry; |
47
|
|
|
$this->container = $container; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function supportsSuite(Suite $suite) |
54
|
|
|
{ |
55
|
|
|
return $suite->hasSetting('contexts_as_services'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function buildEnvironment(Suite $suite) |
62
|
|
|
{ |
63
|
|
|
$environment = new UninitializedContextServiceEnvironment($suite); |
64
|
|
|
foreach ($this->getSuiteContexts($suite) as $serviceId) { |
65
|
|
|
$environment->registerContextClass($serviceId, $this->contextRegistry->getClass($serviceId)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $environment; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function supportsEnvironmentAndSubject(Environment $environment, $testSubject = null) |
75
|
|
|
{ |
76
|
|
|
return $environment instanceof UninitializedContextServiceEnvironment; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function isolateEnvironment(Environment $uninitializedEnvironment, $testSubject = null) |
83
|
|
|
{ |
84
|
|
|
if (!$uninitializedEnvironment instanceof UninitializedContextServiceEnvironment) { |
85
|
|
|
throw new EnvironmentIsolationException(sprintf( |
86
|
|
|
'ContextServiceEnvironmentHandler does not support isolation of `%s` environment.', |
87
|
|
|
get_class($uninitializedEnvironment) |
88
|
|
|
), $uninitializedEnvironment); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!$this->container->isScopeActive('scenario')) { |
92
|
|
|
$this->container->enterScope('scenario'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$environment = new InitializedContextEnvironment($uninitializedEnvironment->getSuite()); |
96
|
|
|
foreach ($uninitializedEnvironment->getContextsServicesIds() as $serviceId) { |
97
|
|
|
/** @var Context $context */ |
98
|
|
|
$context = $this->container->get($serviceId); |
99
|
|
|
$environment->registerContext($context); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $environment; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Suite $suite |
107
|
|
|
* |
108
|
|
|
* @return string[] |
109
|
|
|
* |
110
|
|
|
* @throws SuiteConfigurationException If `contexts_as_services` setting is not an array |
111
|
|
|
*/ |
112
|
|
|
private function getSuiteContexts(Suite $suite) |
113
|
|
|
{ |
114
|
|
|
if (!is_array($suite->getSetting('contexts_as_services'))) { |
115
|
|
|
throw new SuiteConfigurationException( |
116
|
|
|
sprintf('`contexts_as_services` setting of the "%s" suite is expected to be an array, %s given.', |
117
|
|
|
$suite->getName(), |
118
|
|
|
gettype($suite->getSetting('contexts_as_services')) |
119
|
|
|
), |
120
|
|
|
$suite->getName() |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $suite->getSetting('contexts_as_services'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|