I'm trying to run some basic unit tests for my Laravel application with PHPUnit.
However, all HTTP requests return a 404 not found HTTP status code, so we are failing at the first hurdle.
After trawling the internet, I have found information about how to remedy this, but so far none of these actually work:
- Checking I've referenced the named route correctly.
- Checking that this route has a controller method.
- Checking that it works in the browser.
- I've ensured that my application url is set correctly in all the right places:
TestCase::$baseUrl
,Config->app->url
...Its set tohttp://localhost/myappfolder
- I've tried different methods from within my unit test methods to get an HTTP response object. So far all result in a 404:
$this->route()
,$this->action()
,$this->call()
. - I've tried removing all references to route protection Middleware from my controllers. I believe that it is disabled when running unit tests, but I've done this as I'm pulling my proverbial hair out trying to diagnose this problem. No effect.
- My application's roots file has several PHP
require
s that includes smaller route files. Taylor Otwell has said previously that a routes file like this, but containingrequire_once
can cause these 404 errors. He suggests changing torequire
. Some developers report that this fixes it for them, but mine have just beenrequire
all along. I have also tried moving routes back into the main routes file anyway, but this has not worked either.
Investigations have been usually unusual:
- If I edit the
route()
method withinIlluminate\Foundation\Testing\Concerns
to print out the URL it generates, it is in fact perfectly formed:http://localhost/myprojectfolder/route-url-text
. Visiting this page in the browser works fine. Visiting it with cURL works too. Running$this->call()
on the URL thats been generated, and then running PHPUnit shows the 404 again. So its just PHPUnit doing something weird.
Here is the code I'm using in the unit test script:
public function testThis()
{
$response = $this->route('GET', 'myRouteName', [
'myParam' => 5
]);
dd($response->status()); // 404 - always!
}
Its like there is something stepping in and explicitly throwing a 404...Would really appreciate if someone could shed light on this.