Passed
Push — master ( ed42e4...b3b06a )
by Kirill
04:44
created

ConfigureTest::bindFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework, SpiralScout LLC.
5
 *
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
declare(strict_types=1);
10
11
namespace Spiral\Tests\Console;
12
13
use Spiral\Console\Command\ConfigureCommand;
14
use Spiral\Console\Config\ConsoleConfig;
15
use Spiral\Console\Console;
16
use Spiral\Console\StaticLocator;
17
use Spiral\Tests\Console\Fixtures\AnotherFailedCommand;
18
use Spiral\Tests\Console\Fixtures\FailedCommand;
19
use Spiral\Tests\Console\Fixtures\HelperCommand;
20
use Spiral\Tests\Console\Fixtures\TestCommand;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Throwable;
23
24
class ConfigureTest extends BaseTest
25
{
26
    public const TOKENIZER_CONFIG = [
27
        'directories' => [__DIR__ . '/../src/Command', __DIR__ . '/Fixtures/'],
28
        'exclude'     => []
29
    ];
30
31
    public const CONFIG = [
32
        'locateCommands' => false,
33
        'configure'      => [
34
            ['command' => 'test', 'header' => 'Test Command'],
35
            ['command' => 'helper', 'options' => ['helper' => 'writeln'], 'footer' => 'Good!'],
36
            ['invoke' => [self::class, 'do']],
37
            ['invoke' => self::class . '::do'],
38
            'Spiral\Tests\Console\ok',
39
            ['invoke' => self::class . '::err'],
40
        ]
41
    ];
42
43
    /**
44
     * @throws Throwable
45
     */
46
    public function testConfigure(): void
47
    {
48
        $core = $this->getCore(new StaticLocator([
49
            HelperCommand::class,
50
            ConfigureCommand::class,
51
            TestCommand::class
52
        ]));
53
        $this->container->bind(Console::class, $core);
54
55
        $actual = $core->run('configure')->getOutput()->fetch();
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Output\BufferedOutput. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $actual = $core->run('configure')->getOutput()->/** @scrutinizer ignore-call */ fetch();
Loading history...
56
57
        $expected = <<<'text'
58
Configuring project:
59
60
Test Command
61
Hello World - 0
62
hello
63
Good!
64
65
OK
66
OK
67
OK2
68
exception
69
70
All done!
71
72
text;
73
74
        $this->assertSame(
75
            \str_replace("\r", '', $expected),
76
            \str_replace("\r", '', $actual)
77
        );
78
    }
79
80
    /**
81
     * @throws Throwable
82
     */
83
    public function testBreakFailure(): void
84
    {
85
        $core = $this->bindFailure();
86
87
        $output = $core->run('configure', ['--break' => true]);
88
        $result = $output->getOutput()->fetch();
89
90
        $this->assertStringContainsString('Unhandled failed command error at', $result);
91
        $this->assertStringContainsString('Aborting.', $result);
92
        $this->assertStringNotContainsString('Unhandled another failed command error at', $result);
93
        $this->assertEquals(1, $output->getCode());
94
    }
95
96
    /**
97
     * @throws Throwable
98
     */
99
    public function testIgnoreAndBreakFailure(): void
100
    {
101
        $core = $this->bindFailure();
102
103
        $output = $core->run('configure', ['--ignore' => true, '--break' => true]);
104
        $result = $output->getOutput()->fetch();
105
106
        $this->assertStringContainsString('Unhandled failed command error at', $result);
107
        $this->assertStringNotContainsString('Aborting.', $result);
108
        $this->assertStringContainsString('Unhandled another failed command error at', $result);
109
        $this->assertEquals(0, $output->getCode());
110
    }
111
112
    /**
113
     * @throws Throwable
114
     */
115
    public function testNoBreakFailure(): void
116
    {
117
        $core = $this->bindFailure();
118
        $this->container->bind(Console::class, $core);
119
120
        $output = $core->run('configure');
121
        $result = $output->getOutput()->fetch();
122
123
        $this->assertStringContainsString('Unhandled failed command error at', $result);
124
        $this->assertStringNotContainsString('Aborting.', $result);
125
        $this->assertStringContainsString('Unhandled another failed command error at', $result);
126
        $this->assertEquals(1, $output->getCode());
127
    }
128
129
    public function do(OutputInterface $output): void
130
    {
131
        $output->write('OK');
132
    }
133
134
    public function err(OutputInterface $output): void
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

134
    public function err(/** @scrutinizer ignore-unused */ OutputInterface $output): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        throw new ShortException('Failed configure command');
137
    }
138
139
    /**
140
     * @return Console
141
     */
142
    private function bindFailure(): Console
143
    {
144
        $core = $this->getCore(new StaticLocator([
145
            HelperCommand::class,
146
            ConfigureCommand::class,
147
            TestCommand::class,
148
            FailedCommand::class,
149
            AnotherFailedCommand::class,
150
        ]));
151
        $this->container->bind(ConsoleConfig::class, new ConsoleConfig([
152
            'locateCommands' => false,
153
            'configure'      => [
154
                ['command' => 'failed', 'header' => 'Failed Command'],
155
                ['command' => 'failed:another', 'header' => 'Another failed Command'],
156
            ]
157
        ]));
158
        $this->container->bind(Console::class, $core);
159
160
        return $core;
161
    }
162
}
163
164
function ok(OutputInterface $output): void
165
{
166
    $output->write('OK2');
167
}
168