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() | |
)); | |
} | |
//... | |
} |
Thanks for the tip. I have it on my list to push this into dbunit, but
don't quite have the setup at home to test it out.
…On Thu, Dec 15, 2011 at 5:30 AM, gyardley < ***@***.*** > wrote:
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");
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1319731
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
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()
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
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");