I have the same problem as many other similar questions but none of the solutions worked.
I have a very simple test case:
class ExampleTest extends TestCase
{
public function test_find_english()
{
$this->withoutExceptionHandling();
$response = $this->get("api/find?id=1&language=eng");
$response->assertOk();
}
}
If I go to the endpoint in a browser or Postman I get the expected result, but if I run the test I get:
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
Symfony\Component\HttpKernel\Exception\NotFoundHttpException : GET http://localhost/api/find
I tried:
- Specifying the URL with or without a leading
/
. - With or without
api/
. - Specifying the URL in full, including the
http://
part. - Specifying the URL that works in my browser, as well as
http://localhost
andhttp://127.0.0.1
, with and without:8000
.
I also tried the above in my .env and .env.testing files, as well as the phpunit.xml file, noting that the URL that appears in the exception does not reflect any change unless I put it right in the get() method even though I ran php artisan config:cache
and php artisan cache:clear
after each change.
This is my phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<!-- <server name="BCRYPT_ROUNDS" value="4"/>-->
<!-- <server name="CACHE_DRIVER" value="array"/>-->
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<!-- <server name="MAIL_MAILER" value="array"/>-->
<!-- <server name="QUEUE_CONNECTION" value="sync"/>-->
<!-- <server name="SESSION_DRIVER" value="array"/>-->
<!-- <server name="TELESCOPE_ENABLED" value="false"/>-->
</php>
</phpunit>
And my .env.testing:
APP_NAME=MyApp
APP_ENV=testing
APP_KEY=# redacted
APP_DEBUG=true
APP_URL=http://localhost
.env is identical except for APP_ENV=local
. The problem was already there before I added .env.testing.
I am running this on a Vagrant instance (Laravel Homestead).