getting uri_string() to work with Controller tests (phpunit) |
I cannot make uri_string() and site_url() functions work within controllers when run through phpunit.
For example, in a freshly installed composer project I add this route (in app/Config/Routes.php): PHP Code: $routes->get('checkup/first/(:segment)', 'Checkup::first/$1'); This controller (app/Controllers/Checkup.php): PHP Code: <?php And this test (tests/app/Controllers/CheckupTest.php) PHP Code: <?php then I run PHP Code: vendor/bin/phpunit I wrote in comments where the functions go wrong in the test (the test should fail, but it succeeds). Am I missing something, or is this a bug?
==
Donatas G.
The following test passes.
PHP Code: public function testFirst() (08-08-2023, 11:03 PM)kenjis Wrote: The following test passes. mhm, not really. Actually phpunit does not count $result->see() as assertions, so when I run vendor/bin/phpunit --filter CheckupTest the test does pass, but reports only one assertion. And whatever I write in the $result->see('') argument, nothing changes, the tests pass. And once I enclose the $result->see() within $this->assertTrue(), the tests fail (but report correct number of assertions).
==
Donatas G.
Oh, I thought see() was an assertion method.
https://codeigniter4.github.io/CodeIgnit...om-helpers But it just returns bool. The documentation is comfusing. You are correct. The output is: Code: uri_string() returns: ; Segment after first: second; current_url() returns: http://example.com/index.php (08-09-2023, 01:12 PM)kenjis Wrote: Oh, I thought see() was an assertion method. Yep, that is what I thought as well. A unit testing tutorial, like there is with forms or validation, would be really great to have. However, when I started implementing what I was reading, I did make progress through trial and error and a lot of additional reading. Anyway, any ideas why uri_string would not work here, with controller tests? It does seem to work in integration tests (in a self-contained example), although I do seem to be loosing the last fragment when doing integration tests in my app. Need to investigate that as well...
==
Donatas G.
I sent a PR to improve the docs: https://github.com/codeigniter4/CodeIgniter4/pull/7804
uri_string() has hidden dependencies for getting the current URI. But Controller tests do not setup the dependencies correctly. That's why uri_string() does not work in testing. The current URI handling in the framework is very complex. It depends a lot of things. So probably to fix it is not easy. I'm trying to clean up the process in 4.4 branch: https://github.com/codeigniter4/CodeIgniter4/pull/7282 See the tests would pass: https://github.com/codeigniter4/CodeIgni...112ea2c025
Try this:
PHP Code: public function testFirst() |
Welcome Guest, Not a member yet? Register Sign In |