/
Step.php
127 lines (106 loc) · 3.44 KB
/
Step.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
<?php
namespace Codeception;
abstract class Step
{
/**
* @var string
*/
protected $action;
/**
* @var array
*/
protected $arguments;
/**
* Constructor.
*
* @param array $arguments
*/
public function __construct(array $arguments)
{
$this->action = array_shift($arguments);
$this->arguments = $arguments;
}
/**
* Returns this step's action.
*
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Returns this step's arguments.
*
* @param boolean $asString
* @return array|string
*/
public function getArguments($asString = FALSE)
{
if (!$asString) {
return $this->arguments;
} else {
$arguments = $this->arguments;
foreach ($arguments as $k => $argument) {
if (!is_string($argument) and is_callable($argument, true)) {
$arguments[$k] = 'lambda function';
continue;
}
if (is_object($argument)) {
if (method_exists($argument, '__toString')) {
$arguments[$k] = $argument->__toString();
} elseif (isset($argument->__mocked)) {
$arguments[$k] = $this->formatClassName($argument->__mocked);
} else {
$arguments[$k] = $this->formatClassName(get_class($argument));
}
continue;
}
// if (settype($argument, 'string') === false) throw new \InvalidArgumentException('Argument can\'t be converted to string or serialized');
}
switch (count($arguments)) {
case 0:
return '';
case 1:
return '"' . $arguments[0] . '"';
default:
return stripcslashes(json_encode($arguments));
}
}
}
protected function formatClassName($classname)
{
return 'instance of '.$classname;
}
abstract public function getName();
public function __toString()
{
return "I " . $this->getHumanizedAction();
}
public function getHumanizedAction()
{
return $this->humanize($this->getAction()). ' ' . $this->getHumanizedArguments();
}
public function getHtmlAction() {
$args = $this->getHumanizedArguments();
$args = preg_replace('~\$(.*?)\s~','$<span style="color: #3C3C89; font-weight: bold;">$1</span>', $args);
return $this->humanize($this->getAction()). ' <span style="color: #732E81;">'.$args.'</span>';
}
public function getHumanizedActionWithoutArguments() {
return $this->humanize($this->getAction());
}
public function getHumanizedArguments() {
return $this->clean($this->getArguments(true));
}
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 strtolower($text);
}
}