Symfony2 tests: Issue with fixtures and foreign keys

Guido Kritz

I'm developing a project with Symfony and Doctrine and I have an issue with my tests. I'm using Fixtures to load data for the tests, and some of my entities have associations between them. The first time I run my tests with empty DB tables the tests succeed flawlessly. But when I run the test once again, I got an error:

Doctrine\DBAL\DBALException: An exception occurred while executing 'DELETE FROM Ent':

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (my_db.Ent, CONSTRAINT FK_FE5D1D1E727ACA70 FOREIGN KEY (parent_id) REFERENCES Ent (id))

The reason for the error is obvious: a row cannot be deleted when another row is referencing it though a foreign key.

If I manually truncate the table disabling foreign key constrains first and run the test again with an empty table, the tests are successful once again. But I have to repeat this every time I run the tests.

So the question is: is there a way to make Symfony or PHPUnit handle this automatically?

Thanks!

EDIT: I forgot to mention I'm using the Liip\FunctionalTestBundle\Test\WebTestCase class.

Guido Kritz

I ended up extending the WebTestCase as suggested by Nextar. Thanks!

This is the code:

<?php

namespace Acme\MyBundle\Tests\Controller;

use Liip\FunctionalTestBundle\Test\WebTestCase;

class MyWebTestCase extends WebTestCase {

  protected function loadFixtures(array $classNames, $omName = null, $registryName = 'doctrine', $purgeMode = null) {
    $this->getContainer()->get('doctrine')->getManager()->getConnection()->query(sprintf('SET FOREIGN_KEY_CHECKS=0'));
    $result = parent::loadFixtures($classNames, $omName, $registryName, $purgeMode);
    $this->getContainer()->get('doctrine')->getManager()->getConnection()->query(sprintf('SET FOREIGN_KEY_CHECKS=1'));
    return $result;
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails loading fixtures with wrong foreign keys

From Dev

Label references with custom foreign keys in rails fixtures

From Dev

Best practice for inserting objects with foreign keys in symfony2

From Dev

Speedup symfony2 fixtures loading in tests with security.encoder_factory

From Dev

Issue with Laravel Migrations and Foreign Keys

From Dev

Issue with foreign keys in sql data modeler

From Dev

Symfony2 UrlGenerator in tests

From Dev

Query a table with 2 foreign keys

From Dev

symfony2: trying to load fixtures using hautelook/alicebundle

From Dev

Symfony2 execute SQL file in Doctrine Fixtures Load

From Dev

Symfony2: Functional testing is failing during Fixtures load

From Dev

django "use_natural_foreign_keys=True" issue

From Dev

Django update MySQL database issue involving foreign keys

From Dev

Django tests with selenium not loading fixtures

From Dev

Independent functional tests with LiipFunctionalTestBundle and fixtures?

From Dev

Symfony2 access private services in tests

From Dev

phpunit in symfony2 - No tests executed

From Dev

User token in Symfony2 Functional Tests

From Dev

Symfony2 - Locale in unit tests

From Dev

Select from 2 tables with 2 foreign keys

From Dev

Select from 2 tables with 2 foreign keys

From Dev

Symfony2 logout issue2

From Dev

Symfony2 routing unsupported keys

From Dev

Symfony2 routing unsupported keys

From Dev

LINQ to Entities .Contains for a tuple (2 foreign keys)

From Dev

EF is creating 2 foreign keys for navigation property

From Dev

CakePHP 2 Model relations multiple foreign Keys

From Dev

3 Tables, 2 Foreign Keys, Sum by month

From Dev

Foreign Keys in Web API 2 - Best practices?

Related Related

HotTag

Archive