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
<?php
namespace Codeception;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Interfaces\RequiresPackage;
use Codeception\Lib\ModuleContainer;
use Codeception\Util\Shared\Asserts;
/**
* Basic class for Modules and Helpers.
* You must extend from it while implementing own helpers.
*
* Public methods of this class start with `_` prefix in order to ignore them in actor classes.
* Module contains **HOOKS** which allow to handle test execution routine.
*
*/
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 = [];
/**
* Module constructor.
*
* Requires module container (to provide access between modules of suite) and config.
*
* @param ModuleContainer $moduleContainer
* @param null $config
*/
public function __construct(ModuleContainer $moduleContainer, $config = null)
{
$this->moduleContainer = $moduleContainer;
$this->backupConfig = $this->config;
if (is_array($config)) {
$this->_setConfig($config);
}
}
/**
* Allows to define initial module config.
* Can be used in `_beforeSuite` hook of Helpers or Extensions
*
* ```php
* <?php
* public function _beforeSuite($settings = []) {
* $this->getModule('otherModule')->_setConfig($this->myOtherConfig);
* }
* ```
*
* @param $config
* @throws Exception\ModuleConfigException
* @throws ModuleException
*/
public function _setConfig($config)
{
$this->config = $this->backupConfig = array_merge($this->config, $config);
$this->validateConfig();
}
/**
* Allows to redefine config for a specific test.
* Config is restored at the end of a test.
*
* ```php
* <?php
* // cleanup DB only for specific group of tests
* public function _before(Test $test) {
* if (in_array('cleanup', $test->getMetadata()->getGroups()) {
* $this->getModule('Db')->_reconfigure(['cleanup' => true]);
* }
* }
* ```
*
* @param $config
* @throws Exception\ModuleConfigException
* @throws ModuleException
*/
public function _reconfigure($config)
{
$this->config = array_merge($this->backupConfig, $config);
$this->onReconfigure();
$this->validateConfig();
}
/**
* HOOK to be executed when config changes with `_reconfigure`.
*/
protected function onReconfigure()
{
// update client on reconfigurations
}
/**
* Reverts config changed by `_reconfigure`
*/
public function _resetConfig()
{
$this->config = $this->backupConfig;
}
/**
* Validates current config for required fields and required packages.
*
* @throws Exception\ModuleConfigException
* @throws ModuleException
*/
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"
);
}
if ($this instanceof RequiresPackage) {
$errorMessage = '';
foreach ($this->_requires() as $className => $package) {
if (class_exists($className)) {
continue;
}
$errorMessage .= "Class $className can't be loaded, please add $package to composer.json\n";
}
if ($errorMessage) {
throw new ModuleException($this, $errorMessage);
}
}
}
/**
* Returns a module name for a Module, a class name for Helper
*
* @return string
*/
public function _getName()
{
$moduleName = '\\'.get_class($this);
if (strpos($moduleName, ModuleContainer::MODULE_NAMESPACE) === 0) {
return substr($moduleName, strlen(ModuleContainer::MODULE_NAMESPACE));
}
return $moduleName;
}
/**
* Checks if a module has required fields
*
* @return bool
*/
public function _hasRequiredFields()
{
return !empty($this->requiredFields);
}
/**
* **HOOK** triggered after module is created and configuration is loaded
*/
public function _initialize()
{
}
/**
* **HOOK** executed before suite
*
* @param array $settings
*/
public function _beforeSuite($settings = [])
{
}
/**
* **HOOK** executed after suite
*/
public function _afterSuite()
{
}
/**
* **HOOK** executed before step
*
* @param Step $step
*/
public function _beforeStep(Step $step)
{
}
/**
* **HOOK** executed after step
*
* @param Step $step
*/
public function _afterStep(Step $step)
{
}
/**
* **HOOK** executed before test
*
* @param TestInterface $test
*/
public function _before(TestInterface $test)
{
}
/**
* **HOOK** executed after test
*
* @param TestInterface $test
*/
public function _after(TestInterface $test)
{
}
/**
* **HOOK** executed when test fails but before `_after`
*
* @param TestInterface $test
* @param \Exception $fail
*/
public function _failed(TestInterface $test, $fail)
{
}
/**
* Print debug message to the screen.
*
* @param $message
*/
protected function debug($message)
{
codecept_debug($message);
}
/**
* Print debug message with a title
*
* @param $title
* @param $message
*/
protected function debugSection($title, $message)
{
if (is_array($message) or is_object($message)) {
$message = stripslashes(json_encode($message));
}
$this->debug("[$title] $message");
}
/**
* Checks that module is enabled.
*
* @param $name
* @return bool
*/
protected function hasModule($name)
{
return $this->moduleContainer->hasModule($name);
}
/**
* Get all enabled modules
*
* @return array
*/
protected function getModules()
{
return $this->moduleContainer->all();
}
/**
* Get another module by its name:
*
* ```php
* <?php
* $this->getModule('WebDriver')->_findElements('.items');
* ```
*
* @param $name
* @return Module
* @throws ModuleException
*/
protected function getModule($name)
{
if (!$this->hasModule($name)) {
throw new Exception\ModuleException(__CLASS__, "Module $name couldn't be connected");
}
return $this->moduleContainer->getModule($name);
}
/**
* Get config values or specific config item.
*
* @param null $key
* @return array|mixed|null
*/
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;
}
}