Repository navigation
Expand file tree
/
Copy pathConfiguration.php
More file actions
274 lines (221 loc) · 9.95 KB
/
Copy pathConfiguration.php
File metadata and controls
274 lines (221 loc) · 9.95 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
<?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();
protected static $config = null;
protected static $dir = null;
protected static $logDir = null;
protected static $dataDir = null;
protected static $helpersDir = null;
protected static $testsDir = null;
public static $lock = false;
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];
}
}
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 suites()
{
return self::$suites;
}
public static function modules($settings)
{
if (file_exists($guy = $settings['path'] . DIRECTORY_SEPARATOR . $settings['class_name'] . '.php')) require_once $guy;
$modules = array();
$namespace = isset($settings['namespace']) ? $settings['namespace'] : '';
$moduleNames = $settings['modules']['enabled'];
foreach ($moduleNames as $moduleName)
{
$classname = $namespace.'\\Codeception\\Module\\' . $moduleName;
if (!class_exists($classname)) {
$classname = '\\Codeception\\Module\\' . $moduleName;
if (!class_exists($classname)) {
throw new ConfigurationException($moduleName.' could not be found and loaded');
}
}
$moduleConfig = (isset($settings['modules']['config'][$moduleName])) ? $settings['modules']['config'][$moduleName] : array();
$modules[$moduleName] = new $classname($moduleConfig);
}
return $modules;
}
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;
}
public static function dataDir()
{
return self::$dir . DIRECTORY_SEPARATOR . self::$dataDir . DIRECTORY_SEPARATOR;
}
public static function helpersDir()
{
return self::$dir . DIRECTORY_SEPARATOR . self::$helpersDir . DIRECTORY_SEPARATOR;
}
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;
}
public static function projectDir()
{
return self::$dir . DIRECTORY_SEPARATOR;
}
public static function isEmpty()
{
return !(bool)self::$testsDir;
}
protected 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;
}
}