• Download Gist
PHPUnitExtensionsDatabaseOperationMySQL55Truncate.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?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");
}
}
Test.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?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()
));
}
//...
}

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

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 <
reply@reply.github.com

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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.