Answer the question
In order to leave comments, you need to log in
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
$fixtures = new ProductFixtures();
$fixtures->load($this->entityManager);
/**
* @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
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);
}
}
use LoadFixturesTrait;
...
protected function setUp()
{
SomeTest::loadFixtures(static::createClient(), [ProductsFixture::class]);
}
Symfony\Bundle\FrameworkBundle\Client
, then this object must be passed to the above method.
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
$ symfony composer require dama/doctrine-test-bundle --dev
...
<extensions>
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>
...
$ make tests
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question