forked from Codeception/Codeception
/
Module.php
212 lines (173 loc) · 4.66 KB
/
Module.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace Codeception;
use Codeception\Lib\ModuleContainer;
use Codeception\Util\Shared\Asserts;
abstract class Module
{
use Asserts;
/**
* @var ModuleContainer
*/
protected $moduleContainer;
/**
* By setting it to false module wan't inherit methods of parent class.
*
* @var bool
*/
public static $includeInheritedActions = true;
/**
* Allows to explicitly set what methods have this class.
*
* @var array
*/
public static $onlyActions = [];
/**
* Allows to explicitly exclude actions from module.
*
* @var array
*/
public static $excludeActions = [];
/**
* Allows to rename actions
*
* @var array
*/
public static $aliases = [];
protected $storage = [];
protected $config = [];
protected $backupConfig = [];
protected $requiredFields = [];
public function __construct(ModuleContainer $moduleContainer, $config = null)
{
$this->moduleContainer = $moduleContainer;
$this->backupConfig = $this->config;
if (is_array($config)) {
$this->_setConfig($config);
}
}
public function _setConfig($config)
{
$this->config = $this->backupConfig = array_merge($this->config, $config);
$this->validateConfig();
}
public function _reconfigure($config)
{
$this->config = array_merge($this->backupConfig, $config);
$this->onReconfigure();
$this->validateConfig();
}
protected function onReconfigure()
{
// update client on reconfigurations
}
public function _resetConfig()
{
$this->config = $this->backupConfig;
}
protected function validateConfig()
{
$fields = array_keys($this->config);
if (array_intersect($this->requiredFields, $fields) != $this->requiredFields) {
throw new Exception\ModuleConfigException(
get_class($this),
"\nOptions: " . implode(', ', $this->requiredFields) . " are required\n" .
"Please, update the configuration and set all the required fields\n\n"
);
}
}
public function _getName()
{
$moduleName = '\\'.get_class($this);
if (strpos($moduleName, ModuleContainer::MODULE_NAMESPACE) === 0) {
return substr($moduleName, strlen(ModuleContainer::MODULE_NAMESPACE));
}
return $moduleName;
}
public function _hasRequiredFields()
{
return !empty($this->requiredFields);
}
// HOOK: used after configuration is loaded
public function _initialize()
{
}
// HOOK: on every Guy class initialization
public function _cleanup()
{
}
// HOOK: before each suite
public function _beforeSuite($settings = [])
{
}
// HOOK: after suite
public function _afterSuite()
{
}
// HOOK: before every step
public function _beforeStep(Step $step)
{
}
// HOOK: after every step
public function _afterStep(Step $step)
{
}
// HOOK: before scenario
public function _before(TestInterface $test)
{
}
// HOOK: after scenario
public function _after(TestInterface $test)
{
}
// HOOK: on fail
public function _failed(TestInterface $test, $fail)
{
}
protected function debug($message)
{
codecept_debug($message);
}
protected function debugSection($title, $message)
{
if (is_array($message) or is_object($message)) {
$message = stripslashes(json_encode($message));
}
$this->debug("[$title] $message");
}
protected function hasModule($name)
{
return $this->moduleContainer->hasModule($name);
}
protected function getModules()
{
return $this->moduleContainer->all();
}
protected function getModule($name)
{
if (!$this->hasModule($name)) {
throw new Exception\ModuleException(__CLASS__, "Module $name couldn't be connected");
}
return $this->moduleContainer->getModule($name);
}
public function _getConfig($key = null)
{
if (!$key) {
return $this->config;
}
if (isset($this->config[$key])) {
return $this->config[$key];
}
return null;
}
protected function scalarizeArray($array)
{
foreach ($array as $k => $v) {
if (!is_null($v) && !is_scalar($v)) {
$array[$k] = (is_array($v) || $v instanceof \ArrayAccess)
? $this->scalarizeArray($v)
: (string)$v;
}
}
return $array;
}
}