Pregunta

I'm learning to test my Symfony2 code and I'm trying to build test as should be, though, so I'm using DataFixtures to load the data and by the way I think that this test my Entities too. I'll use one test as example: CreateCompanyControllerTest.php, here is the basic code I run in every test:

private $em;

protected static $application;

public function setUp() {
    static::$kernel = static::createKernel();
    static::$kernel->boot();
    $this->em = static::$kernel->getContainer()->get('doctrine')->getManager();

    self::runCommand('doctrine:schema:update --force');

    $loader = new Loader();
    $loader->addFixture(new LoadCompanyData());

    $purger = new ORMPurger();
    $executor = new ORMExecutor($this->em, $purger);
    $executor->execute($loader->getFixtures());
}

protected static function runCommand($command) {
    $command = sprintf('%s --quiet', $command);

    return self::getApplication()->run(new StringInput($command));
}

protected static function getApplication() {
    if (null === self::$application) {
        $client = static::createClient();

        self::$application = new Application($client->getKernel());
        self::$application->setAutoExit(false);
    }

    return self::$application;
}

I don't know if is right all the time to run the command doctrine:schema:update --force since it cleanup my DB complete and this is my first doubt. Now regarding this same problem in LoadCompanyData.php I have some entities dependencies (see code below:

 $mediaType = $manager->getRepository('MediaBundle:NMediaType')->find(1);
 $mediaStatus = $manager->getRepository('MediaBundle:NMediaStatus')->find(1);

But since I run the command doctrine:schema:update --force my DB got cleaned and then the test fails with this message:

1) Company\RegisterCompanyBundle\Tests\Controller\CreateCompanyControllerTest::testcreateCompanyAction Argument 1 passed to Wuelto\Common\MediaBundle\Entity\Media::setType() must be an instance of Common\MediaBundle\Entity\NMediaType, null given, called in /var/www/html/kraken/src/Company/RegisterCompanyBundle/DataFixtures/ORM/LoadCompanyData.php on line 46 and defined

And it's right because that table is empty. Then knowing this:

  1. Is there any way to avoid this?
  2. Should I create a fixture for any dependent entity and then call where I need? IN this case any example will be fine since I don't know how to.
  3. I know that getOrder() in Data Fixtures set the order in which fixtures will be loaded but, how I use it? I mean for example before load the data for the Company I should first add the data for dependent entities, any advice on this?
¿Fue útil?

Solución

It's good practice to clean your test db all the time, your test should be self contained and not rely on anything left over in the db from a previous test.

That being said, yes, you should also set up fixtures or helper function that insert whatever necessary data you need in your test db.

You can do this setup once for each test file. DB tests are expensive, time wise, so if you can get away with mocking calls to db you should do that, but it all depends on what you are trying to test.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top