Skip to content

Commit 8c7ab80

Browse files
authored
fix: make:test requires 3 inputs after entering an empty class name (#9637)
* Add failing test * Add fix * Add changelog
1 parent e52b915 commit 8c7ab80

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

system/CLI/GeneratorTrait.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,22 +282,21 @@ protected function qualifyClassName(): string
282282
return $namespace . $directoryString . str_replace('/', '\\', $class);
283283
}
284284

285-
/**
286-
* Normalize input classname.
287-
*/
288285
private function normalizeInputClassName(): string
289286
{
290287
// Gets the class name from input.
291288
$class = $this->params[0] ?? CLI::getSegment(2);
292289

293290
if ($class === null && $this->hasClassName) {
294-
// @codeCoverageIgnoreStart
295-
$nameLang = $this->classNameLang !== ''
291+
$nameField = $this->classNameLang !== ''
296292
? $this->classNameLang
297293
: 'CLI.generator.className.default';
298-
$class = CLI::prompt(lang($nameLang), null, 'required');
294+
$class = CLI::prompt(lang($nameField), null, 'required');
295+
296+
// Reassign the class name to the params array in case
297+
// the class name is requested again
298+
$this->params[0] = $class;
299299
CLI::newLine();
300-
// @codeCoverageIgnoreEnd
301300
}
302301

303302
helper('inflector');

tests/system/Commands/TestGeneratorTest.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace CodeIgniter\Commands;
1515

16+
use CodeIgniter\CLI\CLI;
1617
use CodeIgniter\Test\CIUnitTestCase;
18+
use CodeIgniter\Test\Mock\MockInputOutput;
1719
use CodeIgniter\Test\StreamFilterTrait;
1820
use PHPUnit\Framework\Attributes\DataProvider;
1921
use PHPUnit\Framework\Attributes\Group;
@@ -31,6 +33,9 @@ protected function setUp(): void
3133
parent::setUp();
3234

3335
$this->resetStreamFilterBuffer();
36+
37+
putenv('NO_COLOR=1');
38+
CLI::init();
3439
}
3540

3641
protected function tearDown(): void
@@ -39,19 +44,22 @@ protected function tearDown(): void
3944

4045
$this->clearTestFiles();
4146
$this->resetStreamFilterBuffer();
47+
48+
putenv('NO_COLOR');
49+
CLI::init();
4250
}
4351

4452
private function clearTestFiles(): void
4553
{
46-
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
54+
preg_match('/File created: (.*)/', $this->getStreamFilterBuffer(), $result);
4755

48-
$file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, trim(substr($result, strlen('File created: '))));
56+
$file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, $result[1] ?? '');
4957
if (is_file($file)) {
5058
unlink($file);
5159
}
5260

5361
$dir = dirname($file) . DIRECTORY_SEPARATOR;
54-
if (is_dir($dir) && ! in_array($dir, [TESTPATH, TESTPATH . 'system/', TESTPATH . '_support/'], true)) {
62+
if (is_dir($dir) && ! in_array($dir, ['/', TESTPATH, TESTPATH . 'system/', TESTPATH . '_support/'], true)) {
5563
rmdir($dir);
5664
}
5765
}
@@ -81,4 +89,31 @@ public static function provideGenerateTestFiles(): iterable
8189
// the 4 slashes are needed to escape here and in the command
8290
yield 'namespace style class name' => ['Foo\\\\Bar', 'Foo/BarTest'];
8391
}
92+
93+
public function testGenerateTestWithEmptyClassName(): void
94+
{
95+
$expectedFile = ROOTPATH . 'tests/FooTest.php';
96+
97+
try {
98+
$io = new MockInputOutput();
99+
CLI::setInputOutput($io);
100+
101+
// Simulate running `make:test` with no input followed by entering `Foo`
102+
$io->setInputs(['', 'Foo']);
103+
command('make:test');
104+
105+
$expectedOutput = 'Test class name : ' . PHP_EOL;
106+
$expectedOutput .= 'The "Test class name" field is required.' . PHP_EOL;
107+
$expectedOutput .= 'Test class name : Foo' . PHP_EOL . PHP_EOL;
108+
$expectedOutput .= 'File created: ROOTPATH/tests/FooTest.php' . PHP_EOL . PHP_EOL;
109+
$this->assertSame($expectedOutput, $io->getOutput());
110+
$this->assertFileExists($expectedFile);
111+
} finally {
112+
if (is_file($expectedFile)) {
113+
unlink($expectedFile);
114+
}
115+
116+
CLI::resetInputOutput();
117+
}
118+
}
84119
}

user_guide_src/source/changelogs/v4.6.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Bugs Fixed
3838
- **Cache:** Fixed a bug where a corrupted or unreadable cache file could cause an unhandled exception in ``FileHandler::getItem()``.
3939
- **Commands:** Fixed a bug in ``make:test`` where it would always error on Windows.
4040
- **Commands:** Fixed a bug in ``make:test`` where the generated test file would not end with ``Test.php``.
41+
- **Commands:** Fixed a bug in ``make:test`` where input prompt would display for three times after not entering a class name.
4142
- **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.
4243
- **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.
4344
- **Database:** Fixed encapsulation violation in ``BasePreparedQuery`` when accessing ``BaseConnection::transStatus`` protected property.

0 commit comments

Comments
 (0)