PHPUnit and MySQL truncation error(PHPUnit 和 MySQL 截断错误)
问题描述
I am getting a headache with PHPUnit's behavior of always running TRUNCATE
prior to inserting fixtures without first setting foreign key checks off:
Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint
Basically, PHPUnit tries to truncate a table before it inserts fixtures. How do I tell it to SET FOREIGN_KEY_CHECKS=0;
?
I found the answer it seems. I ended up overriding some methods by extending a class.
<?php
/**
* Disables foreign key checks temporarily.
*/
class TruncateOperation extends PHPUnit_Extensions_Database_Operation_Truncate
{
public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
{
$connection->getConnection()->query("SET foreign_key_checks = 0");
parent::execute($connection, $dataSet);
$connection->getConnection()->query("SET foreign_key_checks = 1");
}
}
Then example usage:
class FooTest extends PHPUnit_Extensions_Database_TestCase
{
public function getSetUpOperation()
{
$cascadeTruncates = true; // If you want cascading truncates, false otherwise. If unsure choose false.
return new PHPUnit_Extensions_Database_Operation_Composite(array(
new TruncateOperation($cascadeTruncates),
PHPUnit_Extensions_Database_Operation_Factory::INSERT()
));
}
}
So I'm effectively disabling foreign key checks and setting them back if they were ever set. Obviously you should make a base class that has this functionality and you extend it rather than PHPUnit's TestCase.
这篇关于PHPUnit 和 MySQL 截断错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHPUnit 和 MySQL 截断错误


基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01