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
<?php
namespace Codeception;
use Codeception\Lib\ModuleContainer;
use Codeception\Step\Argument\FormattedOutput;
use Codeception\Step\Meta as MetaStep;
use Codeception\Util\Locator;
abstract class Step
{
const STACK_POSITION = 3;
/**
* @var string
*/
protected $action;
/**
* @var array
*/
protected $arguments;
protected $debugOutput;
public $executed = false;
protected $line = null;
protected $file = null;
protected $prefix = 'I';
/**
* @var MetaStep
*/
protected $metaStep = null;
protected $failed = false;
public function __construct($action, array $arguments = [])
{
$this->action = $action;
$this->arguments = $arguments;
}
public function saveTrace()
{
$stack = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
if (count($stack) <= self::STACK_POSITION) {
return;
}
$traceLine = $stack[self::STACK_POSITION - 1];
if (!isset($traceLine['file'])) {
return;
}
$this->file = $traceLine['file'];
$this->line = $traceLine['line'];
$this->addMetaStep($traceLine, $stack);
}
private function isTestFile($file)
{
return preg_match('~[^\\'.DIRECTORY_SEPARATOR.'](Cest|Cept|Test).php$~', $file);
}
public function getName()
{
$class = explode('\\', __CLASS__);
return end($class);
}
public function getAction()
{
return $this->action;
}
public function getLine()
{
if ($this->line && $this->file) {
return codecept_relative_path($this->file) . ':' . $this->line;
}
}
public function hasFailed()
{
return $this->failed;
}
public function getArguments()
{
return $this->arguments;
}
public function getArgumentsAsString($maxLength = 200)
{
$arguments = $this->arguments;
$argumentCount = count($arguments);
$totalLength = $argumentCount - 1; // count separators before adding length of individual arguments
foreach ($arguments as $key => $argument) {
$stringifiedArgument = $this->stringifyArgument($argument);
$arguments[$key] = $stringifiedArgument;
$totalLength += mb_strlen($stringifiedArgument, 'utf-8');
}
if ($totalLength > $maxLength && $maxLength > 0) {
//sort arguments from shortest to longest
uasort($arguments, function ($arg1, $arg2) {
$length1 = mb_strlen($arg1, 'utf-8');
$length2 = mb_strlen($arg2, 'utf-8');
if ($length1 === $length2) {
return 0;
}
return ($length1 < $length2) ? -1 : 1;
});
$allowedLength = floor(($maxLength - $argumentCount + 1) / $argumentCount);
$lengthRemaining = $maxLength;
$argumentsRemaining = $argumentCount;
foreach ($arguments as $key => $argument) {
$argumentsRemaining--;
if (mb_strlen($argument, 'utf-8') > $allowedLength) {
$arguments[$key] = mb_substr($argument, 0, $allowedLength - 4, 'utf-8') . '...' . mb_substr($argument, -1, 1, 'utf-8');
$lengthRemaining -= ($allowedLength + 1);
} else {
$lengthRemaining -= (mb_strlen($arguments[$key], 'utf-8') + 1);
//recalculate allowed length because this argument was short
if ($argumentsRemaining > 0) {
$allowedLength = floor(($lengthRemaining - $argumentsRemaining + 1) / $argumentsRemaining);
}
}
}
//restore original order of arguments
ksort($arguments);
}
return implode(',', $arguments);
}
protected function stringifyArgument($argument)
{
if (is_string($argument)) {
return '"' . strtr($argument, ["\n" => '\n', "\r" => '\r', "\t" => ' ']) . '"';
} elseif (is_resource($argument)) {
$argument = (string)$argument;
} elseif (is_array($argument)) {
foreach ($argument as $key => $value) {
if (is_object($value)) {
$argument[$key] = $this->getClassName($value);
}
}
} elseif (is_object($argument)) {
if ($argument instanceof FormattedOutput) {
$argument = $argument->getOutput();
} elseif (method_exists($argument, '__toString')) {
$argument = (string)$argument;
} elseif (get_class($argument) == 'Facebook\WebDriver\WebDriverBy') {
$argument = Locator::humanReadableString($argument);
} else {
$argument = $this->getClassName($argument);
}
}
$arg_str = json_encode($argument, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$arg_str = str_replace('\"', '"', $arg_str);
return $arg_str;
}
protected function getClassName($argument)
{
if ($argument instanceof \Closure) {
return 'Closure';
} elseif ((isset($argument->__mocked))) {
return $this->formatClassName($argument->__mocked);
}
return $this->formatClassName(get_class($argument));
}
protected function formatClassName($classname)
{
return trim($classname, "\\");
}
public function getPhpCode($maxLength)
{
$result = "\${$this->prefix}->" . $this->getAction() . '(';
$maxLength = $maxLength - mb_strlen($result, 'utf-8') - 1;
$result .= $this->getHumanizedArguments($maxLength) .')';
return $result;
}
/**
* @return MetaStep
*/
public function getMetaStep()
{
return $this->metaStep;
}
public function __toString()
{
$humanizedAction = $this->humanize($this->getAction());
return $humanizedAction . ' ' . $this->getHumanizedArguments();
}
public function toString($maxLength)
{
$humanizedAction = $this->humanize($this->getAction());
$maxLength = $maxLength - mb_strlen($humanizedAction, 'utf-8') - 1;
return $humanizedAction . ' ' . $this->getHumanizedArguments($maxLength);
}
public function getHtml($highlightColor = '#732E81')
{
if (empty($this->arguments)) {
return sprintf('%s %s', ucfirst($this->prefix), $this->humanize($this->getAction()));
}
return sprintf('%s %s <span style="color: %s">%s</span>', ucfirst($this->prefix), htmlspecialchars($this->humanize($this->getAction())), $highlightColor, htmlspecialchars($this->getHumanizedArguments()));
}
public function getHumanizedActionWithoutArguments()
{
return $this->humanize($this->getAction());
}
public function getHumanizedArguments($maxLength = 200)
{
return $this->getArgumentsAsString($maxLength);
}
protected function clean($text)
{
return str_replace('\/', '', $text);
}
protected function humanize($text)
{
$text = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1 \\2', $text);
$text = preg_replace('/([a-z\d])([A-Z])/', '\\1 \\2', $text);
$text = preg_replace('~\bdont\b~', 'don\'t', $text);
return mb_strtolower($text, 'UTF-8');
}
public function run(ModuleContainer $container = null)
{
$this->executed = true;
if (!$container) {
return null;
}
$activeModule = $container->moduleForAction($this->action);
if (!is_callable([$activeModule, $this->action])) {
throw new \RuntimeException("Action '{$this->action}' can't be called");
}
try {
$res = call_user_func_array([$activeModule, $this->action], $this->arguments);
} catch (\Exception $e) {
$this->failed = true;
if ($this->getMetaStep()) {
$this->getMetaStep()->setFailed(true);
}
throw $e;
}
return $res;
}
/**
* If steps are combined into one method they can be reproduced as meta-step.
* We are using stack trace to analyze if steps were called from test, if not - they were called from meta-step.
*
* @param $step
* @param $stack
*/
protected function addMetaStep($step, $stack)
{
if (($this->isTestFile($this->file)) || ($step['class'] == 'Codeception\Scenario')) {
return;
}
$i = count($stack) - self::STACK_POSITION - 1;
// get into test file and retrieve its actual call
while (isset($stack[$i])) {
$step = $stack[$i];
$i--;
if (!isset($step['file']) or !isset($step['function']) or !isset($step['class'])) {
continue;
}
if (!$this->isTestFile($step['file'])) {
continue;
}
// in case arguments were passed by reference, copy args array to ensure dereference. array_values() does not dereference values
$this->metaStep = new Step\Meta($step['function'], array_map(function ($i) {
return $i;
}, array_values($step['args'])));
$this->metaStep->setTraceInfo($step['file'], $step['line']);
// pageobjects or other classes should not be included with "I"
if (!in_array('Codeception\Actor', class_parents($step['class']))) {
$this->metaStep->setPrefix($step['class'] . ':');
}
return;
}
}
/**
* @param MetaStep $metaStep
*/
public function setMetaStep($metaStep)
{
$this->metaStep = $metaStep;
}
/**
* @return string
*/
public function getPrefix()
{
return $this->prefix . ' ';
}
}