fix: handle namespaced helper found on Common helper

This commit is contained in:
Abdul Malik Ikhsan 2024-12-30 20:59:00 +07:00
parent c0c3e02e8a
commit f551dbd0ad
No known key found for this signature in database
GPG Key ID: 69AC5BC354C89BE6
2 changed files with 32 additions and 1 deletions

View File

@ -598,7 +598,7 @@ if (! function_exists('helper')) {
if (str_contains($filename, '\\')) {
$path = $loader->locateFile($filename, 'Helpers');
if ($path !== '') {
if ($path === false) {
throw FileNotFoundException::forFileNotFound($filename);
}

View File

@ -13,7 +13,9 @@ declare(strict_types=1);
namespace CodeIgniter;
use CodeIgniter\Autoloader\Autoloader;
use CodeIgniter\Autoloader\FileLocator;
use CodeIgniter\Files\Exceptions\FileNotFoundException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;
use PHPUnit\Framework\Attributes\CoversFunction;
@ -148,4 +150,33 @@ final class CommonHelperTest extends CIUnitTestCase
$this->assertSame($this->dummyHelpers[0], foo_bar_baz());
}
public function testNamespacedHelperNotFound(): void
{
$this->expectException(FileNotFoundException::class);
$locator = $this->getMockLocator();
Services::injectMock('locator', $locator);
helper('foo\barbaz');
}
public function testNamespacedHelperFound(): void
{
$autoloader = new Autoloader();
$autoloader->addNamespace('Tests\Support\Helpers', TESTPATH . '_support/Helpers');
$locator = new FileLocator($autoloader);
Services::injectMock('locator', $locator);
$found = true;
try {
helper('Tests\Support\Helpers\baguette');
} catch (FileNotFoundException) {
$found = false;
}
$this->assertTrue($found);
}
}