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