Skip to content

Commit e52b915

Browse files
authored
Merge pull request #9636 from paulbalandan/make-test-suffix
fix: ensure `make:test` generates test files ending in `Test`
2 parents 268a527 + b1fdbb7 commit e52b915

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

system/Commands/Generators/TestGenerator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class TestGenerator extends BaseCommand
7676
*/
7777
public function run(array $params)
7878
{
79+
// Ensure tests are always suffixed with 'Test'
80+
$params['suffix'] = null;
81+
7982
$this->component = 'Test';
8083
$this->template = 'test.tpl.php';
8184

tests/system/Commands/TestGeneratorTest.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use CodeIgniter\Test\StreamFilterTrait;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\Attributes\Group;
1920

2021
/**
@@ -25,22 +26,59 @@ final class TestGeneratorTest extends CIUnitTestCase
2526
{
2627
use StreamFilterTrait;
2728

29+
protected function setUp(): void
30+
{
31+
parent::setUp();
32+
33+
$this->resetStreamFilterBuffer();
34+
}
35+
2836
protected function tearDown(): void
37+
{
38+
parent::tearDown();
39+
40+
$this->clearTestFiles();
41+
$this->resetStreamFilterBuffer();
42+
}
43+
44+
private function clearTestFiles(): void
2945
{
3046
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
31-
$file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, trim(substr($result, 14)));
32-
$dir = dirname($file);
47+
48+
$file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, trim(substr($result, strlen('File created: '))));
3349
if (is_file($file)) {
3450
unlink($file);
3551
}
36-
if (is_dir($dir)) {
52+
53+
$dir = dirname($file) . DIRECTORY_SEPARATOR;
54+
if (is_dir($dir) && ! in_array($dir, [TESTPATH, TESTPATH . 'system/', TESTPATH . '_support/'], true)) {
3755
rmdir($dir);
3856
}
3957
}
4058

41-
public function testGenerateTest(): void
59+
#[DataProvider('provideGenerateTestFiles')]
60+
public function testGenerateTestFiles(string $name, string $expectedClass): void
61+
{
62+
command(sprintf('make:test %s', $name));
63+
64+
$expectedTestFile = str_replace('/', DIRECTORY_SEPARATOR, sprintf('%stests/%s.php', ROOTPATH, $expectedClass));
65+
$expectedMessage = sprintf('File created: %s', str_replace(ROOTPATH, 'ROOTPATH' . DIRECTORY_SEPARATOR, $expectedTestFile));
66+
$this->assertStringContainsString($expectedMessage, $this->getStreamFilterBuffer());
67+
$this->assertFileExists($expectedTestFile);
68+
}
69+
70+
/**
71+
* @return iterable<string, array{0: string, 1: string}>
72+
*/
73+
public static function provideGenerateTestFiles(): iterable
4274
{
43-
command('make:test Foo/Bar');
44-
$this->assertFileExists(ROOTPATH . 'tests/Foo/Bar.php');
75+
yield 'simple class name' => ['Foo', 'FooTest'];
76+
77+
yield 'namespaced class name' => ['Foo/Bar', 'Foo/BarTest'];
78+
79+
yield 'class with suffix' => ['Foo/BarTest', 'Foo/BarTest'];
80+
81+
// the 4 slashes are needed to escape here and in the command
82+
yield 'namespace style class name' => ['Foo\\\\Bar', 'Foo/BarTest'];
4583
}
4684
}

user_guide_src/source/changelogs/v4.6.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Bugs Fixed
3737

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.
40+
- **Commands:** Fixed a bug in ``make:test`` where the generated test file would not end with ``Test.php``.
4041
- **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.
4142
- **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.
4243
- **Database:** Fixed encapsulation violation in ``BasePreparedQuery`` when accessing ``BaseConnection::transStatus`` protected property.

0 commit comments

Comments
 (0)