Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 : Unit testing with sqlite

I'm using phpunit with Symfony2.

I decided to use sqlite for my tests.

The issue I'm having is that the foreign keys constraints are ignored.

I know I have to execute the following query in order to use foreign keys : PRAGMA foreign_keys = ON).

My question is : is there a way to always use foreign keys when creating the database schema with sqlite ?

Thanks !

like image 307
Brice Avatar asked Oct 14 '14 14:10

Brice


1 Answers

Unfortunately it is impossible. Accordingly to SQLite documentation:

Assuming the library is compiled with foreign key constraints enabled, it must still be enabled by the application at runtime, using the PRAGMA foreign_keys command.

I would suggest to create your own test case class and use setUp() method to enable foreign keys.

class SQLiteTestCase extends \PHPUnit_Framework_TestCase
{
  protected function setUp()
  {
    parent::setUp();
    // Here add your code to enable foreign keys
  }
}

class MyTest extends SQLiteTestCase
{
  protected function setUp()
  {
    // Setup your test data-set here
    parent::setUp();
  }
}
like image 116
Aleksander Wons Avatar answered Oct 02 '22 12:10

Aleksander Wons