diff --git a/system/CLI/GeneratorTrait.php b/system/CLI/GeneratorTrait.php index 1024fa695259..62f5ceec09ae 100644 --- a/system/CLI/GeneratorTrait.php +++ b/system/CLI/GeneratorTrait.php @@ -282,22 +282,21 @@ protected function qualifyClassName(): string return $namespace . $directoryString . str_replace('/', '\\', $class); } - /** - * Normalize input classname. - */ private function normalizeInputClassName(): string { // Gets the class name from input. $class = $this->params[0] ?? CLI::getSegment(2); if ($class === null && $this->hasClassName) { - // @codeCoverageIgnoreStart - $nameLang = $this->classNameLang !== '' + $nameField = $this->classNameLang !== '' ? $this->classNameLang : 'CLI.generator.className.default'; - $class = CLI::prompt(lang($nameLang), null, 'required'); + $class = CLI::prompt(lang($nameField), null, 'required'); + + // Reassign the class name to the params array in case + // the class name is requested again + $this->params[0] = $class; CLI::newLine(); - // @codeCoverageIgnoreEnd } helper('inflector'); diff --git a/tests/system/Commands/TestGeneratorTest.php b/tests/system/Commands/TestGeneratorTest.php index 0053a311cef9..b67c136a4009 100644 --- a/tests/system/Commands/TestGeneratorTest.php +++ b/tests/system/Commands/TestGeneratorTest.php @@ -13,7 +13,9 @@ namespace CodeIgniter\Commands; +use CodeIgniter\CLI\CLI; use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\Mock\MockInputOutput; use CodeIgniter\Test\StreamFilterTrait; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; @@ -31,6 +33,9 @@ protected function setUp(): void parent::setUp(); $this->resetStreamFilterBuffer(); + + putenv('NO_COLOR=1'); + CLI::init(); } protected function tearDown(): void @@ -39,19 +44,22 @@ protected function tearDown(): void $this->clearTestFiles(); $this->resetStreamFilterBuffer(); + + putenv('NO_COLOR'); + CLI::init(); } private function clearTestFiles(): void { - $result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer()); + preg_match('/File created: (.*)/', $this->getStreamFilterBuffer(), $result); - $file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, trim(substr($result, strlen('File created: ')))); + $file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, $result[1] ?? ''); if (is_file($file)) { unlink($file); } $dir = dirname($file) . DIRECTORY_SEPARATOR; - if (is_dir($dir) && ! in_array($dir, [TESTPATH, TESTPATH . 'system/', TESTPATH . '_support/'], true)) { + if (is_dir($dir) && ! in_array($dir, ['/', TESTPATH, TESTPATH . 'system/', TESTPATH . '_support/'], true)) { rmdir($dir); } } @@ -81,4 +89,31 @@ public static function provideGenerateTestFiles(): iterable // the 4 slashes are needed to escape here and in the command yield 'namespace style class name' => ['Foo\\\\Bar', 'Foo/BarTest']; } + + public function testGenerateTestWithEmptyClassName(): void + { + $expectedFile = ROOTPATH . 'tests/FooTest.php'; + + try { + $io = new MockInputOutput(); + CLI::setInputOutput($io); + + // Simulate running `make:test` with no input followed by entering `Foo` + $io->setInputs(['', 'Foo']); + command('make:test'); + + $expectedOutput = 'Test class name : ' . PHP_EOL; + $expectedOutput .= 'The "Test class name" field is required.' . PHP_EOL; + $expectedOutput .= 'Test class name : Foo' . PHP_EOL . PHP_EOL; + $expectedOutput .= 'File created: ROOTPATH/tests/FooTest.php' . PHP_EOL . PHP_EOL; + $this->assertSame($expectedOutput, $io->getOutput()); + $this->assertFileExists($expectedFile); + } finally { + if (is_file($expectedFile)) { + unlink($expectedFile); + } + + CLI::resetInputOutput(); + } + } } diff --git a/user_guide_src/source/changelogs/v4.6.2.rst b/user_guide_src/source/changelogs/v4.6.2.rst index ccdb5de06e3c..ce2a88701a8a 100644 --- a/user_guide_src/source/changelogs/v4.6.2.rst +++ b/user_guide_src/source/changelogs/v4.6.2.rst @@ -38,6 +38,7 @@ Bugs Fixed - **Cache:** Fixed a bug where a corrupted or unreadable cache file could cause an unhandled exception in ``FileHandler::getItem()``. - **Commands:** Fixed a bug in ``make:test`` where it would always error on Windows. - **Commands:** Fixed a bug in ``make:test`` where the generated test file would not end with ``Test.php``. +- **Commands:** Fixed a bug in ``make:test`` where input prompt would display for three times after not entering a class name. - **CURLRequest:** Fixed a bug where intermediate HTTP responses were not properly removed from the response chain in certain scenarios, causing incorrect status codes and headers to be returned instead of the final response. - **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness. - **Database:** Fixed encapsulation violation in ``BasePreparedQuery`` when accessing ``BaseConnection::transStatus`` protected property.