H
H
HellWalk2020-09-01 12:40:34
symfony
HellWalk, 2020-09-01 12:40:34

Best way to load fixtures in Symfony + phpunit + codeception?

The option for loading fixtures from the symphony documentation is as follows:

php bin/console doctrine:fixtures:load


But executing this command every time before running tests is such a mess.

You can load fixtures in the test itself:

$fixtures = new ProductFixtures();
$fixtures->load($this->entityManager);


And shove it somewhere... in setUp().

But, and this option seems to me not the best. There is no better way?

For example, somehow through annotations:

/**
     * @fixture ProductFixture
     * @dataProvider invalidDateProvider
     * @param string $dateFrom
     * @param string $dateTo
     * @throws ApiException
     */
    public function testInvalidDate(string $dateFrom, string $dateTo): void


?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-09-01
@gebrak

Through annotations, it would not be the best option, because you need to pass either the ObjectManager or the Client, with which you can then get the necessary objects.
You can make a trait, which is then used in the test class.

declare(strict_types=1);

namespace App\Tests\Common;

use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\Loader;
use Symfony\Bundle\FrameworkBundle\Client;

trait LoadFixturesTrait
{
    public static function loadFixtures(Client $client, array $fixtures, bool $append=false)
    {
        $fixturesLoader = $client->getContainer()->get(Loader::class);
        $fixturesExecutor = $client->getContainer()->get(ORMExecutor::class);

        foreach ($fixtures as $fixture) {
            $fixturesLoader->addFixture(
                $client->getContainer()->get($fixture)
            );
        }

        $fixturesExecutor->execute($fixturesLoader->getFixtures(), $append);
    }
}

And in the test class:
use LoadFixturesTrait;
...
protected function setUp()
{
  SomeTest::loadFixtures(static::createClient(), [ProductsFixture::class]);
}

The array contains a list of fixtures to be applied.
If an instance of the class is used in the test method Symfony\Bundle\FrameworkBundle\Client, then this object must be passed to the above method.

B
bkosun, 2020-09-01
@bkosun

You can use Makefile to automate the Makefile workflow
:

SHELL := /bin/bash

tests:
  symfony console doctrine:fixtures:load -n
  symfony php bin/phpunit
.PHONY: tests

https://github.com/the-fast-track/book-5.0-4/blob/...
To reset the database after each test, use DoctrineTestBundle:
$ symfony composer require dama/doctrine-test-bundle --dev

phpunit.xml
...
    <extensions>
        <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
    </extensions>
...

https://github.com/the-fast-track/book-5.0-4/blob/...
Every time you want to run tests, use the make tests command:
$ make tests
https://symfony.com/doc/current /the-fast-track/en/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question