1

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.

user3818576
  • 2,979
  • 8
  • 37
  • 62

1 Answers1

1

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
Tokeeen.com
  • 718
  • 7
  • 19
  • thanks for this. Is there another way to load it in my webtestcase? – user3818576 Nov 13 '17 at 03:36
  • what is --env=dev? – user3818576 Nov 13 '17 at 06:24
  • 1
    "env" is the current working environment, so when developing you generally want to use "dev". On the other when deploying in a production server server, you will launch the commands using env=prod. (prod stands for production which is an env optimized and without debug informations) – Tokeeen.com Nov 13 '17 at 08:52
  • 1
    To load directly in the test file: https://stackoverflow.com/questions/14752930/best-way-to-create-a-test-database-and-load-fixtures-on-symfony-2-webtestcase?rq=1 – Tokeeen.com Nov 13 '17 at 08:54