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

UpdateTest::testConfigure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 32
rs 9.52
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\UpdateCommand;
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 UpdateTest 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
        'commands'       => [],
34
        'update'         => [
35
            ['command' => 'test', 'header' => 'Test Command'],
36
            ['command' => 'helper', 'options' => ['helper' => 'writeln'], 'footer' => 'Good!'],
37
            ['invoke' => [self::class, 'do']],
38
            ['invoke' => self::class . '::do'],
39
            'Spiral\Tests\Console\ok',
40
            ['invoke' => self::class . '::err'],
41
        ]
42
    ];
43
44
    public function testConfigure(): void
45
    {
46
        $core = $this->getCore(new StaticLocator([
47
            HelperCommand::class,
48
            TestCommand::class,
49
            UpdateCommand::class
50
        ]));
51
52
        $this->container->bind(Console::class, $core);
53
54
        $actual = $core->run('update')->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

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

133
    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...
134
    {
135
        throw new ShortException('Failed update command');
136
    }
137
138
    /**
139
     * @return Console
140
     */
141
    private function bindFailure(): Console
142
    {
143
        $core = $this->getCore(new StaticLocator([
144
            HelperCommand::class,
145
            TestCommand::class,
146
            UpdateCommand::class,
147
            FailedCommand::class,
148
            AnotherFailedCommand::class,
149
        ]));
150
        $this->container->bind(ConsoleConfig::class, new ConsoleConfig([
151
            'locateCommands' => false,
152
            'commands'       => [],
153
            'update'         => [
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