CommandTestTrait::setCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
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\Console;
11
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Tester\CommandTester;
14
15
trait CommandTestTrait
16
{
17
    use ApplicationTestTrait,
18
        CommandDependencyMockTrait;
19
20
    /**
21
     * @var Command
22
     */
23
    protected $command;
24
25
    /** @var  CommandTester */
26
    protected $commandTester;
27
28
    /**
29
     * @var array
30
     */
31
    protected $arguments = [];
32
33
    /**
34
     * @var string
35
     */
36
    protected $argumentString;
37
38
    /**
39
     * @var \Throwable
40
     */
41
    private $lastException;
42
43
    /**
44
     * @var int
45
     */
46
    private $lastExitCode = 0;
47
48
    /**
49
     * @param Command $command
50
     */
51
    public function setCommand(Command $command)
52
    {
53
        $this->command = $command;
54
        $this->setCommandTester(
55
            new CommandTester(
56
                $command
57
            )
58
        );
59
        $this->getApplication()
60
            ->add(
61
                $command
62
            );
63
64
    }
65
66
    /**
67
     * @return Command
68
     */
69
    public function getCommand(): Command
70
    {
71
        if (!isset($this->command)) {
72
            $this->setCommand(
73
                new Command()
74
            );
75
        }
76
77
        return $this->command;
78
    }
79
80
    /**
81
     * @param CommandTester $commandTester
82
     */
83
    protected function setCommandTester(CommandTester $commandTester)
84
    {
85
        $this->commandTester = $commandTester;
86
    }
87
88
    /**
89
     * @return CommandTester
90
     */
91
    public function getCommandTester(): CommandTester
92
    {
93
        return $this->commandTester;
94
    }
95
96
    protected function resetCommand()
97
    {
98
        $this->command = null;
99
        $this->commandTester = null;
100
        $this->arguments = [];
101
        $this->argumentString = null;
102
    }
103
104
    /**
105
     * @param array $arguments
106
     */
107
    public function setArguments(array $arguments)
108
    {
109
        $this->arguments = $arguments;
110
    }
111
112
    /**
113
     * @param string $name
114
     * @param bool|string|int|float $value
115
     */
116
    public function addArgument(string $name, $value)
117
    {
118
        $this->arguments[$name] = $value;
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function getArguments(): array
125
    {
126
        return $this->arguments;
127
    }
128
129
    /**
130
     * @param string $argumentString
131
     */
132
    public function setArgumentString(string $argumentString)
133
    {
134
        $this->argumentString = $argumentString;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getArgumentString(): string
141
    {
142
        return !isset($this->argumentString)
143
            ? $this->argumentString
144
            : $this->argumentString = implode(' ', $this->arguments);
145
    }
146
147
    /**
148
     * @param string $command
149
     * @param null|string $namespace
150
     * @return string
151
     */
152
    protected function renderCommandName(string $command, ?string $namespace)
153
    {
154
        return $namespace === null
155
            ? $command
156
            : sprintf(
157
                '%s:%s',
158
                $namespace,
159
                $command
160
            );
161
    }
162
}
163
164