forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandsTest.php
More file actions
178 lines (142 loc) · 5.1 KB
/
CommandsTest.php
File metadata and controls
178 lines (142 loc) · 5.1 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
<?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\CLI;
use CodeIgniter\Autoloader\FileLocatorInterface;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Services;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use RuntimeException;
use Tests\Support\Commands\AppInfo;
/**
* @internal
*/
#[CoversClass(Commands::class)]
#[Group('Others')]
final class CommandsTest extends CIUnitTestCase
{
use StreamFilterTrait;
#[After]
#[Before]
protected function resetAll(): void
{
$this->resetServices();
CLI::reset();
}
private function copyAppListCommands(): void
{
if (! is_dir(APPPATH . 'Commands')) {
mkdir(APPPATH . 'Commands');
}
copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php');
}
private function deleteAppListCommands(): void
{
if (is_file(APPPATH . 'Commands/ListCommands.php')) {
unlink(APPPATH . 'Commands/ListCommands.php');
}
}
public function testRunOnUnknownCommand(): void
{
$commands = new Commands();
$this->assertSame(EXIT_ERROR, $commands->run('app:unknown', []));
$this->assertArrayNotHasKey('app:unknown', $commands->getCommands());
$this->assertStringContainsString('Command "app:unknown" not found', $this->getStreamFilterBuffer());
}
public function testRunOnUnknownCommandButWithOneAlternative(): void
{
$commands = new Commands();
$this->assertSame(EXIT_ERROR, $commands->run('app:inf', []));
$this->assertSame(
<<<'EOT'
Command "app:inf" not found.
Did you mean this?
app:info
EOT,
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}
public function testRunOnUnknownCommandButWithMultipleAlternatives(): void
{
$commands = new Commands();
$this->assertSame(EXIT_ERROR, $commands->run('app:', []));
$this->assertSame(
<<<'EOT'
Command "app:" not found.
Did you mean one of these?
app:destructive
app:info
EOT,
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}
public function testRunOnAbstractCommandCannotBeRun(): void
{
$commands = new Commands();
$this->assertSame(EXIT_ERROR, $commands->run('app:pablo', []));
$this->assertArrayNotHasKey('app:pablo', $commands->getCommands());
$this->assertStringContainsString('Command "app:pablo" not found', $this->getStreamFilterBuffer());
}
public function testRunOnKnownCommand(): void
{
$commands = new Commands();
$this->assertSame(EXIT_SUCCESS, $commands->run('app:info', []));
$this->assertArrayHasKey('app:info', $commands->getCommands());
$this->assertStringContainsString('CodeIgniter Version', $this->getStreamFilterBuffer());
}
public function testDestructiveCommandIsNotRisky(): void
{
$this->expectException(RuntimeException::class);
command('app:destructive');
}
public function testDiscoverCommandsDoNotRunTwice(): void
{
$locator = $this->createMock(FileLocatorInterface::class);
$locator
->expects($this->once())
->method('listFiles')
->with('Commands/')
->willReturn([SUPPORTPATH . 'Commands/AppInfo.php']);
$locator
->expects($this->once())
->method('findQualifiedNameFromPath')
->with(SUPPORTPATH . 'Commands/AppInfo.php')
->willReturn(AppInfo::class);
Services::injectMock('locator', $locator);
$commands = new Commands(); // discoverCommands will be called in the constructor
$commands->discoverCommands();
}
public function testDiscoverCommandsWithNoFiles(): void
{
$locator = $this->createMock(FileLocatorInterface::class);
$locator
->expects($this->once())
->method('listFiles')
->with('Commands/')
->willReturn([]);
$locator
->expects($this->never())
->method('findQualifiedNameFromPath');
Services::injectMock('locator', $locator);
new Commands();
}
public function testDiscoveredCommandsCanBeOverridden(): void
{
$this->copyAppListCommands();
command('list');
$this->assertStringContainsString('This is App\Commands\ListCommands', $this->getStreamFilterBuffer());
$this->assertStringNotContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
$this->deleteAppListCommands();
}
}