How to load datafixtures hautelook alice in phpunit symfony?

2.6k Views Asked by At

I try to add this code in my DefaultControllerTest

$load = new Loader();
$load->load('src/AppBundle/DataFixtures/ORM/product.yml');

and here is the complete code of my controller

<?php

namespace Tests\AppBundle\Controller;

use Nelmio\Alice\Fixtures\Loader;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DefaultControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $load = new Loader();
        $load->load('src/AppBundle/DataFixtures/ORM/product.yml');
        $client = static::createClient();

        $crawler = $client->request('GET', '/');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertContains('Welcome to Symfony', $crawler->filter('#container h1')->text());
    }
}

If I run phpunit. It works and no errors found. It successfully tested but the problem here the product.yml doesn't insert any data in my database. But If I run this command bin/console hautelook_alice:doctrine:fixtures:load --append. This will works. It insert the data. How can I load the datafixture before I test the controller? I try to research more about it. but I have no clue on how to add it now.

1

There are 1 best solutions below

4
Tokeeen.com On

You can simply use a bash script. Something like this (adapt to your needs):

#!/bin/bash
echo "# Refresh data model and load fixtures"
php bin/console doctrine:database:create --if-not-exists --env=dev
php bin/console doctrine:schema:drop --force --env=dev
php bin/console doctrine:schema:create --env=dev
php bin/console doctrine:schema:validate --env=dev
php bin/console hautelook_alice:doctrine:fixtures:load --append --env=dev
echo -e " --> DONE\n"

Then you can launch phpunit that will use this fresh database. Or you could just add the phpunit call in this script:

./bin/simple-phpunit --debug --verbose