1
|
|
|
<?php |
2
|
|
|
namespace Ciandt\Behat\PlaceholdersExtension\Tester; |
3
|
|
|
|
4
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Config\ConfigsRepository; |
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 $configsRepo; |
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, ConfigsRepository $configsRepo) |
62
|
|
|
{ |
63
|
|
|
$this->baseTester = $baseTester; |
64
|
|
|
$this->configsRepo = $configsRepo; |
|
|
|
|
65
|
|
|
$this->variantTags = $variantTags; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function setEnvironment($environment) |
69
|
|
|
{ |
70
|
|
|
$this->environment = $environment; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
* @todo use the tag to get correct section, not always the default one |
76
|
|
|
*/ |
77
|
|
|
public function setUp(Environment $env, FeatureNode $feature, StepNode $step, $skip) |
78
|
|
|
{ |
79
|
|
|
if ($this->configsRepo->hasTag($step->configTag)) { |
|
|
|
|
80
|
|
|
$this->configSection = 'default'; |
81
|
|
|
$this->placeholders = $this->configsRepo->getConfigSection($step->configTag, $this->configSection)['placeholders']; |
|
|
|
|
82
|
|
|
$this->configPath = $this->configsRepo->getFilePath($step->configTag); |
|
|
|
|
83
|
|
|
if ($step->variant) { |
84
|
|
|
$this->variant = $step->variant; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return new SuccessfulSetup(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function test(Environment $env, FeatureNode $feature, StepNode $step, $skip = false) |
95
|
|
|
{ |
96
|
|
|
$tester = $this->baseTester; |
97
|
|
|
if ($this->placeholders) { |
|
|
|
|
98
|
|
|
$result = $tester->test($env, $feature, $this->reconstructStep($step), $skip); |
99
|
|
|
return $result; |
100
|
|
|
} else { |
101
|
|
|
return $tester->test($env, $feature, $step, $skip); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function reconstructStep(StepNode $step) |
106
|
|
|
{ |
107
|
|
|
//@todo replace placeholders on arguments (tablenode) |
108
|
|
|
$arguments = $step->getArguments(); |
109
|
|
|
$text = $this->replacePlaceholders($step->getText(), $step->variant, $this->environment); |
|
|
|
|
110
|
|
|
return new StepNode( |
111
|
|
|
$step->getKeyword(), $text, $arguments, $step->getLine(), $step->getKeywordType()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function replacePlaceholders($string, $var, $env) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
preg_match_all('/\${(?P<key>[^}]+)}/i', $string, $placeholders, PREG_SET_ORDER); |
117
|
|
|
foreach ($placeholders as $placeholder) { |
|
|
|
|
118
|
|
|
$key = $placeholder['key']; |
119
|
|
|
$value = $this->getReplacement($key); |
120
|
|
|
$string = str_replace('${' . $key . '}', $value, $string); |
121
|
|
|
} |
122
|
|
|
return $string; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getReplacement($placeholderKey) |
126
|
|
|
{ |
127
|
|
|
|
128
|
|
|
$values = $this->placeholders; |
129
|
|
|
$configPath = $this->configPath; |
130
|
|
|
$section = $this->configSection; |
131
|
|
|
$variant = $this->variant; |
132
|
|
|
$environment = $this->environment; |
133
|
|
|
|
134
|
|
|
$keys = array('$' . $variant, '$' . $environment, $placeholderKey); |
135
|
|
|
$treePosition = "$configPath>$section>placeholders"; |
136
|
|
|
|
137
|
|
|
return $this->recursivePlaceholderSearch($keys, $values, $treePosition); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function recursivePlaceholderSearch($keys, $values, $treePosition) |
141
|
|
|
{ |
142
|
|
|
if (empty($keys) || !is_array($values)) { |
143
|
|
|
return $values; |
144
|
|
|
} |
145
|
|
|
$key = array_pop($keys); |
146
|
|
|
if (key_exists($key, $values)) { |
147
|
|
|
return $this->recursivePlaceholderSearch($keys, $values[$key], "$treePosition>$key"); |
148
|
|
|
} elseif (key_exists('$default', $values)) { |
149
|
|
|
return $this->recursivePlaceholderSearch($keys, $values['$default'], $treePosition . '>$default'); |
150
|
|
|
} else { |
151
|
|
|
throw new \Exception("no placeholder is defined on $treePosition>[$key or \$default]"); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function tearDown(Environment $env, FeatureNode $feature, StepNode $step, $skip, StepResult $result) |
159
|
|
|
{ |
160
|
|
|
return new SuccessfulTeardown(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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..