1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
4
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
5
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Behat context class. |
10
|
|
|
*/ |
11
|
|
|
class FeatureContext implements SnippetAcceptingContext |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $phpBin; |
17
|
|
|
/** |
18
|
|
|
* @var Process |
19
|
|
|
*/ |
20
|
|
|
private $process; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Prepares test folders in the temporary directory. |
24
|
|
|
* |
25
|
|
|
* @BeforeScenario |
26
|
|
|
*/ |
27
|
|
|
public function prepareProcess() |
28
|
|
|
{ |
29
|
|
|
$phpFinder = new PhpExecutableFinder(); |
30
|
|
|
if (false === $php = $phpFinder->find()) { |
31
|
|
|
throw new \RuntimeException('Unable to find the PHP executable.'); |
32
|
|
|
} |
33
|
|
|
$this->phpBin = $php; |
34
|
|
|
$this->process = new Process(null); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Runs behat command with provided parameters |
39
|
|
|
* |
40
|
|
|
* @When /^I run "behat(?: ((?:\"|[^"])*))?"$/ |
41
|
|
|
* |
42
|
|
|
* @param string $argumentsString |
43
|
|
|
*/ |
44
|
|
|
public function iRunBehat($argumentsString = '') |
45
|
|
|
{ |
46
|
|
|
$argumentsString = strtr($argumentsString, array('\'' => '"')); |
47
|
|
|
|
48
|
|
|
$this->process->setWorkingDirectory(__DIR__ . '/../../testapp'); |
49
|
|
|
$this->process->setCommandLine( |
50
|
|
|
sprintf( |
51
|
|
|
'%s %s %s %s', |
52
|
|
|
$this->phpBin, |
53
|
|
|
escapeshellarg(BEHAT_BIN_PATH), |
54
|
|
|
$argumentsString, |
55
|
|
|
strtr('--format-settings=\'{"timer": false}\'', array('\'' => '"', '"' => '\"')) |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
$this->process->start(); |
59
|
|
|
$this->process->wait(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks whether previously runned command passes|failes with provided output. |
64
|
|
|
* |
65
|
|
|
* @Then /^it should (fail|pass) with:$/ |
66
|
|
|
* |
67
|
|
|
* @param string $success "fail" or "pass" |
68
|
|
|
* @param PyStringNode $text PyString text instance |
69
|
|
|
*/ |
70
|
|
|
public function itShouldPassWith($success, PyStringNode $text) |
71
|
|
|
{ |
72
|
|
|
$this->itShouldFail($success); |
73
|
|
|
$this->theOutputShouldContain($text); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks whether last command output contains provided string. |
78
|
|
|
* |
79
|
|
|
* @Then the output should contain: |
80
|
|
|
* |
81
|
|
|
* @param PyStringNode $text PyString text instance |
82
|
|
|
*/ |
83
|
|
|
public function theOutputShouldContain(PyStringNode $text) |
84
|
|
|
{ |
85
|
|
|
PHPUnit_Framework_Assert::assertContains($this->getExpectedOutput($text), $this->getOutput()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function getExpectedOutput(PyStringNode $expectedText) |
89
|
|
|
{ |
90
|
|
|
$text = strtr($expectedText, array('\'\'\'' => '"""')); |
91
|
|
|
|
92
|
|
|
// windows path fix |
93
|
|
|
if ('/' !== DIRECTORY_SEPARATOR) { |
94
|
|
|
$text = preg_replace_callback( |
95
|
|
|
'/ features\/[^\n ]+/', function ($matches) { |
96
|
|
|
return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); |
97
|
|
|
}, $text |
98
|
|
|
); |
99
|
|
|
$text = preg_replace_callback( |
100
|
|
|
'/\<span class\="path"\>features\/[^\<]+/', function ($matches) { |
101
|
|
|
return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); |
102
|
|
|
}, $text |
103
|
|
|
); |
104
|
|
|
$text = preg_replace_callback( |
105
|
|
|
'/\+[fd] [^ ]+/', function ($matches) { |
106
|
|
|
return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); |
107
|
|
|
}, $text |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $text; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks whether previously runned command failed|passed. |
116
|
|
|
* |
117
|
|
|
* @Then /^it should (fail|pass)$/ |
118
|
|
|
* |
119
|
|
|
* @param string $success "fail" or "pass" |
120
|
|
|
*/ |
121
|
|
|
public function itShouldFail($success) |
122
|
|
|
{ |
123
|
|
|
if ('fail' === $success) { |
124
|
|
View Code Duplication |
if (0 === $this->getExitCode()) { |
|
|
|
|
125
|
|
|
echo 'Actual output:' . PHP_EOL . PHP_EOL . $this->getOutput(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
PHPUnit_Framework_Assert::assertNotEquals(0, $this->getExitCode()); |
129
|
|
|
} else { |
130
|
|
View Code Duplication |
if (0 !== $this->getExitCode()) { |
|
|
|
|
131
|
|
|
echo 'Actual output:' . PHP_EOL . PHP_EOL . $this->getOutput(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
PHPUnit_Framework_Assert::assertEquals(0, $this->getExitCode()); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function getExitCode() |
139
|
|
|
{ |
140
|
|
|
return $this->process->getExitCode(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function getOutput() |
144
|
|
|
{ |
145
|
|
|
$output = $this->process->getErrorOutput() . $this->process->getOutput(); |
146
|
|
|
|
147
|
|
|
// Normalize the line endings in the output |
148
|
|
|
if ("\n" !== PHP_EOL) { |
149
|
|
|
$output = str_replace(PHP_EOL, "\n", $output); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return trim(preg_replace("/ +$/m", '', $output)); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.