ZF-11606: getRowCount() dies in Zend_Test_PHPUnit_Db_Connection

Description

h1. ISSUE with an test class implementation like this,


class MyTest extends Zend_Test_PHPUnit_DatabaseTestCase {
  ...
  public function testFoo (){
    ...
    $this->getConnection()->getRowCount('guestbook');
    ...
  }
  ...
}

# run it,
$ phpunit MyTest.php

then you get: {quote} Fatal error: Call to a member function query() on a non-object in /path/to/lib/php/PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.php on line 177 {quote}

h1. PROBLEM * getRowCount() is not overridden by Zend_Test_PHPUnit_Db_Connection class, the original (parent class) PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection's method is called. * in this PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection's getRowCount() implementation, it's using the property $this->connection * this is not used with Zend_Test_PHPUnit_Db_Connection, $this->_connection is the correct property to use. * thus, the non-object call.

h1. THE FIX * getRowCount() should be overrided by Zend_Test_PHPUnit_Db_Connection * below is the patch:


$ cd library/Zend/Test/PHPUnit/Db
$ diff -Naru Connection.php.original Connection.php 
--- Connection.php.original     2011-07-25 20:18:05.000000000 +0900
+++ Connection.php      2011-07-25 20:39:03.000000000 +0900
@@ -147,4 +147,14 @@
         return "DELETE";
     }
 
+    public function getRowCount($tableName, $whereClause = NULL)
+    {
+        $query = "SELECT COUNT(*) FROM ".$this->quoteSchemaObject($tableName);
+
+        if (isset($whereClause)) {
+            $query .= " WHERE {$whereClause}";
+        }
+
+        return (int) $this->_connection->query($query)->fetchColumn();
+    }
 }

h1. or... {quote} we can fiddle around *__get()* to pass $this->connection call to $this->_connection. {quote}

Comments

No comments to display