|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\CLI; |
15 | 15 |
|
| 16 | +use App\Commands\ListCommands as AppListCommands; |
16 | 17 | use CodeIgniter\Autoloader\FileLocatorInterface; |
| 18 | +use CodeIgniter\Commands\ListCommands; |
17 | 19 | use CodeIgniter\Test\CIUnitTestCase; |
18 | 20 | use CodeIgniter\Test\StreamFilterTrait; |
19 | 21 | use Config\Services; |
@@ -42,22 +44,6 @@ protected function resetAll(): void |
42 | 44 | CLI::reset(); |
43 | 45 | } |
44 | 46 |
|
45 | | - private function copyAppListCommands(): void |
46 | | - { |
47 | | - if (! is_dir(APPPATH . 'Commands')) { |
48 | | - mkdir(APPPATH . 'Commands'); |
49 | | - } |
50 | | - |
51 | | - copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php'); |
52 | | - } |
53 | | - |
54 | | - private function deleteAppListCommands(): void |
55 | | - { |
56 | | - if (is_file(APPPATH . 'Commands/ListCommands.php')) { |
57 | | - unlink(APPPATH . 'Commands/ListCommands.php'); |
58 | | - } |
59 | | - } |
60 | | - |
61 | 47 | public function testRunOnUnknownCommand(): void |
62 | 48 | { |
63 | 49 | $commands = new Commands(); |
@@ -166,13 +152,22 @@ public function testDiscoverCommandsWithNoFiles(): void |
166 | 152 |
|
167 | 153 | public function testDiscoveredCommandsCanBeOverridden(): void |
168 | 154 | { |
169 | | - $this->copyAppListCommands(); |
| 155 | + // The fixture sits outside any PSR-4 root, so the autoloader cannot load it. |
| 156 | + require_once SUPPORTPATH . '_command/ListCommands.php'; |
| 157 | + |
| 158 | + $files = [ |
| 159 | + SUPPORTPATH . '_command/ListCommands.php' => AppListCommands::class, |
| 160 | + SYSTEMPATH . 'Commands/ListCommands.php' => ListCommands::class, |
| 161 | + ]; |
| 162 | + |
| 163 | + $locator = $this->createMock(FileLocatorInterface::class); |
| 164 | + $locator->method('listFiles')->with('Commands/')->willReturn(array_keys($files)); |
| 165 | + $locator->method('findQualifiedNameFromPath')->willReturnCallback(static fn (string $file): string => $files[$file]); |
| 166 | + Services::injectMock('locator', $locator); |
170 | 167 |
|
171 | | - command('list'); |
| 168 | + (new Commands())->run('list', []); |
172 | 169 |
|
173 | 170 | $this->assertStringContainsString('This is App\Commands\ListCommands', $this->getStreamFilterBuffer()); |
174 | 171 | $this->assertStringNotContainsString('Displays basic usage information.', $this->getStreamFilterBuffer()); |
175 | | - |
176 | | - $this->deleteAppListCommands(); |
177 | 172 | } |
178 | 173 | } |
0 commit comments