diff --git a/tests/system/CLI/CommandsTest.php b/tests/system/CLI/CommandsTest.php index ca93d9a3fb57..c208954752d3 100644 --- a/tests/system/CLI/CommandsTest.php +++ b/tests/system/CLI/CommandsTest.php @@ -13,6 +13,8 @@ namespace CodeIgniter\CLI; +use App\Commands\AppAboutCommand as AppAboutCommandOverride; +use App\Commands\AppInfo as AppInfoOverride; use CodeIgniter\Autoloader\FileLocator; use CodeIgniter\Autoloader\FileLocatorInterface; use CodeIgniter\CLI\Exceptions\CommandNotFoundException; @@ -68,23 +70,14 @@ private function getUndecoratedBuffer(): string return preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()) ?? ''; } - private function copyCommand(string $path): void + private function loadOverrideFixture(string $file): string { - if (! is_dir(APPPATH . 'Commands')) { - mkdir(APPPATH . 'Commands'); - } - - copy($path, APPPATH . 'Commands/' . basename($path)); - clearstatcache(true); - } + $path = SUPPORTPATH . '_command/' . $file; - private function deleteCommand(string $path): void - { - if (is_file(APPPATH . 'Commands/' . basename($path))) { - unlink(APPPATH . 'Commands/' . basename($path)); - } + // The fixture sits outside any PSR-4 root, so the autoloader cannot load it. + require_once $path; - clearstatcache(true); + return $path; } public function testRunOnUnknownCommand(): void @@ -358,7 +351,7 @@ public function testRunCommandViaAlias(): void public function testAliasClashingWithCommandNameFailsHard(): void { - $this->injectAliasLocator([ + $this->injectFixtureLocator([ AliasTargetCommand::class => SUPPORTPATH . 'InvalidCommands/AliasTargetCommand.php', AliasClashCommand::class => SUPPORTPATH . 'InvalidCommands/AliasClashCommand.php', ]); @@ -371,7 +364,7 @@ public function testAliasClashingWithCommandNameFailsHard(): void public function testAliasClashingWithAnotherAliasFailsHard(): void { - $this->injectAliasLocator([ + $this->injectFixtureLocator([ AliasClashCommand::class => SUPPORTPATH . 'InvalidCommands/AliasClashCommand.php', AliasSecondClashCommand::class => SUPPORTPATH . 'InvalidCommands/AliasSecondClashCommand.php', ]); @@ -580,26 +573,28 @@ public function testGetCommandAlternativesThrowsDeprecationWhenCommandsArrayIsPa public function testDiscoveredLegacyCommandsCanBeOverridden(): void { - $this->copyCommand(SUPPORTPATH . '_command/AppInfo.php'); + $this->injectFixtureLocator([ + AppInfoOverride::class => $this->loadOverrideFixture('AppInfo.php'), + AppInfo::class => SUPPORTPATH . 'Commands/Legacy/AppInfo.php', + ]); - command('app:info'); + (new Commands())->runLegacy('app:info', []); $this->assertStringContainsString('This is App\Commands\AppInfo', $this->getStreamFilterBuffer()); $this->assertStringNotContainsString('CodeIgniter Version:', $this->getStreamFilterBuffer()); - - $this->deleteCommand(SUPPORTPATH . '_command/AppInfo.php'); } public function testDiscoveredModernCommandsCanBeOverridden(): void { - $this->copyCommand(SUPPORTPATH . '_command/AppAboutCommand.php'); + $this->injectFixtureLocator([ + AppAboutCommandOverride::class => $this->loadOverrideFixture('AppAboutCommand.php'), + AppAboutCommand::class => SUPPORTPATH . 'Commands/Modern/AppAboutCommand.php', + ]); - command('app:about a'); + (new Commands())->runCommand('app:about', ['a'], []); $this->assertStringContainsString('This is App\Commands\AppAboutCommand', $this->getStreamFilterBuffer()); $this->assertStringNotContainsString('CodeIgniter Version:', $this->getStreamFilterBuffer()); - - $this->deleteCommand(SUPPORTPATH . '_command/AppAboutCommand.php'); } private function injectDuplicateLocator(): void @@ -630,7 +625,7 @@ private function injectDuplicateLocator(): void * * @param array $classToFile */ - private function injectAliasLocator(array $classToFile): void + private function injectFixtureLocator(array $classToFile): void { $map = [];