michalsn / codeigniter-htmx

HTMX helper library for CodeIgniter 4 framework
https://michalsn.github.io/codeigniter-htmx/
MIT License
75 stars 15 forks source link

TypeError when running Feature tests #4

Closed lonnieezell closed 1 year ago

lonnieezell commented 1 year ago

This library works great for normal use. However, when running in a feature test I end up getting the following error:

1) Tests\Blog\PagesTest::testIndex
TypeError: Michalsn\CodeIgniterHtmx\Config\Services::request(): Return value must be of type CodeIgniter\HTTP\CLIRequest|Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest, CodeIgniter\HTTP\IncomingRequest returned

/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/michalsn/codeigniter-htmx/src/Config/Services.php:29
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Config/BaseService.php:253
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Config/Services.php:285
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Config/BaseService.php:253
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Test/FeatureTestTrait.php:182
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Test/FeatureTestTrait.php:219
/Users/kilishan/WebSites/personal/learn-codeigniter/tests/Blog/PagesTest.php:32

I have changed the IncomingRequest use statement in the BaseController as recommended in the README with no change.

A simple test would be:

public function testIndex()
{
    $result = $this->get('blog');

    $result->assertOK();
    $result->assertSee('Latest Posts');
}
lonnieezell commented 1 year ago

Ah, this is an issue with CodeIgniter's FeatureTestTrait. It instantiates the IncomingRequest class directly, instead of using the Service.

https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Test/FeatureTestTrait.php#L295

Closing.