Completed
Pull Request — master (#182)
by Vincent
05:39 queued 01:30
created

CommandContext::commandShouldThrowException()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 9.0534
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Context;
4
5
use Behat\Gherkin\Node\PyStringNode;
6
use Symfony\Component\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
    public function initialize(array $config, ContainerInterface $container)
37
    {
38
        parent::initialize($config, $container);
39
        $this->application = new Application();
40
    }
41
42
    /**
43
     * @param string $command
44
     *
45
     * @When /^I run (.*)$/
46
     */
47
    public function iRunCommand($command)
48
    {
49
        $inputString = trim($command);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
50
        $input = new StringInput($inputString);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51
        $this->output = new StreamOutput(tmpfile());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
        $this->exception = null;
53
54
        try {
55
            $this->exitCode = $this->application->doRun($input, $this->output);
56
        } catch (\Exception $e) {
57
            $this->exception = $e;
58
            $this->exitCode = -255;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
59
        }
60
    }
61
62
    /**
63
     * @param int $code
64
     *
65
     * @throws \Exception
66
     *
67
     * @Then /^command should be successfully executed$/
68
     * @Then /^command exit code should be (?P<code>\-\d+|\d+)$/
69
     */
70
    public function commandExitCodeShouldBe($code = 0)
71
    {
72
        try {
73
            \PHPUnit_Framework_Assert::assertEquals($code, $this->exitCode);
74
        } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_ExpectationFailedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
75
            throw new \Exception(
76
                sprintf('Command exit code "%s" does not match expected "%s"', $this->exitCode, $code),
77
                0,
78
                $e
79
            );
80
        }
81
    }
82
83
    /**
84
     * @param PyStringNode $message
0 ignored issues
show
Documentation introduced by
Should the type for parameter $message not be null|PyStringNode?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
85
     *
86
     * @throws \Exception
87
     *
88
     * @Then /^command should throw an exception$/
89
     * @Then /^command should throw following exception:?$/
90
     */
91
    public function commandShouldThrowException(PyStringNode $message = null)
92
    {
93
        if (!$this->exception instanceof \Exception) {
94
            throw new \Exception('Command does not throw any exception', 0, $this->exception);
95
        }
96
        if (null !== $message) {
97
            try {
98
                \PHPUnit_Framework_Assert::assertSame($message->getRaw(), $this->exception->getMessage());
99
            } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_ExpectationFailedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
100
                throw new \Exception(
101
                    sprintf(
102
                        'Command exception message "%s" does not match expected "%s"',
103
                        $this->exception->getMessage(),
104
                        $message->getRaw()
105
                    ),
106
                    0,
107
                    $e
108
                );
109
            }
110
        }
111
    }
112
113
    /**
114
     * @param PyStringNode $string
115
     *
116
     * @throws \Exception
117
     *
118
     * @Then /^command output should be like:?$/
119
     */
120
    public function commandOutputShouldBeLike(PyStringNode $string)
121
    {
122
        $commandOutput = $this->getRawCommandOutput();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
123
        $pyStringNodeContent = $string->getRaw();
124
125
        try {
126
            \PHPUnit_Framework_Assert::assertContains($pyStringNodeContent, $commandOutput);
127
        } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_ExpectationFailedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
128
            throw new \Exception(
129
                sprintf("Command output is not like it should be\n#########>\n%s\n<#########\n", $commandOutput),
130
                0,
131
                $e
132
            );
133
        }
134
    }
135
136
    /**
137
     * @return string
138
     *
139
     * @throws \Exception
140
     */
141
    private function getRawCommandOutput()
142
    {
143
        if (!$this->output) {
144
            throw new \Exception('No command output!');
145
        }
146
        rewind($this->output->getStream());
147
148
        return stream_get_contents($this->output->getStream());
149
    }
150
}
151