I'd still like to retain the truncate operation in the SimpleTester and the workaround I'm using ATM is rather cumbersome.
I have subclassed SimpleTester with the following:
class My_Test_PHPUnit_Db_SimpleTester extends Zend_Test_PHPUnit_Db_SimpleTester {
public function __construct(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection) {
parent::__construct($connection);
$this->setUpOperation = new PHPUnit_Extensions_Database_Operation_Composite(array(
new My_Test_PHPUnit_Db_Operation_Truncate(),
new Zend_Test_PHPUnit_Db_Operation_Insert(),
));
}
}
And also the Truncate operation:
class My_Test_PHPUnit_Db_Operation_Truncate extends Zend_Test_PHPUnit_Db_Operation_Truncate {
protected function _truncate(Zend_Db_Adapter_Abstract $db, $tableName) {
// can't pass this through because when quoteIdentifier is called without
// the second argument then quoting always happens => breakage
// return parent::_truncate($db, $db->quoteIdentifier($tableName, true));
$tableName = $db->quoteIdentifier($tableName, true);
if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) {
$db->query('DELETE FROM '.$tableName);
} else if($db instanceof Zend_Db_Adapter_Db2) {
if(strstr(PHP_OS, "WIN")) {
$file = tempnam(sys_get_temp_dir(), "zendtestdbibm_");
file_put_contents($file, "");
$db->query('IMPORT FROM '.$file.' OF DEL REPLACE INTO '.$tableName);
unlink($file);
} else {
$db->query('IMPORT FROM /dev/null OF DEL REPLACE INTO '.$tableName);
}
} else if($this->isMssqlOrOracle($db)) {
$db->query('TRUNCATE TABLE '.$tableName);
} else {
$db->query('TRUNCATE '.$tableName);
}
}
private function isMssqlOrOracle($db) {
return (
$db instanceof Zend_Db_Adapter_Pdo_Mssql ||
$db instanceof Zend_Db_Adapter_Sqlsrv ||
$db instanceof Zend_Db_Adapter_Pdo_Oci ||
$db instanceof Zend_Db_Adapter_Oracle
);
}
}
Here is the patch to ZF 1.9.5 to allow this to work (can't upgrade to ZF 10.x due to project priorities):
Index: php/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php
===================================================================
— php/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php (revision 22559)
+++ php/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php (working copy)
@@ -77,7 +77,7 @@
*/
protected function _truncate(Zend_Db_Adapter_Abstract $db, $tableName)
{
- $tableName = $db->quoteIdentifier($tableName);
+ $tableName = $db->quoteIdentifier($tableName, true);
if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) {
$db->query('DELETE FROM '.$tableName);
} else if($db instanceof Zend_Db_Adapter_Db2) {
@@ -111,4 +111,4 @@
$db instanceof Zend_Db_Adapter_Oracle
);
}
-}
\ No newline at end of file
+}
Unfortunately this system munges the formatting.
I'd still like to retain the truncate operation in the SimpleTester and the workaround I'm using ATM is rather cumbersome.
I have subclassed SimpleTester with the following:
class My_Test_PHPUnit_Db_SimpleTester extends Zend_Test_PHPUnit_Db_SimpleTester {
public function __construct(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection) { parent::__construct($connection); $this->setUpOperation = new PHPUnit_Extensions_Database_Operation_Composite(array( new My_Test_PHPUnit_Db_Operation_Truncate(), new Zend_Test_PHPUnit_Db_Operation_Insert(), )); }
}
And also the Truncate operation:
class My_Test_PHPUnit_Db_Operation_Truncate extends Zend_Test_PHPUnit_Db_Operation_Truncate {
protected function _truncate(Zend_Db_Adapter_Abstract $db, $tableName) {
// can't pass this through because when quoteIdentifier is called without
// the second argument then quoting always happens => breakage
// return parent::_truncate($db, $db->quoteIdentifier($tableName, true));
$tableName = $db->quoteIdentifier($tableName, true);
if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) { $db->query('DELETE FROM '.$tableName); } else if($db instanceof Zend_Db_Adapter_Db2) {
if(strstr(PHP_OS, "WIN")) { $file = tempnam(sys_get_temp_dir(), "zendtestdbibm_"); file_put_contents($file, ""); $db->query('IMPORT FROM '.$file.' OF DEL REPLACE INTO '.$tableName); unlink($file); } else { $db->query('IMPORT FROM /dev/null OF DEL REPLACE INTO '.$tableName); }
} else if($this->isMssqlOrOracle($db)) { $db->query('TRUNCATE TABLE '.$tableName); } else { $db->query('TRUNCATE '.$tableName); }
}
private function isMssqlOrOracle($db) { return ( $db instanceof Zend_Db_Adapter_Pdo_Mssql || $db instanceof Zend_Db_Adapter_Sqlsrv || $db instanceof Zend_Db_Adapter_Pdo_Oci || $db instanceof Zend_Db_Adapter_Oracle ); }
}
Here is the patch to ZF 1.9.5 to allow this to work (can't upgrade to ZF 10.x due to project priorities):
Index: php/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php
===================================================================
— php/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php (revision 22559)
+++ php/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php (working copy)
@@ -77,7 +77,7 @@
*/
protected function _truncate(Zend_Db_Adapter_Abstract $db, $tableName)
{
+ $tableName = $db->quoteIdentifier($tableName, true);
if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) { $db->query('DELETE FROM '.$tableName); } else if($db instanceof Zend_Db_Adapter_Db2) { @@ -111,4 +111,4 @@ $db instanceof Zend_Db_Adapter_Oracle ); }
-}
\ No newline at end of file
+}
Unfortunately this system munges the formatting.
- $tableName = $db->quoteIdentifier($tableName);
+ $tableName = $db->quoteIdentifier($tableName, true);
if($db instanceof Zend_Db_Adapter_Pdo_Sqlite) {
$db->query('DELETE FROM '.$tableName);
} else if($db instanceof Zend_Db_Adapter_Db2) {
@@ -111,4 +111,4 @@
$db instanceof Zend_Db_Adapter_Oracle
);
}
-}
\ No newline at end of file
+}
Unfortunately this system munges the formatting.