forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandTest.php
More file actions
192 lines (156 loc) · 5.6 KB
/
CommandTest.php
File metadata and controls
192 lines (156 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands;
use CodeIgniter\CLI\Commands;
use CodeIgniter\Log\Logger;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use RuntimeException;
use Tests\Support\Commands\AppInfo;
use Tests\Support\Commands\ParamsReveal;
/**
* @internal
*/
#[Group('Others')]
final class CommandTest extends CIUnitTestCase
{
use StreamFilterTrait;
private Logger $logger;
private Commands $commands;
protected function setUp(): void
{
$this->resetServices();
parent::setUp();
$this->logger = service('logger');
$this->commands = service('commands');
}
protected function getBuffer(): string
{
return $this->getStreamFilterBuffer();
}
public function testListCommands(): void
{
command('list');
// make sure the result looks like a command list
$this->assertStringContainsString('Lists the available commands.', $this->getBuffer());
$this->assertStringContainsString('Displays basic usage information.', $this->getBuffer());
}
public function testListCommandsSimple(): void
{
command('list --simple');
$this->assertStringContainsString('db:seed', $this->getBuffer());
$this->assertStringNotContainsString('Lists the available commands.', $this->getBuffer());
}
public function testCustomCommand(): void
{
command('app:info');
$this->assertStringContainsString('CI Version:', $this->getBuffer());
}
public function testShowError(): void
{
command('app:info');
$commands = $this->commands->getCommands();
/** @var AppInfo */
$command = new $commands['app:info']['class']($this->logger, $this->commands);
$command->helpme();
$this->assertStringContainsString('Displays basic usage information.', $this->getBuffer());
}
public function testCommandCall(): void
{
command('app:info');
$commands = $this->commands->getCommands();
/** @var AppInfo */
$command = new $commands['app:info']['class']($this->logger, $this->commands);
$command->bomb();
$this->assertStringContainsString('Invalid "background" color:', $this->getBuffer());
}
public function testAbstractCommand(): void
{
command('app:pablo');
$this->assertStringContainsString('not found', $this->getBuffer());
}
public function testNamespacesCommand(): void
{
command('namespaces');
$this->assertStringContainsString('| Namespace', $this->getBuffer());
$this->assertStringContainsString('| Config', $this->getBuffer());
$this->assertStringContainsString('| Yes', $this->getBuffer());
}
public function testInexistentCommandWithNoAlternatives(): void
{
command('app:oops');
$this->assertStringContainsString('Command "app:oops" not found', $this->getBuffer());
}
public function testInexistentCommandsButWithOneAlternative(): void
{
command('namespace');
$this->assertStringContainsString('Command "namespace" not found.', $this->getBuffer());
$this->assertStringContainsString('Did you mean this?', $this->getBuffer());
$this->assertStringContainsString('namespaces', $this->getBuffer());
}
public function testInexistentCommandsButWithManyAlternatives(): void
{
command('clear');
$this->assertStringContainsString('Command "clear" not found.', $this->getBuffer());
$this->assertStringContainsString('Did you mean one of these?', $this->getBuffer());
$this->assertStringContainsString(':clear', $this->getBuffer());
}
public function testDestructiveCommandIsNotRisky(): void
{
$this->expectException(RuntimeException::class);
command('app:destructive');
}
/**
* @param list<string> $expected
*/
#[DataProvider('provideCommandParsesArgsCorrectly')]
public function testCommandParsesArgsCorrectly(string $input, array $expected): void
{
ParamsReveal::$args = null;
command($input);
$this->assertSame($expected, ParamsReveal::$args);
}
public static function provideCommandParsesArgsCorrectly(): iterable
{
return [
[
'reveal as df',
['as', 'df'],
],
[
'reveal',
[],
],
[
'reveal seg1 seg2 -opt1 -opt2',
['seg1', 'seg2', 'opt1' => null, 'opt2' => null],
],
[
'reveal seg1 seg2 -opt1 val1 seg3',
['seg1', 'seg2', 'opt1' => 'val1', 'seg3'],
],
[
'reveal as df -gh -jk -qw 12 zx cv',
['as', 'df', 'gh' => null, 'jk' => null, 'qw' => '12', 'zx', 'cv'],
],
[
'reveal as -df "some stuff" -jk 12 -sd "Some longer stuff" -fg \'using single quotes\'',
['as', 'df' => 'some stuff', 'jk' => '12', 'sd' => 'Some longer stuff', 'fg' => 'using single quotes'],
],
[
'reveal as -df "using mixed \'quotes\'\" here\""',
['as', 'df' => 'using mixed \'quotes\'" here"'],
],
];
}
}