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