Completed
Pull Request — master (#182)
by Vincent
17:17
created

CommandContext::iRunCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4286
cc 2
eloc 10
nc 2
nop 1
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());
0 ignored issues
show
Documentation introduced by
$this->getKernel() is of type object|null, but the function expects a object<Symfony\Component...Kernel\KernelInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
    }
42
43
    /**
44
     * @param string $command
45
     *
46
     * @When /^I run (.*)$/
47
     */
48
    public function iRunCommand($command)
49
    {
50
        $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...
51
        $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...
52
        $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...
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;
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...
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) {
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...
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
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...
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) {
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...
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();
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...
125
        $pyStringNodeContent = $string->getRaw();
126
127
        try {
128
            \PHPUnit_Framework_Assert::assertContains($pyStringNodeContent, $commandOutput);
129
        } 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...
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