Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load fixtures one time before all phpunit test on symfony 3

I ve got a SF3 application and lot of functionnals tests. Before each tests we load and purge all fixtures. Time of all tests are so long. I would like to load fixtures just one time et truncate after last test.

Is it the good method to improve functionnal tests speed ?

Is there a php method in phpunit which is launched just one time before all tests ? (Because setUpBeforeClass is executed before each test)

An exemple of the setUpBeforeClass method in my test's classes.

class SearchRegisterControllerTest extends WebTestCase
{
    /** @var Client $client */
    private $client;

    protected static $application;

    public static function setUpBeforeClass()
    {
        $kernel = static::createKernel();
        $kernel->boot();

        $em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
        $schemaTool = new SchemaTool($em);
        $metadata = $em->getMetadataFactory()->getAllMetadata();

        $schemaTool->dropSchema($metadata);
        $schemaTool->createSchema($metadata);

        /** @var Client $client */
        $client = static::createClient();
        $em = $client->getContainer()->get('doctrine.orm.entity_manager');

        $loader = new Loader();

        $loader->loadFromDirectory('src/MyNameSpace/AppBundle/DataFixtures/ORM');

        $purger = new ORMPurger();
        $executor = new ORMExecutor($em, $purger);
        $executor->execute($loader->getFixtures(), true);

    }

Thanks in advance.

like image 561
psylo66 Avatar asked Sep 12 '17 13:09

psylo66


People also ask

What are PHPUnit fixtures?

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.

What are fixtures in Symfony?

Fixtures are used to load a "fake" set of data into a database that can then be used for testing or to help give you some interesting data while you're developing your application. This bundle is compatible with any database supported by Doctrine ORM (MySQL, PostgreSQL, SQLite, etc.).

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.


1 Answers

You could implement a test listener.

tests/StartTestSuiteListener.php

namespace App\Tests;



class StartTestSuite extends \PHPUnit_Framework_BaseTestListener
{
    public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
    {
      // do initial stuff
    }

}

Then enable enable the test listener in your phpunit.xml config:

phpunit.xml

<phpunit
 ...
>
    <listeners>
        <listener class="App\Tests\StartTestSuiteListener">
        </listener>
    </listeners>
[...]
</phpunit>

In the same manner you could implement a endTestSuite (Check for all event listed in the doc)

Hope this help

like image 129
Matteo Avatar answered Sep 27 '22 20:09

Matteo