sqlite - Symfony2 : Unit testing with sqlite

标签 sqlite symfony doctrine phpunit

我在 Symfony2 中使用 phpunit。

我决定使用 sqlite 进行测试。

我遇到的问题是外键约束被忽略了。

我知道我必须执行以下查询才能使用外键:PRAGMA foreign_keys = ON)。

我的问题是:有没有办法在使用 sqlite 创建数据库模式时始终使用外键?

谢谢!

最佳答案

不幸的是,这是不可能的。根据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.

我建议创建您自己的测试用例类并使用 setUp() 方法启用外键。

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();
  }
}

关于sqlite - Symfony2 : Unit testing with sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26363623/

相关文章:

android - 如何生成具有可变列数表的数据库?

php - Doctrine Query Builder 从表中删除所有条目

php - 将 Gaufrette Stream Wrappers 与 AsseticBundle 结合使用

mysql - 计算具有特定值的行时出现问题 - Doctrine、Symfony2、mysql

codeigniter - 自动创建日期 - 注释 - Codeigniter 2 和 Doctrine 2

python - SQL删除重复行

android - 我如何从另一个 Activity 更新数据库?

sql - SQL 中的列表枚举?

php - 如何在功能测试之前预热 Symfony 环境以检查执行时间?

symfony - 如何从 JMS 序列化器获取始终数组?