|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\CLI; |
15 | 15 |
|
| 16 | +use CodeIgniter\CLI\Attributes\Command; |
| 17 | +use CodeIgniter\CLI\Attributes\GeneratorCommand; |
16 | 18 | use CodeIgniter\Exceptions\LogicException; |
17 | 19 | use CodeIgniter\Test\CIUnitTestCase; |
18 | 20 | use CodeIgniter\Test\Mock\MockInputOutput; |
@@ -413,6 +415,89 @@ public function testAcceptingCodeIgniterNamespacePromptProceeds(): void |
413 | 415 | $this->assertStringContainsString('File exists: ', $io->getOutput()); |
414 | 416 | } |
415 | 417 |
|
| 418 | + public function testNonInteractiveCodeIgniterNamespaceWarnsOnceAndProceeds(): void |
| 419 | + { |
| 420 | + $io = new MockInputOutput(); |
| 421 | + CLI::setInputOutput($io); |
| 422 | + |
| 423 | + $command = $this->createClassAndViewGenerator(); |
| 424 | + $command->setInteractive(false); |
| 425 | + |
| 426 | + $this->assertSame(EXIT_ERROR, $command->run([CLI::class], ['namespace' => 'CodeIgniter'])); |
| 427 | + $this->assertSame( |
| 428 | + <<<'EOT' |
| 429 | +
|
| 430 | + Warning: Using the "CodeIgniter" namespace will generate the file in the system directory. |
| 431 | + File exists: "SYSTEMPATH/CLI/CLI.php" |
| 432 | + File exists: "SYSTEMPATH/CLI/Commands.php" |
| 433 | + |
| 434 | + EOT, |
| 435 | + preg_replace('/\e\[[^m]+m/', '', $io->getOutput()), |
| 436 | + ); |
| 437 | + } |
| 438 | + |
| 439 | + public function testAcceptedCodeIgniterNamespaceIsReusedForTheView(): void |
| 440 | + { |
| 441 | + $io = new MockInputOutput(); |
| 442 | + $io->setInputs(['y']); |
| 443 | + CLI::setInputOutput($io); |
| 444 | + |
| 445 | + $command = $this->createClassAndViewGenerator(); |
| 446 | + $command->setInteractive(true); |
| 447 | + |
| 448 | + $this->assertSame(EXIT_ERROR, $command->run([CLI::class], ['namespace' => 'CodeIgniter'])); |
| 449 | + $this->assertSame( |
| 450 | + <<<'EOT' |
| 451 | +
|
| 452 | + Warning: Using the "CodeIgniter" namespace will generate the file in the system directory. |
| 453 | + Are you sure you want to continue? [y, n]: y |
| 454 | + File exists: "SYSTEMPATH/CLI/CLI.php" |
| 455 | + File exists: "SYSTEMPATH/CLI/Commands.php" |
| 456 | + |
| 457 | + EOT, |
| 458 | + preg_replace('/\e\[[^m]+m/', '', $io->getOutput()), |
| 459 | + ); |
| 460 | + } |
| 461 | + |
| 462 | + public function testCodeIgniterNamespaceIsConfirmedAgainOnTheNextRun(): void |
| 463 | + { |
| 464 | + $io = new MockInputOutput(); |
| 465 | + $io->setInputs(['y', 'n']); |
| 466 | + CLI::setInputOutput($io); |
| 467 | + |
| 468 | + $command = $this->createClassAndViewGenerator(); |
| 469 | + $command->setInteractive(true); |
| 470 | + |
| 471 | + $this->assertSame(EXIT_ERROR, $command->run([CLI::class], ['namespace' => 'CodeIgniter'])); |
| 472 | + $this->assertSame(EXIT_SUCCESS, $command->run([CLI::class], ['namespace' => 'CodeIgniter'])); |
| 473 | + $this->assertSame( |
| 474 | + <<<'EOT' |
| 475 | +
|
| 476 | + Warning: Using the "CodeIgniter" namespace will generate the file in the system directory. |
| 477 | + Are you sure you want to continue? [y, n]: y |
| 478 | + File exists: "SYSTEMPATH/CLI/CLI.php" |
| 479 | + File exists: "SYSTEMPATH/CLI/Commands.php" |
| 480 | + Warning: Using the "CodeIgniter" namespace will generate the file in the system directory. |
| 481 | + Are you sure you want to continue? [y, n]: n |
| 482 | + Operation has been cancelled. |
| 483 | + |
| 484 | + EOT, |
| 485 | + preg_replace('/\e\[[^m]+m/', '', $io->getOutput()), |
| 486 | + ); |
| 487 | + } |
| 488 | + |
| 489 | + private function createClassAndViewGenerator(): AbstractGeneratorCommand |
| 490 | + { |
| 491 | + return new #[Command(name: 'make:classandview', description: 'Generates a class and a view.', group: 'Fixtures')] |
| 492 | + #[GeneratorCommand(component: 'Widget', template: 'config.tpl.php')] |
| 493 | + class (new Commands()) extends AbstractGeneratorCommand { |
| 494 | + protected function execute(array $arguments, array $options): int |
| 495 | + { |
| 496 | + return $this->generateClass() | $this->generateView(Commands::class); |
| 497 | + } |
| 498 | + }; |
| 499 | + } |
| 500 | + |
416 | 501 | public function testImportsAreSortedInGeneratedContent(): void |
417 | 502 | { |
418 | 503 | $command = new ImportSortingGeneratorCommand(new Commands()); |
|
0 commit comments