forked from Behat/Behat
/
AnnotatedDefinitionProposal.php
134 lines (116 loc) · 4.13 KB
/
AnnotatedDefinitionProposal.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
<?php
namespace Behat\Behat\Definition\Proposal;
use Behat\Gherkin\Node\StepNode,
Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use Behat\Behat\Context\ContextInterface,
Behat\Behat\Definition\DefinitionSnippet,
Behat\Behat\Util\Transliterator;
/*
* This file is part of the Behat.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Annotated definitions proposal.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class AnnotatedDefinitionProposal implements DefinitionProposalInterface
{
/**
* Proposed method names.
*
* @var array
*/
private static $proposedMethods = array();
/**
* @see Behat\Behat\Definition\Proposal\DefinitionProposalInterface::supports()
*/
public function supports(ContextInterface $context)
{
return true;
}
/**
* @see Behat\Behat\Definition\Proposal\DefinitionProposalInterface::propose()
*/
public function propose(ContextInterface $context, StepNode $step)
{
$contextRefl = new \ReflectionObject($context);
$contextClass = $contextRefl->getName();
$text = $step->getText();
$replacePatterns = array(
'/\'([^\']*)\'/', '/\"([^\"]*)\"/', // Quoted strings
'/(\d+)/', // Numbers
);
$regex = preg_replace('/([\/\[\]\(\)\\\^\$\.\|\?\*\+])/', '\\\\$1', $text);
$regex = preg_replace(
$replacePatterns,
array(
"\'([^\']*)\'", "\"([^\"]*)\"",
"(\\d+)",
),
$regex
);
// Single quotes without matching pair (escape in resulting regex):
$regex = preg_replace('/\'.*(?<!\')/', '\\\\$0', $regex);
preg_match('/' . $regex . '/', $text, $matches);
$count = count($matches) - 1;
$methodName = preg_replace($replacePatterns, '', $text);
$methodName = Transliterator::transliterate($methodName, ' ');
$methodName = preg_replace('/[^a-zA-Z\_\ ]/', '', $methodName);
$methodName = str_replace(' ', '', ucwords($methodName));
if (0 !== strlen($methodName)) {
$methodName[0] = strtolower($methodName[0]);
} else {
$methodName = 'stepDefinition1';
}
// get method number from method name
$methodNumber = 2;
if (preg_match('/(\d+)$/', $methodName, $matches)) {
$methodNumber = intval($matches[1]);
}
// check that proposed method name isn't arelady defined in context
while ($contextRefl->hasMethod($methodName)) {
$methodName = preg_replace('/\d+$/', '', $methodName);
$methodName .= $methodNumber++;
}
// check that proposed method name haven't been proposed earlier
if (isset(self::$proposedMethods[$contextClass])) {
foreach (self::$proposedMethods[$contextClass] as $proposedRegex => $proposedMethod) {
if ($proposedRegex !== $regex) {
while ($proposedMethod === $methodName) {
$methodName = preg_replace('/\d+$/', '', $methodName);
$methodName .= $methodNumber++;
}
}
}
}
self::$proposedMethods[$contextClass][$regex] = $methodName;
$args = array();
for ($i = 0; $i < $count; $i++) {
$args[] = "\$argument" . ($i + 1);
}
foreach ($step->getArguments() as $argument) {
if ($argument instanceof PyStringNode) {
$args[] = "PyStringNode \$string";
} elseif ($argument instanceof TableNode) {
$args[] = "TableNode \$table";
}
}
$description = sprintf(<<<PHP
/**
* @%s /^%s$/
*/
public function %s(%s)
{
throw new PendingException();
}
PHP
, '%s', $regex, $methodName, implode(', ', $args)
);
return new DefinitionSnippet($step, $description);
}
}