/
Gherkin.php
217 lines (186 loc) · 7.62 KB
/
Gherkin.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
<?php
namespace Codeception\Test\Loader;
use Behat\Gherkin\Filter\RoleFilter;
use Behat\Gherkin\Keywords\ArrayKeywords as GherkinKeywords;
use Behat\Gherkin\Lexer as GherkinLexer;
use Behat\Gherkin\Node\ExampleNode;
use Behat\Gherkin\Node\OutlineNode;
use Behat\Gherkin\Node\ScenarioInterface;
use Behat\Gherkin\Node\ScenarioNode;
use Behat\Gherkin\Parser as GherkinParser;
use Codeception\Configuration;
use Codeception\Exception\ParseException;
use Codeception\Exception\TestParseException;
use Codeception\Test\Gherkin as GherkinFormat;
use Codeception\Util\Annotation;
class Gherkin implements LoaderInterface
{
protected static $defaultSettings = [
'namespace' => '',
'actor' => '',
'gherkin' => [
'contexts' => [
'default' => [],
'tag' => [],
'role' => []
]
]
];
protected $tests = [];
/**
* @var GherkinParser
*/
protected $parser;
protected $settings = [];
protected $steps = [];
public function __construct($settings = [])
{
$this->settings = Configuration::mergeConfigs(self::$defaultSettings, $settings);
if (!class_exists('Behat\Gherkin\Keywords\ArrayKeywords')) {
throw new TestParseException('Feature file can only be parsed with Behat\Gherkin library. Please install `behat/gherkin` with Composer');
}
$gherkin = new \ReflectionClass('Behat\Gherkin\Gherkin');
$gherkinClassPath = dirname($gherkin->getFileName());
$i18n = require $gherkinClassPath . '/../../../i18n.php';
$keywords = new GherkinKeywords($i18n);
$lexer = new GherkinLexer($keywords);
$this->parser = new GherkinParser($lexer);
$this->fetchGherkinSteps();
}
protected function fetchGherkinSteps()
{
$contexts = $this->settings['gherkin']['contexts'];
foreach ($contexts['tag'] as $tag => $tagContexts) {
$this->addSteps($tagContexts, "tag:$tag");
}
foreach ($contexts['role'] as $role => $roleContexts) {
$this->addSteps($roleContexts, "role:$role");
}
if (empty($this->steps) && empty($contexts['default']) && $this->settings['actor']) { // if no context is set, actor to be a context
$actorContext = $this->settings['namespace']
? rtrim($this->settings['namespace'], '\\') . '\\' . rtrim($this->settings['actor'], '\\')
: $this->settings['actor'];
if ($actorContext) {
$contexts['default'][] = $actorContext;
}
}
if (isset($this->settings['gherkin']['contexts']['path']) &&
isset($this->settings['gherkin']['contexts']['namespace_prefix'])) {
$files = glob($this->settings['gherkin']['contexts']['path'] . '/*/*.php');
// Strip off include path
$files = str_replace([$this->settings['gherkin']['contexts']['path'], '.php', '/'], ['', '', '\\'], $files);
// Add namespace prefix
$namespace = $this->settings['gherkin']['contexts']['namespace_prefix'];
$dynamicContexts = array_map(function ($path) use ($namespace) {
return $namespace . $path;
}, $files);
$this->addSteps($dynamicContexts, 'default');
}
$this->addSteps($contexts['default']);
}
protected function addSteps(array $contexts, $group = 'default')
{
if (!isset($this->steps[$group])) {
$this->steps[$group] = [];
}
foreach ($contexts as $context) {
$methods = get_class_methods($context);
if (!$methods) {
continue;
}
foreach ($methods as $method) {
$annotation = Annotation::forMethod($context, $method);
foreach (['Given', 'When', 'Then'] as $type) {
$patterns = $annotation->fetchAll($type);
foreach ($patterns as $pattern) {
if (!$pattern) {
continue;
}
$this->validatePattern($pattern);
$pattern = $this->makePlaceholderPattern($pattern);
$this->steps[$group][$pattern] = [$context, $method];
}
}
}
}
}
public function makePlaceholderPattern($pattern)
{
if (isset($this->settings['describe_steps'])) {
return $pattern;
}
if (strpos($pattern, '/') !== 0) {
$pattern = preg_quote($pattern);
$pattern = preg_replace('~(\w+)\/(\w+)~', '(?:$1|$2)', $pattern); // or
$pattern = preg_replace('~\\\\\((\w)\\\\\)~', '$1?', $pattern); // (s)
$replacePattern = sprintf(
'(?|\"%s\"|%s)',
"((?|[^\"\\\\\\]|\\\\\\.)*?)", // matching escaped string in ""
'[\D]{0,1}([\d\,\.]+)[\D]{0,1}'
); // or matching numbers with optional $ or € chars
// params converting from :param to match 11 and "aaa" and "aaa\"aaa"
$pattern = preg_replace('~"?\\\:(\w+)"?~', $replacePattern, $pattern);
$pattern = "/^$pattern$/u";
// validating this pattern is slow, so we skip it now
}
return $pattern;
}
private function validatePattern($pattern)
{
if (strpos($pattern, '/') !== 0) {
return; // not a user-regex but a string with placeholder
}
if (@preg_match($pattern, ' ') === false) {
throw new ParseException("Loading Gherkin step with regex\n \n$pattern\n \nfailed. This regular expression is invalid.");
}
}
public function loadTests($filename)
{
$featureNode = $this->parser->parse(file_get_contents($filename), $filename);
if (!$featureNode) {
return;
}
foreach ($featureNode->getScenarios() as $scenarioNode) {
/** @var $scenarioNode ScenarioInterface * */
$steps = $this->steps['default']; // load default context
foreach (array_merge($scenarioNode->getTags(), $featureNode->getTags()) as $tag) { // load tag contexts
if (isset($this->steps["tag:$tag"])) {
$steps = array_merge($steps, $this->steps["tag:$tag"]);
}
}
$roles = $this->settings['gherkin']['contexts']['role']; // load role contexts
foreach ($roles as $role => $context) {
$filter = new RoleFilter($role);
if ($filter->isFeatureMatch($featureNode)) {
$steps = array_merge($steps, $this->steps["role:$role"]);
break;
}
}
if ($scenarioNode instanceof OutlineNode) {
foreach ($scenarioNode->getExamples() as $example) {
/** @var $example ExampleNode **/
$params = implode(', ', $example->getTokens());
$exampleNode = new ScenarioNode($scenarioNode->getTitle() . " | $params", $scenarioNode->getTags(), $example->getSteps(), $example->getKeyword(), $example->getLine());
$this->tests[] = new GherkinFormat($featureNode, $exampleNode, $steps);
}
continue;
}
$this->tests[] = new GherkinFormat($featureNode, $scenarioNode, $steps);
}
}
public function getTests()
{
return $this->tests;
}
public function getPattern()
{
return '~\.feature$~';
}
/**
* @return array
*/
public function getSteps()
{
return $this->steps;
}
}