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

CommandContext   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
c 3
b 1
f 1
lcom 1
cbo 4
dl 0
loc 147
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 10 2
A iRunCommand() 0 14 2
A commandExitCodeShouldBe() 0 12 2
A commandShouldThrowException() 0 21 4
A commandOutputShouldBeLike() 0 15 2
A getRawCommandOutput() 0 9 2
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);
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...
57
        $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...
58
        $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...
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;
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...
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) {
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...
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
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...
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) {
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...
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();
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...
130
        $pyStringNodeContent = $string->getRaw();
131
132
        try {
133
            \PHPUnit_Framework_Assert::assertContains($pyStringNodeContent, $commandOutput);
134
        } 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...
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