0

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 and http://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).

theberzi
  • 2,142
  • 3
  • 20
  • 34

1 Answers1

1

I managed to find the issue. I failed to mention that I'm runnign the tests through PHP Storm, which turned out to be a fatal detail: I thought I had configured my interpreter correctly because it was otherwise working, but I was actually using settings for a different Laravel project I have on the same instance.

If you end up here and are using an IDE to run the tests, make sure your interpreter is correct for the project. Mine was pointing to the configuration file of a different project, so of course it could never work.

theberzi
  • 2,142
  • 3
  • 20
  • 34