Symfony. Mejorando tus tests con DoctrineFixturesBundle

Crea datos de prueba para tus tests con el bundle DoctrineFixturesBundle

Instalación

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

Escribiendo nuestra primera Fixture

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

Cómo crear Fixtures dependientes

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

Cómo acceder a servicios dentro de una Fixture

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

Integrando las fixtures con 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'
));


...

Solución de andar por casa

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

En resumen…

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

Written by

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app