-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnvironmentLoaderTest.php
188 lines (169 loc) · 5.67 KB
/
EnvironmentLoaderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @author Sergii Bondarenko, <sb@firstvector.org>
*/
namespace Behat\Tests;
// Environment loader tools.
use Behat\EnvironmentLoader;
use Behat\EnvironmentReader;
// Example Behat extension for testing.
use Behat\Tests\ExampleExtension\ServiceContainer\ExampleExtension;
// Behat configuration for testing.
use Behat\Testwork\Suite\GenericSuite;
// Behat extension interface.
use Behat\Testwork\ServiceContainer\Extension;
// Tools for dependency injection.
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
// Behat context extension and tools.
use Behat\Behat\Context\ServiceContainer\ContextExtension;
use Behat\Behat\Context\Initializer\ContextInitializer;
// Behat environment extension and tools.
use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
use Behat\Testwork\Environment\Reader\EnvironmentReader as EnvironmentExtensionReader;
// Interface for uninitialized environments with contexts.
use Behat\Behat\Context\Environment\UninitializedContextEnvironment;
/**
* Class EnvironmentLoaderTest.
*
* @package Behat\Tests
*/
class EnvironmentLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var EnvironmentLoader
*/
private $loader;
/**
* @var ExampleExtension
*/
private $extension;
/**
* @var ContainerBuilder
*/
private $container;
/**
* Properties and their values of the "EnvironmentLoader" instance.
*
* @var array
*/
private $properties = [];
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->extension = new ExampleExtension();
$this->container = new ContainerBuilder();
$this->loader = new EnvironmentLoader($this->extension, $this->container);
}
/**
* @test
*/
public function loaderConstructor()
{
$this->readLoaderProperties();
foreach ([
'path' => sprintf('%s/behat/extensions/ExampleExtension', __DIR__),
'namespace' => sprintf('%s\ExampleExtension', __NAMESPACE__),
'container' => $this->container,
'configKey' => $this->extension->getConfigKey(),
'classPath' => sprintf('%s\%s\Example', $this->properties['namespace'], '%s'),
] as $property => $value) {
static::assertEquals($value, $this->properties[$property]);
}
// Reader was set in constructor.
static::assertTrue($this->isContainerHasDefinition(EnvironmentExtension::READER_TAG));
// Initializer must be defined.
static::assertTrue($this->isEnvironmentHasDefinition(ContextExtension::INITIALIZER_TAG));
// And must not be added to DI container.
static::assertFalse($this->isContainerHasDefinition(ContextExtension::INITIALIZER_TAG));
}
/**
* @test
*
* @depends loaderConstructor
*/
public function addEnvironmentReader()
{
$assert = true;
try {
// If we add custom environment reader to extension then exception won't thrown.
$this->loader->addEnvironmentReader();
} catch (\RuntimeException $e) {
$assert = false;
}
static::assertTrue($this->isEnvironmentHasDefinition(EnvironmentExtension::READER_TAG) === $assert);
}
/**
* @test
*
* @depends loaderConstructor
*/
public function load()
{
$this->loader->load();
$this->readLoaderProperties();
// Ensure that all definitions added to DI container.
foreach (array_keys($this->properties['definitions']) as $definition) {
static::assertTrue($this->isContainerHasDefinition($definition));
}
}
/**
* @test
*
* @depends load
*/
public function readerConstructor()
{
$this->readLoaderProperties();
// Read all contexts of our ExampleExtension.
$reader = new EnvironmentReader($this->properties['path'], $this->properties['namespace']);
// Imagine that we have uninitialized environment with a suite for testing (without configuration).
$environment = new UninitializedContextEnvironment(new GenericSuite('phpunit', []));
static::assertTrue($reader->supportsEnvironment($environment));
// Callees mustn't be returned because they are will be read from environment classes.
static::assertTrue($reader->readEnvironmentCallees($environment) === []);
// ExampleExtension has only one context.
static::assertCount(1, $environment->getContextClasses());
}
/**
* @test
*/
public function runBehatTests()
{
$code = 0;
static::assertTrue(chdir('tests/behat'));
system('../../vendor/bin/behat --no-colors', $code);
static::assertTrue(0 === $code);
}
/**
* Get actual values for properties of the loader.
*/
private function readLoaderProperties()
{
// Read all properties of the loader.
foreach ((new \ReflectionClass($this->loader))->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
$this->properties[$property->name] = static::getObjectAttribute($this->loader, $property->name);
}
}
/**
* @param string $definition
*
* @return bool
*/
private function isContainerHasDefinition($definition)
{
return $this->container->has(sprintf('%s.%s', $this->properties['configKey'], $definition));
}
/**
* @param string $definition
*
* @return bool
*/
private function isEnvironmentHasDefinition($definition)
{
$definitions = static::getObjectAttribute($this->loader, 'definitions');
return isset($definitions[$definition]);
}
}