Repository navigation
Expand file tree
/
Copy pathParser.php
More file actions
153 lines (134 loc) · 4.37 KB
/
Copy pathParser.php
File metadata and controls
153 lines (134 loc) · 4.37 KB
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
<?php
namespace Codeception\Lib;
use Codeception\Scenario;
use Codeception\Step;
class Parser {
protected $scenario;
protected $code;
public function __construct(Scenario $scenario)
{
$this->scenario = $scenario;
}
public function prepareToRun($code)
{
$this->parseFeature($code);
$this->parseScenarioOptions($code);
}
public function parseFeature($code)
{
$matches = array();
$code = $this->stripComments($code);
$res = preg_match("~\\\$I->wantTo\\(['\"](.*?)['\"]\\);~", $code, $matches);
if ($res) {
$this->scenario->setFeature($matches[1]);
return;
}
$res = preg_match("~\\\$I->wantToTest\\(['\"](.*?)['\"]\\);~", $code, $matches);
if ($res) {
$this->scenario->setFeature("test ".$matches[1]);
return;
}
}
public function parseScenarioOptions($code, $var = 'scenario')
{
$matches = array();
$code = $this->stripComments($code);
$res = preg_match_all("~\\\$$var->.*?;~", $code, $matches);
if (!$res or !$var) {
return;
}
$$var = $this->scenario;
foreach ($matches[0] as $line) {
eval($line);
}
}
public function parseSteps($code)
{
// parse per line
$friends = [];
$lines = explode("\n", $code);
$isFriend = false;
foreach ($lines as $line) {
// friends
if (preg_match("~\\\$I->haveFriend\((.*?)\);~", $line, $matches)) {
$friends[] = trim($matches[1],'\'"');
}
// friend's section start
if (preg_match("~\\\$(.*?)->does\(~", $line, $matches)) {
if (!in_array($friend = $matches[1], $friends)) {
continue;
}
$isFriend = true;
$this->addCommentStep("\n----- $friend does -----");
continue;
}
// actions
if (preg_match("~\\\$I->(.*)\((.*?)\);~", $line, $matches)) {
$this->addStep($matches);
}
// friend's section ends
if ($isFriend and strpos($line, '}') !== false) {
$this->addCommentStep("-------- back to me\n");
$isFriend = false;
}
}
}
protected function addStep($matches)
{
list($m, $action, $params) = $matches;
if (in_array($action, array('wantTo','wantToTest'))) {
return;
}
$this->scenario->addStep(new Step\Action($action, explode(',', $params)));
}
protected function addCommentStep($comment)
{
$this->scenario->addStep(new \Codeception\Step\Comment($comment,array()));
}
public static function getClassesFromFile($file)
{
include_once $file;
$sourceCode = file_get_contents($file);
$classes = array();
$tokens = token_get_all($sourceCode);
$namespace = '';
for ($i = 0; $i < count($tokens); $i++) {
if ($tokens[$i][0] === T_NAMESPACE) {
$namespace = '';
for ($j = $i + 1; $j < count($tokens); $j++) {
if ($tokens[$j][0] === T_STRING) {
$namespace .= $tokens[$j][1] . '\\';
} else {
if ($tokens[$j] === '{' || $tokens[$j] === ';') {
break;
}
}
}
}
if ($tokens[$i][0] === T_CLASS) {
if (!isset($tokens[$i-2])) {
$classes[] = $namespace . $tokens[$i + 2][1];
continue;
}
if ($tokens[$i-1][0] === T_WHITESPACE and $tokens[$i-2][0] === T_DOUBLE_COLON) {
continue;
}
if ($tokens[$i-1][0] === T_DOUBLE_COLON) {
continue;
}
$classes[] = $namespace . $tokens[$i + 2][1];
}
}
return $classes;
}
/**
* @param $code
* @return mixed
*/
protected function stripComments($code)
{
$code = preg_replace('~\/\/.*?$~m', '', $code); // remove inline comments
$code = preg_replace('~\/*\*.*?\*\/~ms', '', $code);
return $code; // remove block comment
}
}