Symfony. Improving your tests with DoctrineFixturesBundle

Create fake data for your tests with the DoctrineFixturesBundle bundle

Installation

composer require --dev orm-fixtures
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true]

Writing our first Fixture

Image for post
Image for post
bin/console doctrine:fixtures:load

How to create dependent Fixtures

Image for post
Image for post
Image for post
Image for post

How to access services within a Fixture

public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}

Integrating fixtures with PHPUnit

bin/console doctrine:fixtures:load

LiipTestFixturesBundle

composer require --dev liip/test-fixtures-bundle:^1.0.0
use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyControllerTest extends WebTestCase
{
use FixturesTrait;

public function testIndex()
{
// add all your fixtures classes that implement
// Doctrine\Common\DataFixtures\FixtureInterface
$this->loadFixtures(array(
'App\DataFixtures\PostFixtures',
'App\DataFixtures\ProjectFixtures'
));


...

Other solution

Image for post
Image for post
class DeletePostControllerTest extends AuthenticatedClientWebTestCase {protected function setUp() {    $kernel = self::bootKernel();    $this->addFixture(new PostFixtures());    $this->executeFixtures();}

In summary…

¿Quieres recibir más artículos como este?

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store