|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ciandt\Behat\PlaceholdersExtension\Tester; |
|
3
|
|
|
|
|
4
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Config\PlaceholdersRepository; |
|
5
|
|
|
use Behat\Behat\Tester\Result\StepResult; |
|
6
|
|
|
use Behat\Behat\Tester\StepTester; |
|
7
|
|
|
use Behat\Gherkin\Node\FeatureNode; |
|
8
|
|
|
use Behat\Gherkin\Node\StepNode; |
|
9
|
|
|
use Behat\Testwork\Environment\Environment; |
|
10
|
|
|
use Behat\Testwork\Tester\Setup\SuccessfulSetup; |
|
11
|
|
|
use Behat\Testwork\Tester\Setup\SuccessfulTeardown; |
|
12
|
|
|
use Behat\Behat\Tester\ServiceContainer\TesterExtension; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Tester executing step tests in the runtime. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class PlaceholdersReplacer implements StepTester |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var StepTester |
|
23
|
|
|
*/ |
|
24
|
|
|
private $baseTester; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $variantTags; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $variant; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $placeholdersRepository; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private $placeholders = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $configPath; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $configSection; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $environment; |
|
60
|
|
|
|
|
61
|
|
|
public function __construct(StepTester $baseTester, $variantTags, PlaceholdersRepository $placeholdersRepository) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->baseTester = $baseTester; |
|
64
|
|
|
$this->placeholdersRepository = $placeholdersRepository; |
|
|
|
|
|
|
65
|
|
|
$this->variantTags = $variantTags; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
* @todo use the tag to get correct section, not always the default one |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setUp(Environment $env, FeatureNode $feature, StepNode $step, $skip) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->environment = $this->placeholdersRepository->getEnvironment(); |
|
|
|
|
|
|
75
|
|
|
if ($this->placeholdersRepository->hasTag($step->configTag)) { |
|
|
|
|
|
|
76
|
|
|
$this->configSection = 'default'; |
|
77
|
|
|
$this->placeholders = $this->placeholdersRepository-> |
|
|
|
|
|
|
78
|
|
|
getConfigSection($step->configTag, $this->configSection)['placeholders']; |
|
|
|
|
|
|
79
|
|
|
$this->configPath = $this->placeholdersRepository->getFilePath($step->configTag); |
|
|
|
|
|
|
80
|
|
|
if ($step->variant) { |
|
81
|
|
|
$this->variant = $step->variant; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return new SuccessfulSetup(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function test(Environment $env, FeatureNode $feature, StepNode $step, $skip = false) |
|
92
|
|
|
{ |
|
93
|
|
|
$tester = $this->baseTester; |
|
94
|
|
|
if ($this->placeholders) { |
|
|
|
|
|
|
95
|
|
|
$result = $tester->test($env, $feature, $this->reconstructStep($step), $skip); |
|
96
|
|
|
return $result; |
|
97
|
|
|
} else { |
|
98
|
|
|
return $tester->test($env, $feature, $step, $skip); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function reconstructStep(StepNode $step) |
|
103
|
|
|
{ |
|
104
|
|
|
//@todo replace placeholders on arguments (tablenode) |
|
105
|
|
|
$arguments = $step->getArguments(); |
|
106
|
|
|
$text = $this->replacePlaceholders($step->getText(), $step->variant, $this->environment); |
|
|
|
|
|
|
107
|
|
|
return new StepNode( |
|
108
|
|
|
$step->getKeyword(), |
|
109
|
|
|
$text, |
|
110
|
|
|
$arguments, |
|
111
|
|
|
$step->getLine(), |
|
112
|
|
|
$step->getKeywordType() |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function replacePlaceholders($string, $var, $env) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
preg_match_all('/\${(?P<key>[^}]+)}/i', $string, $placeholders, PREG_SET_ORDER); |
|
119
|
|
|
foreach ($placeholders as $placeholder) { |
|
|
|
|
|
|
120
|
|
|
$key = $placeholder['key']; |
|
121
|
|
|
$value = $this->getReplacement($key); |
|
122
|
|
|
$string = str_replace('${' . $key . '}', $value, $string); |
|
123
|
|
|
} |
|
124
|
|
|
return $string; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function getReplacement($placeholderKey) |
|
128
|
|
|
{ |
|
129
|
|
|
|
|
130
|
|
|
$values = $this->placeholders; |
|
131
|
|
|
$configPath = $this->configPath; |
|
132
|
|
|
$section = $this->configSection; |
|
133
|
|
|
$variant = $this->variant; |
|
134
|
|
|
$environment = $this->environment; |
|
135
|
|
|
|
|
136
|
|
|
$keys = array('$' . $variant, '$' . $environment, $placeholderKey); |
|
137
|
|
|
$treePosition = "$configPath>$section>placeholders"; |
|
138
|
|
|
|
|
139
|
|
|
return $this->recursivePlaceholderSearch($keys, $values, $treePosition); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function recursivePlaceholderSearch($keys, $values, $treePosition) |
|
143
|
|
|
{ |
|
144
|
|
|
if (empty($keys) || !is_array($values)) { |
|
145
|
|
|
return $values; |
|
146
|
|
|
} |
|
147
|
|
|
$key = array_pop($keys); |
|
148
|
|
|
if (key_exists($key, $values)) { |
|
149
|
|
|
return $this->recursivePlaceholderSearch($keys, $values[$key], "$treePosition>$key"); |
|
150
|
|
|
} elseif (key_exists('$default', $values)) { |
|
151
|
|
|
return $this->recursivePlaceholderSearch($keys, $values['$default'], $treePosition . '>$default'); |
|
152
|
|
|
} else { |
|
153
|
|
|
throw new \Exception("no placeholder is defined on $treePosition>$key"); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
|
|
public function tearDown(Environment $env, FeatureNode $feature, StepNode $step, $skip, StepResult $result) |
|
161
|
|
|
{ |
|
162
|
|
|
return new SuccessfulTeardown(); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..