FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Codeception/src/Codeception/Module.php at main · Codeception/Codeception · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Codeception
/
Codeception
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
1.3k
Star
4.9k
Code
Issues
151
Pull requests
16
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Codeception
/
src
/
Codeception
/
Module.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
317 lines (278 loc) · 7.82 KB
Breadcrumbs
Codeception
/
src
/
Codeception
/
Module.php
Copy path
File metadata and controls
317 lines (278 loc) · 7.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
declare
(strict_types=
1
);
namespace
Codeception
;
use
Codeception
\
Exception
\
ModuleConfigException
;
use
Codeception
\
Exception
\
ModuleException
;
use
Codeception
\
Lib
\
Interfaces
\
RequiresPackage
;
use
Codeception
\
Lib
\
ModuleContainer
;
use
Codeception
\
Util
\
Shared
\
Asserts
;
use
Exception
;
/**
* 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;
/**
* By setting it to false module wan't inherit methods of parent class.
*/
public
static
bool
$
includeInheritedActions
=
true
;
/**
* Allows to explicitly set what methods have this class.
*/
public
static
array
$
onlyActions
= [];
/**
* Allows to explicitly exclude actions from module.
*/
public
static
array
$
excludeActions
= [];
/**
* Allows to rename actions
*/
public
static
array
$
aliases
= [];
protected
array
$
storage
= [];
protected
array
$
config
= [];
protected
array
$
backupConfig
= [];
protected
array
$
requiredFields
= [];
/**
* Module constructor.
*
* Requires module container (to provide access between modules of suite) and config.
*/
public
function
__construct
(
protected
ModuleContainer
$
moduleContainer
, ?
array
$
config
=
null
)
{
$
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);
* }
* ```
*
* @throws ModuleConfigException|ModuleException
*/
public
function
_setConfig
(
array
$
config
):
void
{
$
this
->
config
=
array_merge
(
$
this
->
config
,
$
config
);
$
this
->
backupConfig
=
$
this
->
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]);
* }
* }
* ```
*
* @throws ModuleConfigException|ModuleException
*/
public
function
_reconfigure
(
array
$
config
):
void
{
$
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
():
void
{
$
this
->
config
=
$
this
->
backupConfig
;
}
/**
* Validates current config for required fields and required packages.
*
* @throws ModuleConfigException|ModuleException
*/
protected
function
validateConfig
():
void
{
if
((
$
missing
=
array_diff
(
$
this
->
requiredFields
,
array_keys
(
$
this
->
config
))) !== []) {
throw
new
ModuleConfigException
(
static
::class,
sprintf
(
"\n
Options: %s are required
\n
Please, update the configuration and set all the required fields
\n\n"
,
implode
(
'
,
'
,
$
missing
)
)
);
}
if
(
$
this
instanceof
RequiresPackage) {
$
errors
=
''
;
foreach
(
$
this
->
_requires
()
as
$
className
=>
$
package
) {
if
(!
class_exists
(
$
className
)) {
$
errors
.=
"
Class
{
$
className
}
can't be loaded, please add
{
$
package
}
to composer.json
\n"
;
}
}
if
(
$
errors
!==
''
) {
throw
new
ModuleException
(
$
this
,
$
errors
);
}
}
}
/**
* Returns a module name for a Module, a class name for Helper
*/
public
function
_getName
():
string
{
$
moduleName
=
'\\'
.
static
::class;
return
str_starts_with
(
$
moduleName
, ModuleContainer::
MODULE_NAMESPACE
)
?
substr
(
$
moduleName
,
strlen
(ModuleContainer::
MODULE_NAMESPACE
))
:
$
moduleName
;
}
/**
* Checks if a module has required fields
*/
public
function
_hasRequiredFields
():
bool
{
return
$
this
->
requiredFields
!== [];
}
/**
* **HOOK** triggered after module is created and configuration is loaded
*/
public
function
_initialize
()
{
}
/**
* **HOOK** executed before suite
*/
public
function
_beforeSuite
(
array
$
settings
= [])
{
}
/**
* **HOOK** executed after suite
*/
public
function
_afterSuite
()
{
}
/**
* **HOOK** executed before step
*/
public
function
_beforeStep
(
Step
$
step
)
{
}
/**
* **HOOK** executed after step
*/
public
function
_afterStep
(
Step
$
step
)
{
}
/**
* **HOOK** executed before test
*/
public
function
_before
(
TestInterface
$
test
)
{
}
/**
* **HOOK** executed after test
*/
public
function
_after
(
TestInterface
$
test
)
{
}
/**
* **HOOK** executed when test fails but before `_after`
*/
public
function
_failed
(
TestInterface
$
test
,
Exception
$
fail
)
{
}
/**
* Print debug message to the screen.
*/
protected
function
debug
(
mixed
$
message
):
void
{
codecept_debug
(
$
message
);
}
/**
* Print debug message with a title
*/
protected
function
debugSection
(
string
$
title
,
mixed
$
msg
):
void
{
if
(
is_array
(
$
msg
) ||
is_object
(
$
msg
)) {
$
msg
=
json_encode
(
$
msg
,
JSON_THROW_ON_ERROR
|
JSON_INVALID_UTF8_SUBSTITUTE
|
JSON_UNESCAPED_SLASHES
);
}
$
this
->
debug
(
"
[
{
$
title
}
]
{
$
msg
}"
);
}
/**
* Short text message to an amount of chars
*/
protected
function
shortenMessage
(
string
$
message
,
int
$
chars
=
150
):
string
{
return
mb_substr
(
$
message
,
0
,
$
chars
,
'
utf-8
'
);
}
/**
* Checks that module is enabled.
*/
protected
function
hasModule
(
string
$
name
):
bool
{
return
$
this
->
moduleContainer
->
hasModule
(
$
name
);
}
/**
* Get all enabled modules
*/
protected
function
getModules
():
array
{
return
$
this
->
moduleContainer
->
all
();
}
/**
* Get another module by its name:
*
* ```php
* <?php
* $this->getModule('WebDriver')->_findElements('.items');
* ```
*
* @throws ModuleException
*/
protected
function
getModule
(
string
$
name
):
Module
{
if
(!
$
this
->
hasModule
(
$
name
)) {
$
this
->
moduleContainer
->
throwMissingModuleExceptionWithSuggestion
(
self
::class,
$
name
);
}
return
$
this
->
moduleContainer
->
getModule
(
$
name
);
}
/**
* Get config values or specific config item.
*
* @param string|null $key
* @return mixed the config item's value or null if it doesn't exist
*/
public
function
_getConfig
(?
string
$
key
=
null
):
mixed
{
return
$
key
===
null
?
$
this
->
config
: (
$
this
->
config
[
$
key
] ??
null
);
}
protected
function
scalarizeArray
(
array
$
array
):
array
{
array_walk_recursive
(
$
array
,
static
function
(&
$
value
):
void
{
if
(!
is_null
(
$
value
) && !
is_scalar
(
$
value
)) {
$
value
= (
string
)
$
value
;
}
}
);
return
$
array
;
}
}
Back
|
FazBrowse Home
|
New Git URL