1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
6
|
|
|
use Behat\Mink\Exception\ExpectationException; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
8
|
|
|
use Symfony\Component\Console\Input\StringInput; |
9
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
|
12
|
|
|
class CommandContext extends RawMinkContext |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var StreamOutput |
16
|
|
|
*/ |
17
|
|
|
private $output; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
private $exitCode; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Exception |
26
|
|
|
*/ |
27
|
|
|
private $exception; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Application |
31
|
|
|
*/ |
32
|
|
|
private $application; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function initialize(array $config, ContainerInterface $container) |
38
|
|
|
{ |
39
|
|
|
parent::initialize($config, $container); |
40
|
|
|
$this->application = new Application($this->getKernel()); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $command |
45
|
|
|
* |
46
|
|
|
* @When /^I run (.*)$/ |
47
|
|
|
*/ |
48
|
|
|
public function iRunCommand($command) |
49
|
|
|
{ |
50
|
|
|
$inputString = trim($command); |
|
|
|
|
51
|
|
|
$input = new StringInput($inputString); |
|
|
|
|
52
|
|
|
$this->output = new StreamOutput(tmpfile()); |
|
|
|
|
53
|
|
|
$this->exception = null; |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$this->exitCode = $this->application->doRun($input, $this->output); |
57
|
|
|
} catch (\Exception $e) { |
58
|
|
|
$this->exception = $e; |
59
|
|
|
$this->exitCode = -255; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $code |
65
|
|
|
* |
66
|
|
|
* @throws ExpectationException |
67
|
|
|
* |
68
|
|
|
* @Then /^Command should be successfully executed$/ |
69
|
|
|
* @Then /^Command exit code should be (?P<code>\-\d+|\d+)$/ |
70
|
|
|
*/ |
71
|
|
|
public function commandExitCodeShouldBe($code = 0) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
\PHPUnit_Framework_Assert::assertEquals($code, $this->exitCode); |
75
|
|
|
} catch (\PHPUnit_Framework_ExpectationFailedException $e) { |
|
|
|
|
76
|
|
|
throw new ExpectationException( |
77
|
|
|
sprintf('Command exit code "%s" does not match expected "%s"', $this->exitCode, $code), |
78
|
|
|
$this->getSession(), |
79
|
|
|
$e |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param PyStringNode $message |
|
|
|
|
86
|
|
|
* |
87
|
|
|
* @throws ExpectationException |
88
|
|
|
* |
89
|
|
|
* @Then /^Command should throw an exception$/ |
90
|
|
|
* @Then /^Command should throw following exception:?$/ |
91
|
|
|
*/ |
92
|
|
|
public function commandShouldThrowException(PyStringNode $message = null) |
93
|
|
|
{ |
94
|
|
|
if (!$this->exception instanceof \Exception) { |
95
|
|
|
throw new ExpectationException('Command does not throw any exception', $this->getSession()); |
96
|
|
|
} |
97
|
|
|
if (null !== $message) { |
98
|
|
|
try { |
99
|
|
|
\PHPUnit_Framework_Assert::assertSame($message->getRaw(), $this->exception->getMessage()); |
100
|
|
|
} catch (\PHPUnit_Framework_ExpectationFailedException $e) { |
|
|
|
|
101
|
|
|
throw new ExpectationException( |
102
|
|
|
sprintf( |
103
|
|
|
'Command exception message "%s" does not match expected "%s"', |
104
|
|
|
$this->exception->getMessage(), |
105
|
|
|
$message->getRaw() |
106
|
|
|
), |
107
|
|
|
$this->getSession(), |
108
|
|
|
$e |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param PyStringNode $string |
116
|
|
|
* |
117
|
|
|
* @throws \Exception |
118
|
|
|
* @throws ExpectationException |
119
|
|
|
* |
120
|
|
|
* @Then /^Command output should be like:?$/ |
121
|
|
|
*/ |
122
|
|
|
public function commandOutputShouldBeLike(PyStringNode $string) |
123
|
|
|
{ |
124
|
|
|
$commandOutput = $this->getRawCommandOutput(); |
|
|
|
|
125
|
|
|
$pyStringNodeContent = $string->getRaw(); |
126
|
|
|
|
127
|
|
|
try { |
128
|
|
|
\PHPUnit_Framework_Assert::assertContains($pyStringNodeContent, $commandOutput); |
129
|
|
|
} catch (\PHPUnit_Framework_ExpectationFailedException $e) { |
|
|
|
|
130
|
|
|
throw new ExpectationException( |
131
|
|
|
sprintf("Command output is not like it should be\n#########>\n%s\n<#########\n", $commandOutput), |
132
|
|
|
$this->getSession(), |
133
|
|
|
$e |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
* |
141
|
|
|
* @throws \Exception |
142
|
|
|
*/ |
143
|
|
|
private function getRawCommandOutput() |
144
|
|
|
{ |
145
|
|
|
if (!$this->output) { |
146
|
|
|
throw new \Exception('No command output!'); |
147
|
|
|
} |
148
|
|
|
rewind($this->output->getStream()); |
149
|
|
|
|
150
|
|
|
return stream_get_contents($this->output->getStream()); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: