2
0
Просмотр исходного кода

ZF-7776 - Made Zend_Test_DbAdapter awqre of the Zend_Db_Profiler and log sql queries and their bind parameters.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17985 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei 16 лет назад
Родитель
Сommit
e9302243f8

+ 15 - 0
documentation/manual/en/module_specs/Zend_Test-PHPUnit-Db-Adapter.xml

@@ -62,6 +62,21 @@ $adapter->appendStatementToStack(
 ]]></programlisting>
 
     <para>
+        By default the query profiler is enabled, so that you can retrieve the executed SQL statements
+        and their bound parameters to check for the correctness of the execution.
+    </para>
+
+    <programlisting language="php"><![CDATA[
+$adapter = new Zend_Test_DbAdapter();
+$stmt = $adapter->query("SELECT * FROM bugs");
+
+$qp = $adapter->getProfiler()->getLastQueryProfile();
+
+echo $qp->getQuerY(); // SELECT * FROM bugs
+]]>
+    </programlisting>
+
+    <para>
         The test adapter never checks if the query specified is really of the type SELECT, DELETE,
         INSERT or UPDATE which is returned next from the stack. The correct order of returning the
         data has to be implemented by the user of the test adapter.

+ 19 - 3
library/Zend/Test/DbAdapter.php

@@ -31,6 +31,11 @@ require_once "Zend/Db/Adapter/Abstract.php";
 require_once "Zend/Test/DbStatement.php";
 
 /**
+ * @see Zend_Db_Profiler
+ */
+require_once 'Zend/Db/Profiler.php';
+
+/**
  * Testing Database Adapter which acts as a stack for SQL Results
  *
  * @category   Zend
@@ -71,7 +76,9 @@ class Zend_Test_DbAdapter extends Zend_Db_Adapter_Abstract
      */
     public function __construct()
     {
-        $this->setProfiler(false);
+        $profiler = new Zend_Db_Profiler();
+        $profiler->setEnabled(true);
+        $this->setProfiler($profiler);
     }
 
     /**
@@ -214,11 +221,20 @@ class Zend_Test_DbAdapter extends Zend_Db_Adapter_Abstract
      */
     public function prepare($sql)
     {
+        $queryId = $this->getProfiler()->queryStart($sql);
+
         if(count($this->_statementStack)) {
-            return array_pop($this->_statementStack);
+            $stmt = array_pop($this->_statementStack);
         } else {
-            return new Zend_Test_DbStatement();
+            $stmt = new Zend_Test_DbStatement();
+        }
+        
+        if($this->getProfiler()->getEnabled() == true) {
+            $qp = $this->getProfiler()->getQueryProfile($queryId);
+            $stmt->setQueryProfile($qp);
         }
+
+        return $stmt;
     }
 
     /**

+ 20 - 0
library/Zend/Test/DbStatement.php

@@ -52,6 +52,11 @@ class Zend_Test_DbStatement implements Zend_Db_Statement_Interface
     protected $_rowCount = 0;
 
     /**
+     * @var Zend_Db_Profiler_Query
+     */
+    protected $_queryProfile = null;
+
+    /**
      * Create a Select statement which returns the given array of rows.
      *
      * @param array $rows
@@ -113,6 +118,14 @@ class Zend_Test_DbStatement implements Zend_Db_Statement_Interface
     }
 
     /**
+     * @param Zend_Db_Profiler_Query $qp
+     */
+    public function setQueryProfile(Zend_Db_Profiler_Query $qp)
+    {
+        $this->_queryProfile = $qp;
+    }
+
+    /**
      * @param int $rowCount
      */
     public function setRowCount($rowCount)
@@ -159,6 +172,9 @@ class Zend_Test_DbStatement implements Zend_Db_Statement_Interface
      */
     public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
     {
+        if($this->_queryProfile !== null) {
+            $this->_queryProfile->bindParam($parameter, $variable);
+        }
         return true;
     }
 
@@ -232,6 +248,10 @@ class Zend_Test_DbStatement implements Zend_Db_Statement_Interface
      */
     public function execute(array $params = array())
     {
+        if($this->_queryProfile !== null) {
+            $this->_queryProfile->bindParams($params);
+            $this->_queryProfile->end();
+        }
         return true;
     }
 

+ 54 - 0
tests/Zend/Test/DbAdapterTest.php

@@ -109,4 +109,58 @@ class Zend_Test_DbAdapterTest extends PHPUnit_Framework_TestCase
             "foo LIMIT 20,10", $sql
         );
     }
+
+    public function testQueryProfiler_EnabledByDefault()
+    {
+        $this->assertTrue($this->_adapter->getProfiler()->getEnabled());
+    }
+
+    public function testQueryPRofiler_PrepareStartsQueryProfiler()
+    {
+        $stmt = $this->_adapter->prepare("SELECT foo");
+
+        $this->assertEquals(1, $this->_adapter->getProfiler()->getTotalNumQueries());
+
+        $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
+        /* @var $qp Zend_Db_Profiler_Query */
+
+        $this->assertFalse($qp->hasEnded());
+    }
+
+    public function testQueryProfiler_QueryStartEndsQueryProfiler()
+    {
+        $stmt = $this->_adapter->query("SELECT foo");
+
+        $this->assertEquals(1, $this->_adapter->getProfiler()->getTotalNumQueries());
+
+        $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
+        /* @var $qp Zend_Db_Profiler_Query */
+
+        $this->assertTrue($qp->hasEnded());
+    }
+
+    public function testQueryProfiler_QueryBindWithParams()
+    {
+        $stmt = $this->_adapter->query("SELECT * FROM foo WHERE bar = ?", array(1234));
+
+        $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
+        /* @var $qp Zend_Db_Profiler_Query */
+
+        $this->assertEquals(array(1 => 1234), $qp->getQueryParams());
+        $this->assertEquals("SELECT * FROM foo WHERE bar = ?", $qp->getQuery());
+    }
+
+    public function testQueryProfiler_PrepareBindExecute()
+    {
+        $var = 1234;
+
+        $stmt = $this->_adapter->prepare("SELECT * FROM foo WHERE bar = ?");
+        $stmt->bindParam(1, $var);
+
+        $qp = $this->_adapter->getProfiler()->getLastQueryProfile();
+        /* @var $qp Zend_Db_Profiler_Query */
+
+        $this->assertEquals(array(1 => 1234), $qp->getQueryParams());
+        $this->assertEquals("SELECT * FROM foo WHERE bar = ?", $qp->getQuery());
+    }
 }