1
|
|
|
<?php |
2
|
|
|
namespace Ciandt\Behat\PlaceholdersExtension\Tester; |
3
|
|
|
|
4
|
|
|
use Behat\Gherkin\Node\FeatureNode; |
5
|
|
|
use Behat\Gherkin\Node\OutlineNode; |
6
|
|
|
use Behat\Gherkin\Node\ScenarioInterface; |
7
|
|
|
use Behat\Gherkin\Node\ScenarioNode; |
8
|
|
|
use Behat\Testwork\Environment\Environment; |
9
|
|
|
use Behat\Testwork\Tester\Result\TestResult; |
10
|
|
|
use Behat\Testwork\Tester\Setup\SuccessfulSetup; |
11
|
|
|
use Behat\Testwork\Tester\Setup\SuccessfulTeardown; |
12
|
|
|
use Behat\Testwork\Tester\SpecificationTester; |
13
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Config\PlaceholdersRepository; |
14
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Utils\PlaceholderUtils; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tester executing feature tests in the runtime. |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
final class ScenarioBranchingFeatureTester implements SpecificationTester |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SpecificationTester |
25
|
|
|
*/ |
26
|
|
|
private $baseTester; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $variantTags; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $configsRepo; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initializes tester. |
40
|
|
|
* |
41
|
|
|
* @param SpecificationTester $baseTester |
42
|
|
|
*/ |
43
|
|
|
public function __construct(SpecificationTester $baseTester, PlaceholdersRepository $configsRepo) |
44
|
|
|
{ |
45
|
|
|
$this->baseTester = $baseTester; |
46
|
|
|
$this->configsRepo = $configsRepo; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function setUp(Environment $env, $spec, $skip) |
53
|
|
|
{ |
54
|
|
|
return new SuccessfulSetup(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function test(Environment $env, $feature, $skip = false) |
61
|
|
|
{ |
62
|
|
|
$tester = $this->baseTester; |
63
|
|
|
$reconstructedFeature = $this->preprocessFeature($feature); |
64
|
|
|
return $tester->test($env, $reconstructedFeature, $skip); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function preprocessFeature(FeatureNode $feature) |
68
|
|
|
{ |
69
|
|
|
$scenarios = array(); |
70
|
|
|
|
71
|
|
|
foreach ($feature->getScenarios() as $scenario) { |
72
|
|
|
$scenarios = array_merge($scenarios,$this->splitScenarioPerVariants($scenario,$feature)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return new FeatureNode( |
76
|
|
|
$feature->getTitle(), |
77
|
|
|
$feature->getDescription(), |
78
|
|
|
$feature->getTags(), |
79
|
|
|
$feature->getBackground(), |
80
|
|
|
$scenarios, |
81
|
|
|
$feature->getKeyword(), |
82
|
|
|
$feature->getLanguage(), |
83
|
|
|
$feature->getFile(), |
84
|
|
|
$feature->getLine() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function splitScenarioPerVariants(ScenarioInterface $scenarioLike, FeatureNode $feature){ |
89
|
|
|
$scenarioTags = $scenarioLike->getTags(); |
90
|
|
|
$featureTags = $feature->getTags(); |
91
|
|
|
$tags = array_merge($scenarioTags,$featureTags); |
92
|
|
|
|
93
|
|
|
$variants = PlaceholderUtils::filterVariantTags($tags,false); |
94
|
|
|
|
95
|
|
|
if (count($variants) <= 1) { |
96
|
|
|
return array($scenarioLike); |
97
|
|
|
} else { |
98
|
|
|
return $this->forkScenario($scenarioLike, $variants); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function forkScenario(ScenarioInterface $scenarioLike, $variants) |
104
|
|
|
{ |
105
|
|
|
$scenarios = array(); |
106
|
|
|
$nonVariantTags = PlaceholderUtils::filterVariantTags($scenarioLike->getTags(),true); |
107
|
|
|
foreach ($variants as $variant) { |
108
|
|
|
$tags = array_merge($nonVariantTags, array($variant)); |
109
|
|
|
if ($scenarioLike instanceof ScenarioNode){ |
110
|
|
|
$scenarios[] = new ScenarioNode( |
111
|
|
|
$scenarioLike->getTitle(), |
112
|
|
|
$tags, |
113
|
|
|
$scenarioLike->getSteps(), |
114
|
|
|
$scenarioLike->getKeyword(), |
115
|
|
|
$scenarioLike->getLine() |
116
|
|
|
); |
117
|
|
|
} elseif ($scenarioLike instanceof OutlineNode){ |
118
|
|
|
$scenarios[] = new OutlineNode( |
119
|
|
|
$scenarioLike->getTitle(), |
120
|
|
|
$tags, |
121
|
|
|
$scenarioLike->getSteps(), |
122
|
|
|
$scenarioLike->getExampleTable(), |
123
|
|
|
$scenarioLike->getKeyword(), |
124
|
|
|
$scenarioLike->getLine() |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return $scenarios; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function tearDown(Environment $env, $spec, $skip, TestResult $result) |
135
|
|
|
{ |
136
|
|
|
return new SuccessfulTeardown(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.