1313
1414namespace CodeIgniter \CLI ;
1515
16+ use App \Commands \AppAboutCommand as AppAboutCommandOverride ;
17+ use App \Commands \AppInfo as AppInfoOverride ;
1618use CodeIgniter \Autoloader \FileLocator ;
1719use CodeIgniter \Autoloader \FileLocatorInterface ;
1820use CodeIgniter \CLI \Exceptions \CommandNotFoundException ;
@@ -68,23 +70,14 @@ private function getUndecoratedBuffer(): string
6870 return preg_replace ('/\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()) ?? '' ;
6971 }
7072
71- private function copyCommand (string $ path ): void
73+ private function loadOverrideFixture (string $ file ): string
7274 {
73- if (! is_dir (APPPATH . 'Commands ' )) {
74- mkdir (APPPATH . 'Commands ' );
75- }
76-
77- copy ($ path , APPPATH . 'Commands/ ' . basename ($ path ));
78- clearstatcache (true );
79- }
75+ $ path = SUPPORTPATH . '_command/ ' . $ file ;
8076
81- private function deleteCommand (string $ path ): void
82- {
83- if (is_file (APPPATH . 'Commands/ ' . basename ($ path ))) {
84- unlink (APPPATH . 'Commands/ ' . basename ($ path ));
85- }
77+ // The fixture sits outside any PSR-4 root, so the autoloader cannot load it.
78+ require_once $ path ;
8679
87- clearstatcache ( true ) ;
80+ return $ path ;
8881 }
8982
9083 public function testRunOnUnknownCommand (): void
@@ -358,7 +351,7 @@ public function testRunCommandViaAlias(): void
358351
359352 public function testAliasClashingWithCommandNameFailsHard (): void
360353 {
361- $ this ->injectAliasLocator ([
354+ $ this ->injectFixtureLocator ([
362355 AliasTargetCommand::class => SUPPORTPATH . 'InvalidCommands/AliasTargetCommand.php ' ,
363356 AliasClashCommand::class => SUPPORTPATH . 'InvalidCommands/AliasClashCommand.php ' ,
364357 ]);
@@ -371,7 +364,7 @@ public function testAliasClashingWithCommandNameFailsHard(): void
371364
372365 public function testAliasClashingWithAnotherAliasFailsHard (): void
373366 {
374- $ this ->injectAliasLocator ([
367+ $ this ->injectFixtureLocator ([
375368 AliasClashCommand::class => SUPPORTPATH . 'InvalidCommands/AliasClashCommand.php ' ,
376369 AliasSecondClashCommand::class => SUPPORTPATH . 'InvalidCommands/AliasSecondClashCommand.php ' ,
377370 ]);
@@ -580,26 +573,28 @@ public function testGetCommandAlternativesThrowsDeprecationWhenCommandsArrayIsPa
580573
581574 public function testDiscoveredLegacyCommandsCanBeOverridden (): void
582575 {
583- $ this ->copyCommand (SUPPORTPATH . '_command/AppInfo.php ' );
576+ $ this ->injectFixtureLocator ([
577+ AppInfoOverride::class => $ this ->loadOverrideFixture ('AppInfo.php ' ),
578+ AppInfo::class => SUPPORTPATH . 'Commands/Legacy/AppInfo.php ' ,
579+ ]);
584580
585- command ( 'app:info ' );
581+ ( new Commands ())-> runLegacy ( 'app:info ' , [] );
586582
587583 $ this ->assertStringContainsString ('This is App\Commands\AppInfo ' , $ this ->getStreamFilterBuffer ());
588584 $ this ->assertStringNotContainsString ('CodeIgniter Version: ' , $ this ->getStreamFilterBuffer ());
589-
590- $ this ->deleteCommand (SUPPORTPATH . '_command/AppInfo.php ' );
591585 }
592586
593587 public function testDiscoveredModernCommandsCanBeOverridden (): void
594588 {
595- $ this ->copyCommand (SUPPORTPATH . '_command/AppAboutCommand.php ' );
589+ $ this ->injectFixtureLocator ([
590+ AppAboutCommandOverride::class => $ this ->loadOverrideFixture ('AppAboutCommand.php ' ),
591+ AppAboutCommand::class => SUPPORTPATH . 'Commands/Modern/AppAboutCommand.php ' ,
592+ ]);
596593
597- command ( 'app:about a ' );
594+ ( new Commands ())-> runCommand ( 'app:about ' , [ ' a ' ], [] );
598595
599596 $ this ->assertStringContainsString ('This is App\Commands\AppAboutCommand ' , $ this ->getStreamFilterBuffer ());
600597 $ this ->assertStringNotContainsString ('CodeIgniter Version: ' , $ this ->getStreamFilterBuffer ());
601-
602- $ this ->deleteCommand (SUPPORTPATH . '_command/AppAboutCommand.php ' );
603598 }
604599
605600 private function injectDuplicateLocator (): void
@@ -630,7 +625,7 @@ private function injectDuplicateLocator(): void
630625 *
631626 * @param array<class-string, string> $classToFile
632627 */
633- private function injectAliasLocator (array $ classToFile ): void
628+ private function injectFixtureLocator (array $ classToFile ): void
634629 {
635630 $ map = [];
636631
0 commit comments