Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
* Executes a mysql 5.5 safe truncate against all tables in a dataset.
* @package DbUnit
* @author Mike Lively <>
* @copyright 2011 Mike Lively <>
* @license BSD License
* @version Release: @package_version@
* @link
* @since Class available since Release 1.0.0
class PHPUnit_Extensions_Database_Operation_MySQL55Truncate 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 @PHAKE_PREV_foreign_key_checks = foreign_key_checks");
$connection->getConnection()->query("SET foreign_key_checks = 0");
parent::execute($connection, $dataSet);
$connection->getConnection()->query("SET foreign_key_checks = @PHAKE_PREV_foreign_key_checks");
Class MyTest 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 PHPUnit_Extensions_Database_Operation_MySQL55Truncate($cascadeTruncates),

Awesome, a real lifesaver when setting up PHPUnit with Kohana.

One tweak - as is, this resulted in the following error:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'foreign_key_checks' in 'field list'

Add @@ in front of foreign_key_checks, since it's a system variable, and it works great:

$connection->getConnection()->query("SET @PHAKE_PREV_foreign_key_checks = @@foreign_key_checks");


When using DBUnit with PHPUnit, you need to overload getSetUpOperation().

Example above works with PostgresSQL database containing tables with foreign keys.

class MyTest extends PHPUnit_Extensions_Database_TestCase {

    // IMPORTANT : overload getSetUpOperation and add "TRUE" parameter to CLEAN_INSERT()
    protected function getSetUpOperation() {
        return PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT(TRUE);
        //                                                                 ⬆⬆⬆

    public function getConnection() {
        $objPDO = new PDO(); // use your own DSN here
        return $this->createDefaultDBConnection($objPDO);

    public function getDataSet() {
        return $this->createXMLDataSet('MyConfig.xml');

    public function testFirst() {
        // Your test code

    public function testSecond() {
        // Your test code

    // Etc.


Not sure it would work with MySQL


Thanks, it works for MySQL.


After implementing the original gist, I get

Zend_Db_Statement_Exception: SQLSTATE[42000]: Syntax error or access violation:1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


Where IM_Model_UserTest.php:20 ends up being PHPUnit_Extensions_Database_TestCase::setUp()


Would love to see truncate cascade support for Postgres (and MySQL) in the released version of DBUnit. The patches suggested here work for me, but it's always nice to not have to tweak vendor packages. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.