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(); |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|