Class not found only when using PHPUnit

I am working on testing using the Symfony2 application (2.7.3), and the page controller does not load the class only when the request is sent from PHPUnit (4.8.6).

The test is as follows:

//AppBundle/Tests/Controller/PagesAvailableTest.php

<?php

namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PagesAvailableTest extends WebTestCase
{
    public function testpages()
    {
        $client = static::createClient();
        $client->request('GET', '/contact'); // Throws the error
    }
}

and throws this error when starting with $ phpunit -c app/:

PHP Fatal error: Class 'AppBundle \ Entity \ ContactMessage' not found in SymfonyRoot /src/AppBundle/Controller/ContactController.php on line 28

When called through a browser, the page loads as expected. When called with $ curl -I http://localhost/contact, page statusHTTP/1.1 200 OK

, , Symfony docs , :

- bootstrap.php.cache( app/phpunit.xml.dist).

<!-- app/phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="bootstrap.php.cache"
>
    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/Bundle/*Bundle/Tests</directory>
            <directory>../src/*Bundle/Tests</directory>
        </testsuite>
    </testsuites>

    <!--<php>-->
        <!--In my experimentation, I uncommented this and tinkered with 
            a few values. After having no luck, I commented it out again. -->
        <!--<server name="KERNEL_DIR" value="." />-->
    <!--</php>-->

    <filter>
        <whitelist>
            <directory>../src</directory>
            <exclude>
                <directory>../src/*Bundle/Resources</directory>
                <directory>../src/*Bundle/Tests</directory>
                <directory>../src/*/*Bundle/Resources</directory>
                <directory>../src/*/*Bundle/Tests</directory>
                <directory>../src/*/Bundle/*Bundle/Resources</directory>
                <directory>../src/*/Bundle/*Bundle/Tests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

PHPUnit?

+4
1

: PHP-, PATH.

$ phpunit -c app/, OS (5.5.20) - PHP 5.6.10, MAMP.

, PHP PHPUnit:

$ /path/to/correct/php phpunit -c app/

+6

Source: https://habr.com/ru/post/1605632/


All Articles