Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
41 changes: 38 additions & 3 deletions tests/system/Commands/TestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +33,9 @@ protected function setUp(): void
parent::setUp();

$this->resetStreamFilterBuffer();

putenv('NO_COLOR=1');
CLI::init();
}

protected function tearDown(): void
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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();
}
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading