CommandContextTrait::iExecuteTheCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
/**
3
 * This file is part of the phpspec-behavior package.
4
 * (c) 2017 Timo Michna <timomichna/yahoo.de>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace features\Behavior\Context\Console;
11
12
use features\Behavior\Console as ConsoleBehavior;
13
14
use Hamcrest\MatcherAssert as Matcher;
15
16
trait CommandContextTrait
17
{
18
    use ConsoleBehavior\CommandTestTrait,
19
        PromptContextTrait;
20
21
    /**
22
     * @Transform :command
23
     *
24
     * @param $command
25
     * @return string
26
     */
27
    public function castCommand($command)
28
    {
29
        return trim((string)$command);
30
    }
31
32
    /**
33
     * @Given command :command exists in namespace :namespace
34
     *
35
     * @param string $command
36
     * @param null|string $namespace
37
     */
38
    public function commandExistsInNamespace(string $command, string $namespace)
39
    {
40
        $this->commandExists(
41
            $this->renderCommandName(
42
                $command,
43
                $namespace
44
            )
45
        );
46
    }
47
48
49
    /**
50
     * @Given command exists :command
51
     *
52
     * @param string $commandName
53
     */
54
    public function commandExists(string $commandName)
55
    {
56
        Matcher::assertThat(
57
            sprintf(
58
                "Command '%s' should exist",
59
                (string)$commandName
60
            ),
61
            $this->getApplication()
62
                ->has(trim((string)$commandName))
63
        );
64
    }
65
66
67
    /**
68
     * @When /^I run "([^"]*)" command$/
69
     *
70
     * @param string $name
71
     */
72
    public function iRunCommand(string $name)
73
    {
74
        $command = $this->getApplication()->find($name);
75
        $this->getCommandTester()
76
            ->execute([
77
                'command' => $command->getName()
78
            ]);
79
    }
80
81
    /**
82
     * @Given I provide (the) argument :name with value :value
83
     * @Given I type argument :name with value :value
84
     *
85
     * @param string $name
86
     * @param string $value
87
     */
88
    public function iProvideArgumentWithValue(string $name, string $value)
89
    {
90
        $this->addArgument($name, $value);
91
    }
92
93
    /**
94
     * @Given I provide no Arguments
95
     */
96
    public function iProvideNoArguments()
97
    {
98
        $this->setArgumentString('');
99
    }
100
101
    /**
102
     * @When I execute the command
103
     */
104
    public function iExecuteTheCommand()
105
    {
106
        try {
107
            $this->lastExitCode = $this->getCommandTester()
108
                ->execute(
109
                    $this->getArguments()
110
                );
111
        } catch (\Throwable $throwable) {
0 ignored issues
show
Bug introduced by
The class Throwable 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...
112
            $this->lastException = $throwable;
113
            $this->lastExitCode = 1;
114
        }
115
    }
116
117
    /**
118
     * @Then I get no error
119
     */
120
    public function iGetNoError()
121
    {
122
        $message = isset($this->lastException)
123
            ? $this->lastException->getMessage()
124
            : '';
125
126
        Matcher::assertThat(
127
            sprintf(
128
                "No error should have been thrown, but got : %s",
129
                $message
130
            ),
131
            !isset($this->lastException)
132
        );
133
    }
134
135
    /**
136
     * @Then I get an error
137
     */
138
    public function iGetAnError()
139
    {
140
        echo $this->getCommandTester()->getDisplay();
141
        Matcher::assertThat(
142
            isset($this->lastException)
143
        );
144
    }
145
146
    /**
147
     * @Then I get an error message
148
     */
149
    public function iGetAnErrorMessage()
150
    {
151
        echo $this->getCommandTester()->getDisplay();
152
        Matcher::assertThat(
153
            isset($this->lastException)
154
        );
155
    }
156
157
    /**
158
     * @Then I get no message
159
     */
160
    public function iGetNoMessage()
161
    {
162
        Matcher::assertThat(
163
            trim($this->getCommandTester()->getDisplay()) === ''
164
        );
165
    }
166
167
    /**
168
     * @Given I use option :optionName
169
     *
170
     * @param string $optionName
171
     */
172
    public function iUseOption(string $optionName)
173
    {
174
        $this->addArgument('--' . $optionName, 1);
175
    }
176
}
177
178