Search code examples
symfony4functional-testingphpunit

Inncorect request URL PHPUnit functional test Symfony 4.2


I can not run the functional test I still get info:

The /blog public URL loads correctly. Failed asserting that 500 is identical to 200.

As I checked with debug $client-> request('GET', $url) creates the address http://localhost/blog is incorrect page address. The correct page address is http: //project.local/blog or http://localhost/project/public/blog

  class PagesControllerTest extends WebTestCase
    {
        public function testShowPost()
        {
            $client = static::createClient();
            $client->request('GET', '/blog');
            $this->assertEquals(200, $client->getResponse()->getStatusCode());
        }

phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="KERNEL_CLASS" value="App\Kernel" />
        <server name="HOSTNAME" value="http://project.local" />
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="/foobar/" />
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>src</directory>
        </whitelist>
    </filter>

    <listeners>
        <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
        <listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitListener" />
    </listeners>
</phpunit>

Solution

  • You can create .env.test this file will be loaded after .env file and override environment variables for your test environment.

    DATABASE_URL=sqlite:///%kernel.project_dir%/data/database_test.sqlite