Created
October 27, 2011 14:41
-
-
Save mlively/1319731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() | |
)); | |
} | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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. :-)