Repository navigation
Expand file tree
/
Copy pathSuiteManager.php
More file actions
284 lines (224 loc) · 9.45 KB
/
Copy pathSuiteManager.php
File metadata and controls
284 lines (224 loc) · 9.45 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace Codeception;
use Codeception\Event\SuiteEvent;
use Codeception\Util\Annotation;
use Symfony\Component\Finder\Finder;
use Symfony\Component\EventDispatcher\EventDispatcher;
class SuiteManager {
protected static $formats = array('Cest', 'Cept', 'Test');
/**
* @var Module[]
*/
public static $modules = array();
public static $actions = array();
/**
* @var \PHPUnit_Framework_TestSuite
*/
protected $suite = null;
/**
* @var null|EventDispatcher
*/
protected $dispatcher = null;
protected $tests = array();
protected $debug = false;
protected $path = '';
protected $testcaseClass = 'Codeception\TestCase';
protected $printer = null;
protected $env = null;
protected $settings = array();
public function __construct(EventDispatcher $dispatcher, $name, $settings)
{
$this->settings = $settings;
$this->dispatcher = $dispatcher;
$this->suite = $this->createSuite($name);
$this->path = $settings['path'];
if ($settings['bootstrap']) $this->settings['bootstrap'] = $this->path . $settings['bootstrap'];
if (isset($settings['current_environment'])) $this->env = $settings['current_environment'];
if (!file_exists($settings['path'] . $settings['class_name'] . '.php')) {
throw new Exception\Configuration($settings['class_name'] . " class doesn't exists in suite folder.\nRun the 'build' command to generate it");
}
require_once $settings['path'] . $settings['class_name'].'.php';
$this->initializeModules($settings);
}
public static function hasModule($moduleName)
{
return isset(self::$modules[$moduleName]);
}
protected function initializeModules($settings)
{
self::$modules = Configuration::modules($settings);
self::$actions = Configuration::actions(self::$modules);
foreach (self::$modules as $module) {
$module->_initialize();
}
}
protected function createSuite($name) {
$suiteClass = $this->settings['suite_class'];
if (!class_exists($suiteClass)) throw new \Codeception\Exception\Configuration("Suite class $suiteClass not found");
$suite = new $suiteClass;
if ($this->settings['namespace']) $name = $this->settings['namespace'] . ".$name";
$suite->setName($name);
if (!($suite instanceof \PHPUnit_Framework_TestSuite)) throw new \Codeception\Exception\Configuration("Suite class is not inherited from PHPUnit_Framework_TestSuite");
return $suite;
}
public function addTest($path) {
$testClasses = $this->getClassesFromFile($path);
foreach ($testClasses as $testClass) {
$reflected = new \ReflectionClass($testClass);
if ($reflected->isAbstract()) continue;
foreach ($reflected->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$test = $this->createTestFromPhpUnitMethod($reflected, $method);
if (!$test) continue;
if (!$this->isCurrentEnvironment(Annotation::forMethod($testClass, $method->name)->fetchAll('env'))) continue;
$groups = \PHPUnit_Util_Test::getGroups($testClass, $method->name);
$this->suite->addTest($test, $groups);
}
}
}
public function addCept($file)
{
$name = $this->relativeName($file);
$this->tests[$name] = $file;
$cept = new TestCase\Cept($this->dispatcher, array(
'name' => $name,
'file' => $file,
'bootstrap' => $this->settings['bootstrap']
));
$cept->preload();
if (! $this->isCurrentEnvironment($cept->getScenario()->getEnv())) {
return;
}
$this->suite->addTest($cept, $cept->getScenario()->getGroups());
}
public function addCest($file) {
$name = $this->relativeName($file);
$this->tests[$name] = $file;
$testClasses = $this->getClassesFromFile($file);
foreach ($testClasses as $testClass) {
$guy = $this->settings['namespace']
? $this->settings['namespace'] . '\\' . $this->settings['class_name']
: $this->settings['class_name'];
$unit = new $testClass;
$methods = get_class_methods($testClass);
foreach ($methods as $method) {
if ($method == '__construct') continue;
$test = $this->createTestFromCestMethod($unit, $method, $file, $guy);
if (!$test) continue;
if (!$this->isCurrentEnvironment($test->getScenario()->getEnv())) continue;
$this->suite->addTest($test, \PHPUnit_Util_Test::getGroups($testClass, $method));
}
}
}
protected function relativeName($file)
{
return $name = str_replace($this->path, '', $file);
}
public function run(PHPUnit\Runner $runner, \PHPUnit_Framework_TestResult $result, $options)
{
$this->dispatcher->dispatch(
CodeceptionEvents::SUITE_BEFORE,
new SuiteEvent($this->suite, $result, $this->settings)
);
$runner->doEnhancedRun($this->suite, $result, $options);
$this->dispatcher->dispatch(
CodeceptionEvents::SUITE_AFTER,
new SuiteEvent($this->suite, $result, $this->settings)
);
}
public function loadTest($path) {
if (!file_exists($path)) throw new \Exception("File $path not found");
foreach (self::$formats as $format) {
if (preg_match("~$format.php$~", $path)) {
call_user_func(array($this, "add$format"), $path);
return;
}
}
if (is_dir($path)) {
$this->path = $path;
$this->loadTests();
return;
}
throw new \Exception('Test format not supported. Please, check you use the right suffix. Available filetypes: Cept, Cest, Test');
}
public function loadTests()
{
$finder = Finder::create()->files()->sortByName()->in($this->path);
foreach (self::$formats as $format) {
$formatFinder = clone($finder);
$testFiles = $formatFinder->name("*$format.php");
foreach ($testFiles as $test) {
call_user_func(array($this, "add$format"), $test->getPathname());
}
}
}
protected function getClassesFromFile($file)
{
$loaded_classes = get_declared_classes();
require_once $file;
$extra_loaded_classes = get_declared_classes();
return array_diff($extra_loaded_classes,$loaded_classes);
}
protected function createTestFromPhpUnitMethod(\ReflectionClass $class, \ReflectionMethod $method)
{
if (!\PHPUnit_Framework_TestSuite::isTestMethod($method)) return;
$test = \PHPUnit_Framework_TestSuite::createTest($class, $method->name);
if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) {
foreach ($test->tests() as $t) {
$this->enhancePhpunitTest($t);
}
return $test;
}
$this->enhancePhpunitTest($test);
return $test;
}
protected function enhancePhpunitTest(\PHPUnit_Framework_TestCase $test)
{
$className = get_class($test);
$methodName = $test->getName(false);
$test->setDependencies(\PHPUnit_Util_Test::getDependencies($className, $methodName));
if (!$test instanceof TestCase\Test) {
if ($this->settings['bootstrap']) require_once $this->settings['bootstrap'];
return;
}
$guy = $this->settings['namespace']
? $this->settings['namespace'] . '\\' . $this->settings['class_name']
: $this->settings['class_name'];
$test->setBootstrap($this->settings['bootstrap']);
$test->setDispatcher($this->dispatcher);
$test->setGuyClass($guy);
$test->getScenario()->groups(\PHPUnit_Util_Test::getGroups($className, $methodName));
$test->getScenario()->env(Annotation::forMethod($className, $methodName)->fetchAll('env'));
}
protected function createTestFromCestMethod($cestInstance, $methodName, $file, $guy)
{
$testClass = get_class($cestInstance);
if (strpos($methodName, '_') === 0) return;
$overriddenGuy = Annotation::forMethod($testClass, $methodName)->fetch('guy');
if (!$overriddenGuy) $overriddenGuy = Annotation::forClass($testClass)->fetch('guy');
if ($overriddenGuy) $guy = $overriddenGuy;
$cest = new TestCase\Cest($this->dispatcher, array(
'name' => $methodName,
'instance' => $cestInstance,
'method' => $methodName,
'file' => $file,
'bootstrap' => $this->settings['bootstrap'],
'guy' => $guy
));
$cest->getScenario()->env(Annotation::forMethod($testClass, $methodName)->fetchAll('env'));
$cest->getScenario()->groups(\PHPUnit_Util_Test::getGroups($testClass, $methodName));
$cest->setDependencies(\PHPUnit_Util_Test::getDependencies($testClass, $methodName));
$cest->preload();
return $cest;
}
/**
* @return null|\PHPUnit_Framework_TestSuite
*/
public function getSuite() {
return $this->suite;
}
protected function isCurrentEnvironment($envs)
{
if (empty($envs)) return true;
return $this->env and in_array($this->env, $envs);
}
}