forked from Codeception/Codeception
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfiguration.php
More file actions
439 lines (353 loc) · 13.8 KB
/
Copy pathConfiguration.php
File metadata and controls
439 lines (353 loc) · 13.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
namespace Codeception;
use Codeception\Exception\Configuration as ConfigurationException;
use Codeception\Util\Autoload;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Finder\Finder;
class Configuration
{
protected static $suites = array();
/**
* @var array Current configuration
*/
protected static $config = null;
/**
* @var string Directory containing main configuration file.
* @see self::projectDir()
*/
protected static $dir = null;
/**
* @var string Current project logs directory.
*/
protected static $logDir = null;
/**
* @var string Current project data directory. This directory is used to hold
* sql dumps and other things needed for current project tests.
*/
protected static $dataDir = null;
/**
* @var string Directory containing helpers. Helpers will be autoloaded if they have suffix "Helper".
*/
protected static $helpersDir = null;
/**
* @var string Directory containing tests and suites of the current project.
*/
protected static $testsDir = null;
public static $lock = false;
/**
* @var array Default config
*/
public static $defaultConfig = array(
'namespace' => '',
'include' => array(),
'paths' => array(),
'modules' => array(),
'extensions' => array(
'enabled' => array(),
'config' => array(),
),
'settings' => array(
'colors' => false,
'log' => false,
'bootstrap' => '_bootstrap.php',
)
);
public static $defaultSuiteSettings = array(
'class_name' => 'NoGuy',
'modules' => array(
'enabled' => array(),
'config' => array(),
),
'suite_class' => '\PHPUnit_Framework_TestSuite',
'error_level' => 'E_ALL & ~E_STRICT & ~E_DEPRECATED',
);
public static function config($configFile = null)
{
if (!$configFile && self::$config) {
return self::$config;
}
if (self::$config && self::$lock) {
return self::$config;
}
if ($configFile === null) {
$configFile = getcwd() . DIRECTORY_SEPARATOR . 'codeception.yml';
}
if (is_dir($configFile)) {
$configFile = $configFile . DIRECTORY_SEPARATOR . 'codeception.yml';
}
$dir = dirname($configFile);
$configDistFile = $dir . DIRECTORY_SEPARATOR . 'codeception.dist.yml';
if (! (file_exists($configDistFile) || file_exists($configFile))) {
throw new ConfigurationException("Configuration file could not be found");
}
$config = self::loadConfigFile($configDistFile, self::$defaultConfig);
$config = self::loadConfigFile($configFile, $config);
if ($config == self::$defaultConfig) {
throw new ConfigurationException("Configuration file is invalid");
}
self::$dir = $dir;
self::$config = $config;
if (!isset($config['paths']['log'])) {
throw new ConfigurationException('Log path is not defined by key "paths: log"');
}
self::$logDir = $config['paths']['log'];
// config without tests, for inclusion of other configs
if (count($config['include']) and !isset($config['paths']['tests'])) {
return $config;
}
if (!isset($config['paths']['tests'])) {
throw new ConfigurationException('Tests directory is not defined in Codeception config by key "paths: tests:"');
}
if (!isset($config['paths']['data'])) {
throw new ConfigurationException('Data path is not defined Codeception config by key "paths: data"');
}
if (!isset($config['paths']['helpers'])) {
throw new ConfigurationException('Helpers path is not defined by key "paths: helpers"');
}
self::$dataDir = $config['paths']['data'];
self::$helpersDir = $config['paths']['helpers'];
self::$testsDir = $config['paths']['tests'];
self::loadBootstrap($config['settings']['bootstrap']);
self::autoloadHelpers();
self::loadSuites();
return $config;
}
protected static function loadBootstrap($bootstrap)
{
if (!$bootstrap) {
return;
}
$bootstrap = self::$dir . DIRECTORY_SEPARATOR . self::$testsDir.DIRECTORY_SEPARATOR.$bootstrap;
if (file_exists($bootstrap)) {
include_once $bootstrap;
}
}
protected static function loadConfigFile($file, $parentConfig)
{
$config = file_exists($file) ? Yaml::parse($file) : array();
return self::mergeConfigs($parentConfig, $config);
}
protected static function autoloadHelpers()
{
Autoload::registerSuffix('Helper', self::helpersDir());
}
protected static function loadSuites()
{
$suites = Finder::create()->files()->name('*.{suite,suite.dist}.yml')->in(self::$dir.DIRECTORY_SEPARATOR.self::$testsDir)->depth('< 1');
self::$suites = array();
foreach ($suites as $suite) {
preg_match('~(.*?)(\.suite|\.suite\.dist)\.yml~', $suite->getFilename(), $matches);
self::$suites[$matches[1]] = $matches[1];
}
}
public static function suiteSettings($suite, $config)
{
// cut namespace name from suite name
if ($suite != $config['namespace'] && substr($suite, 0, strlen($config['namespace'])) == $config['namespace']) {
$suite = substr($suite, strlen($config['namespace']));
}
if (!in_array($suite, self::$suites)) {
throw new \Exception("Suite $suite was not loaded");
}
$globalConf = $config['settings'];
foreach (array('modules','coverage', 'namespace') as $key) {
if (isset($config[$key])) {
$globalConf[$key] = $config[$key];
}
}
$path = $config['paths']['tests'];
$suiteConf = file_exists(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.yml") ? Yaml::parse(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.yml") : array();
$suiteDistconf = file_exists(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.dist.yml") ? Yaml::parse(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.dist.yml") : array();
$settings = self::mergeConfigs(self::$defaultSuiteSettings, $globalConf);
$settings = self::mergeConfigs($settings, $suiteDistconf);
$settings = self::mergeConfigs($settings, $suiteConf);
$settings['path'] = self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $suite . DIRECTORY_SEPARATOR;
return $settings;
}
public static function suiteEnvironments($suite)
{
$settings = self::suiteSettings($suite, self::config());
if (!isset($settings['env']) || !is_array($settings['env'])) {
return array();
}
$environments = array();
foreach ($settings['env'] as $env => $envConfig) {
$environments[$env] = $envConfig ? self::mergeConfigs($settings, $envConfig) : $settings;
$environments[$env]['current_environment'] = $env;
}
return $environments;
}
public static function suites()
{
return self::$suites;
}
/**
* Return instances of enabled modules according suite config.
* Requires Guy class if it exists.
*
* @param array $settings suite settings
* @param bool $requireGuy
* @return array|\Codeception\Module[]
*/
public static function modules($settings, $requireGuy = true)
{
$guyFile = $settings['path'] . DIRECTORY_SEPARATOR . $settings['class_name'] . '.php';
if (file_exists($guyFile) and $requireGuy) {
require_once $guyFile;
}
$modules = array();
$namespace = isset($settings['namespace']) ? $settings['namespace'] : '';
$moduleNames = $settings['modules']['enabled'];
foreach ($moduleNames as $moduleName) {
$moduleConfig = (isset($settings['modules']['config'][$moduleName])) ? $settings['modules']['config'][$moduleName] : array();
$modules[$moduleName] = static::createModule($moduleName, $moduleConfig, $namespace);
}
return $modules;
}
/**
* Creates new module and configures it.
* Module class is searched and resolves according following rules:
*
* 1. if "class" element is fully qualified class name, it will be taken to create module;
* 2. module class will be searched under default namespace, according $namespace parameter:
* $namespace.'\Codeception\Module\' . $class;
* 3. module class will be searched under Codeception module namespace, that is "\Codeception\Module".
*
* @param $class
* @param array $config module configuration
* @param string $namespace default namespace for module.
* @throws Exception\Configuration
* @return \Codeception\Module
*/
public static function createModule($class, $config, $namespace = '')
{
$hasNamespace = (mb_strpos($class, '\\') !== false);
if ($hasNamespace) {
return new $class($config);
}
// try find module under users suite namespace setting
$className = $namespace.'\\Codeception\\Module\\' . $class;
if (!class_exists($className)) {
// fallback to default namespace
$className = '\\Codeception\\Module\\' . $class;
if (!class_exists($className)) {
throw new ConfigurationException($class.' could not be found and loaded');
}
}
return new $className($config);
}
public static function isExtensionEnabled($extensionName)
{
return isset(self::$config['extensions'])
&& isset(self::$config['extensions']['enabled'])
&& in_array($extensionName, self::$config['extensions']['enabled']);
}
public static function actions($modules)
{
$actions = array();
foreach ($modules as $moduleName => $module) {
$class = new \ReflectionClass($module);
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
$inherit = $class->getStaticPropertyValue('includeInheritedActions');
$only = $class->getStaticPropertyValue('onlyActions');
$exclude = $class->getStaticPropertyValue('excludeActions');
// exclude methods when they are listed as excluded
if (in_array($method->name, $exclude)) continue;
if (!empty($only)) {
// skip if method is not listed
if (!in_array($method->name, $only)) continue;
} else {
// skip if method is inherited and inheritActions == false
if (!$inherit and $method->getDeclaringClass() != $class) continue;
}
// those with underscore at the beginning are considered as hidden
if (strpos($method->name, '_') === 0) continue;
$actions[$method->name] = $moduleName;
}
}
return $actions;
}
/**
* Returns current path to `_data` dir.
* Use it to store database fixtures, sql dumps, or other files required by your tests.
*
* @return string
*/
public static function dataDir()
{
return self::$dir . DIRECTORY_SEPARATOR . self::$dataDir . DIRECTORY_SEPARATOR;
}
/**
* Return current path to `_helpers` dir.
* Helpers are custom modules.
*
* @return string
*/
public static function helpersDir()
{
return self::$dir . DIRECTORY_SEPARATOR . self::$helpersDir . DIRECTORY_SEPARATOR;
}
/**
* Returns actual path to current `_log` dir.
* Use it in Helpers or Groups to save result or temporary files.
*
* @return string
* @throws Exception\Configuration
*/
public static function logDir()
{
if (!self::$logDir) {
throw new ConfigurationException("Path for logs not specified. Please, set log path in global config");
}
$dir = realpath(self::$dir . DIRECTORY_SEPARATOR . self::$logDir) . DIRECTORY_SEPARATOR;
if (!is_writable($dir)) {
@mkdir($dir);
@chmod($dir, 777);
}
if (!is_writable($dir)) {
throw new ConfigurationException("Path for logs is not writable. Please, set appropriate access mode for log path.");
}
return $dir;
}
/**
* Returns path to the root of your project.
* Basically returns path to current `codeception.yml` loaded.
* Use this method instead of `__DIR__`, `getcwd()` or anything else.
* @return string
*/
public static function projectDir()
{
return self::$dir . DIRECTORY_SEPARATOR;
}
/**
* Is this a meta-configuration file that just points to other `codeception.yml`?
* If so, it may have no tests by itself.
*
* @return bool
*/
public static function isEmpty()
{
return !(bool)self::$testsDir;
}
public static function mergeConfigs($a1, $a2)
{
if (!is_array($a1) || !is_array($a2)) {
return $a2;
}
$res = array();
foreach ($a2 as $k2 => $v2) {
if (!isset($a1[$k2])) { // if no such key
$res[$k2] = $v2;
unset($a1[$k2]);
continue;
}
$res[$k2] = self::mergeConfigs($a1[$k2], $v2);
unset($a1[$k2]);
}
foreach ($a1 as $k1 => $v1) { // only single elements here left
$res[$k1] = $v1;
}
return $res;
}
}