فهرست منبع

ZF-8362 - Added checks for existing connections and profiler to stop db testsuite from fataling when problems occour.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19102 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei 16 سال پیش
والد
کامیت
77b71c11d2
3فایلهای تغییر یافته به همراه49 افزوده شده و 29 حذف شده
  1. 3 1
      tests/Zend/Db/Profiler/TestCommon.php
  2. 43 25
      tests/Zend/Db/TestUtil/Common.php
  3. 3 3
      tests/Zend/Db/TestUtil/Mysqli.php

+ 3 - 1
tests/Zend/Db/Profiler/TestCommon.php

@@ -49,7 +49,9 @@ abstract class Zend_Db_Profiler_TestCommon extends Zend_Db_TestSetup
 
     public function tearDown()
     {
-        $this->_db->getProfiler()->setEnabled(false);
+        if($this->_db instanceof Zend_Db_Adapter_Abstract) {
+            $this->_db->getProfiler()->setEnabled(false);
+        }
         parent::tearDown();
     }
 

+ 43 - 25
tests/Zend/Db/TestUtil/Common.php

@@ -61,7 +61,7 @@ abstract class Zend_Db_TestUtil_Common
 
     protected function _getSqlCreateTable($tableName)
     {
-        return 'CREATE TABLE ' . $this->_db->quoteIdentifier($tableName, true);
+        return 'CREATE TABLE ' . $this->getAdapter()->quoteIdentifier($tableName, true);
     }
 
     protected function _getSqlCreateTableType()
@@ -71,7 +71,7 @@ abstract class Zend_Db_TestUtil_Common
 
     protected function _getSqlDropTable($tableName)
     {
-        return 'DROP TABLE ' . $this->_db->quoteIdentifier($tableName, true);
+        return 'DROP TABLE ' . $this->getAdapter()->quoteIdentifier($tableName, true);
     }
 
     public function getSqlType($type)
@@ -102,13 +102,13 @@ abstract class Zend_Db_TestUtil_Common
             $pKey = $columns['PRIMARY KEY'];
             unset($columns['PRIMARY KEY']);
             foreach (explode(',', $pKey) as $pKeyCol) {
-                $pKeys[] = $this->_db->quoteIdentifier($pKeyCol, true);
+                $pKeys[] = $this->getAdapter()->quoteIdentifier($pKeyCol, true);
             }
             $pKey = implode(', ', $pKeys);
         }
 
         foreach ($columns as $columnName => $type) {
-            $col[] = $this->_db->quoteIdentifier($columnName, true) . ' ' . $this->getSqlType($type);
+            $col[] = $this->getAdapter()->quoteIdentifier($columnName, true) . ' ' . $this->getSqlType($type);
         }
 
         if ($pKey) {
@@ -117,9 +117,9 @@ abstract class Zend_Db_TestUtil_Common
 
         $sql .= implode(",\n\t", $col);
         $sql .= "\n)" . $this->_getSqlCreateTableType();
-        $result = $this->_rawQuery($sql);
+        $result = $this->_tryRawQuery($sql);
         if ($result === false) {
-            throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
+            throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
         }
         $this->_tables[$tableName] = true;
     }
@@ -137,9 +137,9 @@ abstract class Zend_Db_TestUtil_Common
         if (!$sql) {
             return;
         }
-        $result = $this->_rawQuery($sql);
+        $result = $this->_tryRawQuery($sql);
         if ($result === false) {
-            throw new Zend_Db_Exception("DROP TABLE statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
+            throw new Zend_Db_Exception("DROP TABLE statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
         }
         unset($this->_tables[$tableName]);
     }
@@ -164,9 +164,9 @@ abstract class Zend_Db_TestUtil_Common
         if (!$sql) {
             return;
         }
-        $result = $this->_rawQuery($sql);
+        $result = $this->_tryRawQuery($sql);
         if ($result === false) {
-            throw new Zend_Db_Exception("CREATE SEQUENCE statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
+            throw new Zend_Db_Exception("CREATE SEQUENCE statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
         }
         $this->_sequences[$sequenceName] = true;
     }
@@ -184,9 +184,9 @@ abstract class Zend_Db_TestUtil_Common
         if (!$sql) {
             return;
         }
-        $result = $this->_rawQuery($sql);
+        $result = $this->_tryRawQuery($sql);
         if ($result === false) {
-            throw new Zend_Db_Exception("DROP SEQUENCE statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
+            throw new Zend_Db_Exception("DROP SEQUENCE statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
         }
         unset($this->_sequences[$sequenceName]);
     }
@@ -417,44 +417,44 @@ abstract class Zend_Db_TestUtil_Common
         $tableName = $this->getTableName($tableId);
         $data = $this->{'_getData'.$tableId}();
         foreach ($data as $row) {
-            $sql = 'INSERT INTO ' .  $this->_db->quoteIdentifier($tableName, true);
+            $sql = 'INSERT INTO ' .  $this->getAdapter()->quoteIdentifier($tableName, true);
             $cols = array();
             $vals = array();
             foreach ($row as $col => $val) {
-                $cols[] = $this->_db->quoteIdentifier($col, true);
+                $cols[] = $this->getAdapter()->quoteIdentifier($col, true);
                 if ($val instanceof Zend_Db_Expr) {
                     $vals[] = $val->__toString();
                 } else {
-                    $vals[] = $this->_db->quote($val);
+                    $vals[] = $this->getAdapter()->quote($val);
                 }
             }
             $sql .=        ' (' . implode(', ', $cols) . ')';
             $sql .= ' VALUES (' . implode(', ', $vals) . ')';
-            $result = $this->_rawQuery($sql);
+            $result = $this->_tryRawQuery($sql);
             if ($result === false) {
-                throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
+                throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
             }
         }
     }
 
     protected function _getSqlCreateView($viewName)
     {
-        return 'CREATE VIEW ' . $this->_db->quoteIdentifier($viewName, true);
+        return 'CREATE VIEW ' . $this->getAdapter()->quoteIdentifier($viewName, true);
     }
 
     protected function _getSqlDropView($viewName)
     {
-        return 'DROP VIEW ' . $this->_db->quoteIdentifier($viewName, true);
+        return 'DROP VIEW ' . $this->getAdapter()->quoteIdentifier($viewName, true);
     }
 
     public function createView()
     {
         $sql = $this->_getSqlCreateView('temp_view')
              . ' AS SELECT * FROM '
-             . $this->_db->quoteIdentifier('zfbugs', true);
-        $result = $this->_rawQuery($sql);
+             . $this->getAdapter()->quoteIdentifier('zfbugs', true);
+        $result = $this->_tryRawQuery($sql);
         if ($result === false) {
-            throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
+            throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
         }
     }
 
@@ -464,9 +464,9 @@ abstract class Zend_Db_TestUtil_Common
         if (!$sql) {
             return;
         }
-        $result = $this->_rawQuery($sql);
+        $result = $this->_tryRawQuery($sql);
         if ($result === false) {
-            throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
+            throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->getAdapter()->getConnection()->error);
         }
     }
 
@@ -500,12 +500,30 @@ abstract class Zend_Db_TestUtil_Common
         $this->_db = $db;
     }
 
+    protected function getAdapter()
+    {
+        if($this->_db == null) {
+            require_once "Zend/Db/Exception.php";
+            throw new Zend_Db_Exception("No adapter was set in TestUtils.");
+        }
+        return $this->_db;
+    }
+
     public function tearDown()
     {
         $this->dropView();
         $this->dropTable();
         $this->dropSequence();
-        $this->_db->closeConnection();
+        $this->getAdapter()->closeConnection();
+    }
+
+    protected function _tryRawQuery($sql)
+    {
+        if($this->_db == null) {
+            require_once "Zend/Db/Exception.php";
+            throw new Zend_Db_Exception("No database adapter set.");
+        }
+        $this->_rawQuery($sql);
     }
 
     protected abstract function _rawQuery($sql);

+ 3 - 3
tests/Zend/Db/TestUtil/Mysqli.php

@@ -135,13 +135,13 @@ class Zend_Db_TestUtil_Mysqli extends Zend_Db_TestUtil_Common
 
     protected function _createTestProcedure()
     {
-        $this->_rawQuery('DROP PROCEDURE IF EXISTS zf_test_procedure');
-        $this->_rawQuery('CREATE PROCEDURE zf_test_procedure(IN param1 INTEGER) BEGIN SELECT * FROM zfproducts WHERE product_id = param1; END');
+        $this->_tryRawQuery('DROP PROCEDURE IF EXISTS zf_test_procedure');
+        $this->_tryRawQuery('CREATE PROCEDURE zf_test_procedure(IN param1 INTEGER) BEGIN SELECT * FROM zfproducts WHERE product_id = param1; END');
     }
 
     protected function _dropTestProcedure()
     {
-        $this->_rawQuery('DROP PROCEDURE IF EXISTS zf_test_procedure');
+        $this->_tryRawQuery('DROP PROCEDURE IF EXISTS zf_test_procedure');
     }
 
     protected function _getSqlCreateView($viewName)