1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Noiselabs ZfTestCase Behat Extension. |
4
|
|
|
* |
5
|
|
|
* (c) Vítor Brandão <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Behat\Behat\Context\Context; |
12
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
13
|
|
|
use PHPUnit\Framework\Assert; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class FeatureContext. |
17
|
|
|
* |
18
|
|
|
* Adapted from https://github.com/jakzal/BehatRestExtension/blob/master/features/bootstrap/BehatRunnerContext.php |
19
|
|
|
*/ |
20
|
|
|
class FeatureContext implements Context |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var BehatRunner |
24
|
|
|
*/ |
25
|
|
|
private $behatRunner; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @BeforeScenario |
29
|
|
|
*/ |
30
|
|
|
public function bootstrap() |
31
|
|
|
{ |
32
|
|
|
$this->behatRunner = new BehatRunner(sprintf('%s/%s/', sys_get_temp_dir(), uniqid('ZfTestCaseExtension'))); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @AfterScenario |
37
|
|
|
*/ |
38
|
|
|
public function removeWorkDir() |
39
|
|
|
{ |
40
|
|
|
$this->behatRunner->removeWorkspace(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a file with specified name and context in current workdir. |
45
|
|
|
* |
46
|
|
|
* @Given /^(?:there is )?a file named "([^"]*)" with:$/ |
47
|
|
|
* |
48
|
|
|
* @param string $filename name of the file (relative path) |
49
|
|
|
* @param PyStringNode $content PyString string instance |
50
|
|
|
*/ |
51
|
|
|
public function aFileNamedWith($filename, PyStringNode $content) |
52
|
|
|
{ |
53
|
|
|
$this->behatRunner->addFile($filename, $content->getRaw()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @Given /^a behat configuration:$/ |
58
|
|
|
*/ |
59
|
|
|
public function givenBehatConfiguration(PyStringNode $content) |
60
|
|
|
{ |
61
|
|
|
$this->behatRunner->addFile('behat.yml', $content->getRaw()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @Given /^(?:|an?|the )(?:|context |class |feature |.*)file "(?P<fileName>[^"]*)" contains:$/ |
66
|
|
|
*/ |
67
|
|
|
public function aContextFileNamedWith($fileName, PyStringNode $content) |
68
|
|
|
{ |
69
|
|
|
$this->behatRunner->addFile($fileName, $content->getRaw()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @When /^I run behat$/ |
74
|
|
|
*/ |
75
|
|
|
public function iRunBehat() |
76
|
|
|
{ |
77
|
|
|
$this->behatRunner->run(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @Then /^it should pass$/ |
82
|
|
|
*/ |
83
|
|
|
public function itShouldPass() |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
Assert::assertSame(0, $this->behatRunner->getExitCode(), 'Command terminated with an error'); |
87
|
|
|
Assert::assertStringNotMatchesFormat('PHP Warning:', $this->behatRunner->getFullOutput()); |
88
|
|
|
Assert::assertStringNotMatchesFormat('PHP Notice:', $this->behatRunner->getFullOutput()); |
89
|
|
|
} catch (\Exception $e) { |
90
|
|
|
echo $this->behatRunner->getFullOutput(); |
91
|
|
|
|
92
|
|
|
throw $e; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Then /^it should fail$/ |
98
|
|
|
*/ |
99
|
|
|
public function itShouldFail() |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
|
|
Assert::assertNotSame(0, $this->behatRunner->getExitCode(), 'Command succeeded'); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
echo $this->behatRunner->getFullOutput(); |
105
|
|
|
|
106
|
|
|
throw $e; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @Then /^it should pass with:$/ |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function itShouldPassWith(PyStringNode $expectedOutput) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->itShouldPass(); |
116
|
|
|
|
117
|
|
|
foreach ($expectedOutput->getStrings() as $expectedLine) { |
118
|
|
|
Assert::assertRegExp('/' . preg_quote($expectedLine, '/') . '/sm', $this->behatRunner->getFullOutput()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @Then /^it should fail with:$/ |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function itShouldFailWith(PyStringNode $expectedOutput) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$this->itShouldFail(); |
128
|
|
|
|
129
|
|
|
foreach ($expectedOutput->getStrings() as $expectedLine) { |
130
|
|
|
Assert::assertRegExp('/' . preg_quote($expectedLine, '/') . '/sm', $this->behatRunner->getFullOutput()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $path |
136
|
|
|
*/ |
137
|
|
|
public function givenBehatProject($path) |
138
|
|
|
{ |
139
|
|
|
$this->behatRunner->addDirectory($path); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getFullOutput() |
146
|
|
|
{ |
147
|
|
|
return $this->behatRunner->getFullOutput(); |
148
|
|
|
} |
149
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.