Symfony2: unit tests using sqlite

I am using phpunit with Symfony2.

I decided to use sqlite for my tests.

The problem I am facing is that foreign key constraints are ignored.

I know that I need 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 a database schema using sqlite?

Thanks!

+5
source share
1 answer

Unfortunately this is not possible. According to SQLite documentation :

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

I would suggest creating your own test case and using the 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(); } } 
+2
source

Source: https://habr.com/ru/post/1204653/


All Articles