functional-testingsymfony6

Symfony 6 WebTestCase return 500 or 404 response


I need your help for a basic functional test with Symfony 6.2.

I try to implement WebTestCase class to my project. Problem is, the tested route return an internal server error.

Here my test class

<?php

namespace App\Tests\Controller\Messaging;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class SentMessagesControllerTest extends WebTestCase
{
    public function testMessageSentRouteWithNoCredential(): void
    {
        $client = static::createClient();
        $url = $client->getContainer()->get('router.default')->generate('message_sent');

        $crawler = $client->request('GET', $url);

        $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
    }
}

And here what I get in the console

ErrorException: Invalid "base_uri" option: host or scheme is missing in "". in projectPath\vendor\symfony\http-client\HttpClientTrait.php:442 Stack trace: #0 projectPath\vendor\symfony\framework-bundle\Test\BrowserKitAssertionsTrait.php(38): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::assertThatForResponse(Object(Symfony\Component\HttpFoundation\Test\Constraint \ResponseStatusCodeSame), '') #1 projectPath\tests\Controller\Messaging\SentMessagesControllerTest.php(35): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::assertResponseStatusCodeSame(401) #2 projectPath\vendor\phpunit\phpunit\src\Framework\TestCase.php(1608): App\Tests\Controller\Messaging\SentMessagesControllerTest->testMessageSentRoute() #3 projectPath\vendor\phpunit\phpunit\src\Framework\TestCase.php(1214): PHPUnit\Framework\TestCase->runTest() #4 projectPath\vendor\phpunit\phpunit\src\Framework\TestResult.php(728): PHPUnit\Framework\TestCase->runBare() #5 projectPath\vendor\phpunit\phpunit\src\Framework\TestCase.php(964): PHPUnit\Framework\TestResult->run(Object(App\Tests\Controller\Messaging\SentMessagesControllerTest)) #6 projectPath\vendor\phpunit\phpunit\src\Framework\TestSuite.php(684): PHPUnit\Framework\TestCase->run(Object(PHPUnit\Framework\TestResult)) #7 projectPath\vendor\phpunit\phpunit\src\Framework\TestSuite.php(684): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #8 projectPath\vendor\phpunit\phpunit\src\Framework\TestSuite.php(684): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #9 projectPath\vendor\phpunit\phpunit\src\TextUI\TestRunner.php(651): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #10 projectPath\vendor\phpunit\phpunit\src\TextUI\Command.php(144): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\Framework\TestSuite), Array, Array, true) #11 projectPath\vendor\phpunit\phpunit\src\TextUI\Command.php(97): PHPUnit\TextUI\Command->run(Array, true) #12 projectPath\bin\phpunit(11): PHPUnit\TextUI\Command::main() #13 {main}

I've tried to configure a scoped http client, but result is the same.

Also, I've tried to set directly the full URL when creating the request, like this : $crawler = $client->request('GET', 'https://localhost/project/public/route/path');

In that case, I got a 404 response whereas the route exists and work when I paste it in a web browser.

As you can see from url, my project run under apach with no subdomain.

What to I miss ? A Symfony specific configuration ? I've spent hours crawling internet to find an answer but still don't have any clue :'(. And I'm pretty sure the solution is very simple ^^


Solution

  • Problem solved. I didn't consider the .env.test file... So I've add the missing variables and there is no problème anymore, beginner issue... ^^'