/
Actions.php
301 lines (258 loc) · 9.05 KB
/
Actions.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
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
<?php
namespace Codeception\Lib\Generator;
use Codeception\Codecept;
use Codeception\Configuration;
use Codeception\Lib\Di;
use Codeception\Lib\ModuleContainer;
use Codeception\Util\ReflectionHelper;
use Codeception\Util\Template;
class Actions
{
protected $template = <<<EOF
<?php //[STAMP] {{hash}}
namespace {{namespace}}_generated;
// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile
trait {{name}}Actions
{
/**
* @return \Codeception\Scenario
*/
abstract protected function getScenario();
{{methods}}
}
EOF;
protected $methodTemplate = <<<EOF
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
{{doc}}
* @see \{{module}}::{{method}}()
*/
public function {{action}}({{params}}){{return_type}} {
{{return}}\$this->getScenario()->runStep(new \Codeception\Step\{{step}}('{{method}}', func_get_args()));
}
EOF;
protected $name;
protected $settings;
protected $modules = [];
protected $actions;
protected $numMethods = 0;
/**
* @var array GeneratedStep[]
*/
protected $generatedSteps = [];
public function __construct($settings)
{
$this->name = $settings['actor'];
$this->settings = $settings;
$this->di = new Di();
$modules = Configuration::modules($this->settings);
$this->moduleContainer = new ModuleContainer($this->di, $settings);
foreach ($modules as $moduleName) {
$this->moduleContainer->create($moduleName);
}
$this->modules = $this->moduleContainer->all();
$this->actions = $this->moduleContainer->getActions();
$this->generatedSteps = (array) $settings['step_decorators'];
}
public function produce()
{
$namespace = rtrim($this->settings['namespace'], '\\');
$methods = [];
$code = [];
foreach ($this->actions as $action => $moduleName) {
if (in_array($action, $methods)) {
continue;
}
$class = new \ReflectionClass($this->modules[$moduleName]);
$method = $class->getMethod($action);
$code[] = $this->addMethod($method);
$methods[] = $action;
$this->numMethods++;
}
return (new Template($this->template))
->place('namespace', $namespace ? $namespace . '\\' : '')
->place('hash', self::genHash($this->modules, $this->settings))
->place('name', $this->name)
->place('methods', implode("\n\n ", $code))
->produce();
}
protected function addMethod(\ReflectionMethod $refMethod)
{
$class = $refMethod->getDeclaringClass();
$params = $this->getParamsString($refMethod);
$module = $class->getName();
$body = '';
$doc = $this->addDoc($class, $refMethod);
$doc = str_replace('/**', '', $doc);
$doc = trim(str_replace('*/', '', $doc));
if (!$doc) {
$doc = "*";
}
$returnType = $this->createReturnTypeHint($refMethod);
$methodTemplate = (new Template($this->methodTemplate))
->place('module', $module)
->place('method', $refMethod->name)
->place('return_type', $returnType)
->place('return', ($returnType === ': void' || $returnType === ': never') ? '' : 'return ')
->place('params', $params);
if (0 === strpos($refMethod->name, 'see')) {
$type = 'Assertion';
} elseif (0 === strpos($refMethod->name, 'am')) {
$type = 'Condition';
} else {
$type = 'Action';
}
$body .= $methodTemplate
->place('doc', $doc)
->place('action', $refMethod->name)
->place('step', $type)
->produce();
// add auto generated steps
foreach (array_unique($this->generatedSteps) as $generator) {
if (!is_callable([$generator, 'getTemplate'])) {
throw new \Exception("Wrong configuration for generated steps. $generator doesn't implement \Codeception\Step\GeneratedStep interface");
}
$template = call_user_func([$generator, 'getTemplate'], clone $methodTemplate);
if ($template) {
$body .= $template->produce();
}
}
return $body;
}
/**
* @param \ReflectionMethod $refMethod
* @return array
*/
protected function getParamsString(\ReflectionMethod $refMethod)
{
$params = [];
foreach ($refMethod->getParameters() as $param) {
$type = '';
if (PHP_VERSION_ID >= 70000) {
$reflectionType = $param->getType();
if ($reflectionType !== null) {
$type = $this->stringifyType($reflectionType, $refMethod->getDeclaringClass()) . ' ';
}
}
if ($param->isOptional()) {
$params[] = $type . '$' . $param->name . ' = ' . ReflectionHelper::getDefaultValue($param);
} else {
$params[] = $type . '$' . $param->name;
}
}
return implode(', ', $params);
}
/**
* @param \ReflectionClass $class
* @param \ReflectionMethod $refMethod
* @return string
*/
protected function addDoc(\ReflectionClass $class, \ReflectionMethod $refMethod)
{
$doc = $refMethod->getDocComment();
if (!$doc) {
$interfaces = $class->getInterfaces();
foreach ($interfaces as $interface) {
$i = new \ReflectionClass($interface->name);
if ($i->hasMethod($refMethod->name)) {
$doc = $i->getMethod($refMethod->name)->getDocComment();
break;
}
}
}
if (!$doc and $class->getParentClass()) {
$parent = new \ReflectionClass($class->getParentClass()->name);
if ($parent->hasMethod($refMethod->name)) {
$doc = $parent->getMethod($refMethod->name)->getDocComment();
return $doc;
}
return $doc;
}
return $doc;
}
public static function genHash($modules, $settings)
{
$actions = [];
foreach ($modules as $moduleName => $module) {
$actions[$moduleName] = get_class_methods(get_class($module));
}
return md5(Codecept::VERSION . serialize($actions) . serialize($settings['modules']) . implode(',', (array) $settings['step_decorators']));
}
public function getNumMethods()
{
return $this->numMethods;
}
private function createReturnTypeHint(\ReflectionMethod $refMethod)
{
if (PHP_VERSION_ID < 70000) {
return '';
}
$returnType = $refMethod->getReturnType();
if ($returnType === null) {
return '';
}
return ': ' . $this->stringifyType($returnType, $refMethod->getDeclaringClass());
}
/**
* @param \ReflectionType $type
* @return string
*/
private function stringifyType(\ReflectionType $type, \ReflectionClass $moduleClass)
{
if ($type instanceof \ReflectionUnionType) {
return $this->stringifyNamedTypes($type->getTypes(), $moduleClass, '|');
} elseif ($type instanceof \ReflectionIntersectionType) {
return $this->stringifyNamedTypes($type->getTypes(), $moduleClass, '&');
}
if (PHP_VERSION_ID < 70100) {
$returnTypeString = (string)$type;
} else {
$returnTypeString = $type->getName();
}
return sprintf(
'%s%s',
(PHP_VERSION_ID >= 70100 && $type->allowsNull() && $returnTypeString !== 'mixed') ? '?' : '',
$this->stringifyNamedType($type, $moduleClass)
);
}
/**
* @param array<\ReflectionNamedType|\ReflectionType> $types
* @param \ReflectionClass $moduleClass
* @param string $separator
* @return string
*/
private function stringifyNamedTypes(array $types, \ReflectionClass $moduleClass, $separator)
{
$strings = [];
foreach ($types as $type) {
$strings []= $this->stringifyNamedType($type, $moduleClass);
}
return implode($separator, $strings);
}
/**
* @param \ReflectionNamedType|\ReflectionType $type
* @return string
* @todo param is only \ReflectionNamedType in Codeception 5
*/
private function stringifyNamedType($type, \ReflectionClass $moduleClass)
{
if (PHP_VERSION_ID < 70100) {
$typeName = (string)$type;
} else {
$typeName = $type->getName();
}
if ($typeName === 'self') {
$typeName = $moduleClass->getName();
} elseif ($typeName === 'parent') {
$typeName = $moduleClass->getParentClass()->getName();
}
return sprintf(
'%s%s',
$type->isBuiltin() ? '' : '\\',
$typeName
);
}
}