Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
<?php
/**
* Executes a mysql 5.5 safe truncate against all tables in a dataset.
*
* @package DbUnit
* @author Mike Lively <m@digitalsandwich.com>
* @copyright 2011 Mike Lively <m@digitalsandwich.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
* @link http://www.phpunit.de/
* @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");
}
}
<?php
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),
PHPUnit_Extensions_Database_Operation_Factory::INSERT()
));
}
//...
}
@gyardley

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");

@mlively
Owner
@MaxEvron

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

@LastMonopoly

Thanks, it works for MySQL.

@navarr

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

C:\Users\Navarr\workspace\IM\library\Zend\Db\Statement\Pdo.php:234
C:\Users\Navarr\workspace\IM\library\Zend\Db\Statement\Pdo.php:228
C:\Users\Navarr\workspace\IM\library\Zend\Db\Statement.php:300
C:\Users\Navarr\workspace\IM\library\Zend\Db\Adapter\Abstract.php:479
C:\Users\Navarr\workspace\IM\library\Zend\Db\Adapter\Pdo\Abstract.php:238
C:\Users\Navarr\workspace\IM\library\PHPUnitExtensionsDatabaseOperationMySQL55Truncate.php:20
C:\Users\Navarr\workspace\IM\tests\library\IM_Model_UserTest.php:20

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

@cxj

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.