TestCase::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GuillermoandraeTest\Commands;
4
5
use RuntimeException;
6
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
7
use Symfony\Component\Console\Application;
8
use Symfony\Component\Console\Tester\CommandTester;
9
10
class TestCase extends PHPUnitTestCase
11
{
12
    /**
13
     * @var Application
14
     */
15
    protected $application;
16
17
    protected function setUp()
18
    {
19
        $this->application = new Application();
20
    }
21
22
    /**
23
     * Returns an instance of the command tester using the options provided.
24
     *
25
     * @param string $commandName  The name of the command being tested.
26
     * @param array $options  An array of options to pass to the command.
27
     * @return CommandTester
28
     * @throws RuntimeException
29
     */
30
    protected function getCommandTester(string $commandName, array $options = []): CommandTester
31
    {
32
        if (!$command = $this->getApplication()->find($commandName)) {
33
            throw new RuntimeException(
34
                sprintf('Please register the % command before attempting to test it.', $commandName)
35
            );
36
        }
37
        $commandTester = new CommandTester($command);
38
        $commandTester->execute(['command' => $commandName], $options);
39
        return $commandTester;
40
    }
41
42
    /**
43
     * Returns the application registered with this test.
44
     *
45
     * @return Application
46
     */
47
    protected function getApplication(): Application
48
    {
49
        return $this->application;
50
    }
51
}
52