diff --git a/system/Commands/Generators/TestGenerator.php b/system/Commands/Generators/TestGenerator.php index d37864d76efc..47f71294c823 100644 --- a/system/Commands/Generators/TestGenerator.php +++ b/system/Commands/Generators/TestGenerator.php @@ -76,6 +76,9 @@ class TestGenerator extends BaseCommand */ public function run(array $params) { + // Ensure tests are always suffixed with 'Test' + $params['suffix'] = null; + $this->component = 'Test'; $this->template = 'test.tpl.php'; diff --git a/tests/system/Commands/TestGeneratorTest.php b/tests/system/Commands/TestGeneratorTest.php index b449afd76f05..0053a311cef9 100644 --- a/tests/system/Commands/TestGeneratorTest.php +++ b/tests/system/Commands/TestGeneratorTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; /** @@ -25,22 +26,59 @@ final class TestGeneratorTest extends CIUnitTestCase { use StreamFilterTrait; + protected function setUp(): void + { + parent::setUp(); + + $this->resetStreamFilterBuffer(); + } + protected function tearDown(): void + { + parent::tearDown(); + + $this->clearTestFiles(); + $this->resetStreamFilterBuffer(); + } + + private function clearTestFiles(): void { $result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer()); - $file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, trim(substr($result, 14))); - $dir = dirname($file); + + $file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, trim(substr($result, strlen('File created: ')))); if (is_file($file)) { unlink($file); } - if (is_dir($dir)) { + + $dir = dirname($file) . DIRECTORY_SEPARATOR; + if (is_dir($dir) && ! in_array($dir, [TESTPATH, TESTPATH . 'system/', TESTPATH . '_support/'], true)) { rmdir($dir); } } - public function testGenerateTest(): void + #[DataProvider('provideGenerateTestFiles')] + public function testGenerateTestFiles(string $name, string $expectedClass): void + { + command(sprintf('make:test %s', $name)); + + $expectedTestFile = str_replace('/', DIRECTORY_SEPARATOR, sprintf('%stests/%s.php', ROOTPATH, $expectedClass)); + $expectedMessage = sprintf('File created: %s', str_replace(ROOTPATH, 'ROOTPATH' . DIRECTORY_SEPARATOR, $expectedTestFile)); + $this->assertStringContainsString($expectedMessage, $this->getStreamFilterBuffer()); + $this->assertFileExists($expectedTestFile); + } + + /** + * @return iterable + */ + public static function provideGenerateTestFiles(): iterable { - command('make:test Foo/Bar'); - $this->assertFileExists(ROOTPATH . 'tests/Foo/Bar.php'); + yield 'simple class name' => ['Foo', 'FooTest']; + + yield 'namespaced class name' => ['Foo/Bar', 'Foo/BarTest']; + + yield 'class with suffix' => ['Foo/BarTest', 'Foo/BarTest']; + + // the 4 slashes are needed to escape here and in the command + yield 'namespace style class name' => ['Foo\\\\Bar', 'Foo/BarTest']; } } diff --git a/user_guide_src/source/changelogs/v4.6.2.rst b/user_guide_src/source/changelogs/v4.6.2.rst index 7ebf81108938..ccdb5de06e3c 100644 --- a/user_guide_src/source/changelogs/v4.6.2.rst +++ b/user_guide_src/source/changelogs/v4.6.2.rst @@ -37,6 +37,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``. - **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.