pop_up
pop_up

Reputation: 1569

symfony : can't run functional tests

Version : Symfony 3.4 phpunit 8.3

My phpunit.xml file :

<?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/4.8/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="autoload.php">
    <php>
        <ini name="error_reporting" value="-1" />
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
        <!--
            <server name="KERNEL_DIR" value="/path/to/your/app/" />
        -->
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/Bundle/*Bundle/Tests</directory>
            <directory>../src/*Bundle/Tests</directory>
        </testsuite>
    </testsuites>

    <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>

my test File :

class QuittancementControllerTest extends WebTestCase
{
    private $client;
    /** @var EntityManager */
    private $em;
    private $myApiUrl;
    /** @var BailRepository */
    private $bailRepo;
    /** @var EcritureCompteLocataireRepository */
    private $ecritureRepo;

    public function setUp(): void
    {
        $this->client = static::createClient([], [
            'PHP_AUTH_USER' => 'testUser',
            'PHP_AUTH_PW' => 'pa$$word',
        ]);
        $this->myApiUrl = $this->client->getContainer()->getParameter('myApi_url');
        $this->client->disableReboot();
        $this->em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
        $this->em->beginTransaction();
        $this->ecritureRepo = $this->em->getRepository('AppBundle:EcritureCompteLocataire');
        $this->bailRepo = $this->em->getRepository('AppBundle:Bail');
    }

    public function run(TestResult $result = null): TestResult
    {
        $this->setPreserveGlobalState(false);
        return parent::run($result);
    }

    public function tearDown(): void
    {
        $this->em->rollback();
    }

    /**
     * Vérifie que l'insertion d'une écriture à la date du jour met bien à jour le solde des écritures qui suivent
     * @runInSeparateProcess
     */
    public function testMajSoldeEcritureFuture(): void
    {
        /** @var Bail $bail */
        $bail = $this->bailRepo->findOneBy(['numeroCompteTiers' => '9000001']);
        $postData = array
        (
            'bail' => $bail->getId(),
            'test' => 'example',
        );
        $this->client->request(Request::METHOD_POST, $this->myApiUrl.'/api/test', $postData);
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
        $solde = $this->ecritureRepo->getSolde($bail->getId(), new DateTime());

        // voluntary false assertion
        $this->assertEquals(111, 10);

    }
}

If i run the test like this, i have this error :

{"code":500,"message":"Serialization of 'SimpleXMLElement' is not allowed"} C:\wamp64\www\chronos2017\src\tests\AppBundle\Controller\QuittancementControllerTest.php:44

assertion with statut 200 is write and the second assertion is false but phpunit can't display properly the result

If i delete the line "@runInSeparateProcess", the client call fails :

{"code":500,"message":"Failed to start the session because headers have already been sent by \"C:\wamp64\www\mySite\src\vendor\phpunit\phpunit\src\Util\Printer.php\" at line 109."}

What's the right way to do functional tests with symfony ? Can you help me ?

Upvotes: 0

Views: 639

Answers (1)

L01C
L01C

Reputation: 753

I use symfony 3.4 also but phpunit 7 so maybe it's because of phpunit 8, but I didn't need to make any modifications to my phpunit.xml file. And your error seems to come from your xml file according to the error.

I guess you already used documentation but if not: https://symfony.com/doc/3.4/testing.html

My file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="phpunit.xsd"
         bootstrap="tests/bootstrap.php"
         cacheResult="true"
         verbose="true">
    <testsuites>
        <testsuite name="unit">
            <directory suffix="Test.php">tests/unit</directory>
        </testsuite>

        <testsuite name="end-to-end">
            <directory suffix=".phpt">tests/end-to-end</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
            <exclude>
                <file>src/Framework/Assert/Functions.php</file>
                <file>src/Util/PHP/eval-stdin.php</file>
            </exclude>
        </whitelist>
    </filter>

    <php>
        <const name="PHPUNIT_TESTSUITE" value="true"/>
    </php>
</phpunit>

Upvotes: 0

Related Questions