Passed
Push — master ( 4165df...b83769 )
by Théo
01:49
created

CommandTestCase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 93
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutput() 0 23 2
A tearDown() 0 7 1
A setUp() 0 12 1
A getCommandTester() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Test;
16
17
use KevinGH\Box\Application;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Output\StreamOutput;
20
use Symfony\Component\Console\Tester\CommandTester;
21
use function KevinGH\Box\make_tmp_dir;
0 ignored issues
show
introduced by
The function KevinGH\Box\make_tmp_dir was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
use function KevinGH\Box\remove_dir;
0 ignored issues
show
introduced by
The function KevinGH\Box\remove_dir was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
23
24
/**
25
 * Makes it easier to test Box commands.
26
 *
27
 * @author Kevin Herrera <[email protected]>
28
 */
29
abstract class CommandTestCase extends \PHPUnit\Framework\TestCase
30
{
31
    /**
32
     * @var Application
33
     */
34
    protected $application;
35
36
    /**
37
     * @var string
38
     */
39
    protected $cwd;
40
41
    /**
42
     * @var string
43
     */
44
    protected $tmp;
45
46
    /**
47
     * @var string the name of the command
48
     */
49
    private $name;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function setUp(): void
55
    {
56
        $this->cwd = getcwd();
57
        $this->tmp = make_tmp_dir('box', __CLASS__);
58
59
        chdir($this->tmp);
60
61
        $this->application = new Application();
62
63
        $this->name = $this->getCommand()->getName();
64
65
        $this->application->add($this->getCommand());
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function tearDown(): void
72
    {
73
        chdir($this->cwd);
74
75
        remove_dir($this->tmp);
76
77
        parent::tearDown();
78
    }
79
80
    /**
81
     * Returns the command to be tested.
82
     *
83
     * @return Command the command
84
     */
85
    abstract protected function getCommand(): Command;
86
87
    /**
88
     * Returns the output for the tester.
89
     *
90
     * @param CommandTester $tester the tester
91
     *
92
     * @return string the output
93
     */
94
    protected function getOutput(CommandTester $tester)
95
    {
96
        /** @var $output StreamOutput */
97
        $output = $tester->getOutput();
98
        $stream = $output->getStream();
99
        $string = '';
100
101
        rewind($stream);
102
103
        while (false === feof($stream)) {
104
            $string .= fgets($stream);
105
        }
106
107
        $string = preg_replace(
108
            [
109
                '/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/',
110
                '/[\x03|\x1a]/',
111
            ],
112
            ['', '', ''],
113
            $string
114
        );
115
116
        return str_replace(PHP_EOL, "\n", $string);
117
    }
118
119
    protected function getCommandTester(): CommandTester
120
    {
121
        return new CommandTester($this->application->get($this->name));
122
    }
123
}
124