From 76be5746259f1110aed1e21800db1471d8fe830f Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 25 Mar 2015 13:43:09 -0400
Subject: [PATCH] Fixed broken test.

---
 module/VuFind/src/VuFind/Db/Table/Gateway.php |  3 +-
 .../src/VuFindTest/Auth/DatabaseUnitTest.php  | 59 ++++++++++++++-----
 2 files changed, 47 insertions(+), 15 deletions(-)

diff --git a/module/VuFind/src/VuFind/Db/Table/Gateway.php b/module/VuFind/src/VuFind/Db/Table/Gateway.php
index 474abc0c93c..cc010df9acb 100644
--- a/module/VuFind/src/VuFind/Db/Table/Gateway.php
+++ b/module/VuFind/src/VuFind/Db/Table/Gateway.php
@@ -139,7 +139,8 @@ class Gateway extends AbstractTableGateway implements ServiceLocatorAwareInterfa
 
         // If this is a PostgreSQL connection, we may need to initialize the ID
         // from a sequence:
-        if ($this->adapter->getDriver()->getDatabasePlatformName() == "Postgresql"
+        if ($this->adapter
+            && $this->adapter->getDriver()->getDatabasePlatformName() == "Postgresql"
             && $obj instanceof \VuFind\Db\Row\RowGateway
         ) {
             // Do we have a sequence feature?
diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Auth/DatabaseUnitTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Auth/DatabaseUnitTest.php
index 11b8251e2f4..2f2057bb8b2 100644
--- a/module/VuFind/tests/unit-tests/src/VuFindTest/Auth/DatabaseUnitTest.php
+++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Auth/DatabaseUnitTest.php
@@ -26,7 +26,7 @@
  * @link     http://www.vufind.org  Main Page
  */
 namespace VuFindTest\Auth;
-use VuFind\Auth\Database, Zend\Stdlib\Parameters;
+use VuFind\Auth\Database, Zend\Db\ResultSet\ResultSet, Zend\Stdlib\Parameters;
 
 /**
  * Database authentication test class.
@@ -110,9 +110,7 @@ class DatabaseUnitTest extends \VuFindTest\Unit\DbTestCase
     public function testCreateDuplicateEmail()
     {
         // Fake services:
-        $table = $this->getMock(
-            'VuFind\Db\Table\Tags', ['getByEmail', 'getByUsername']
-        );
+        $table = $this->getMockTable(['getByEmail', 'getByUsername']);
         $table->expects($this->once())->method('getByEmail')
             ->with($this->equalTo('me@mysite.com'))
             ->will($this->returnValue(true));
@@ -136,9 +134,7 @@ class DatabaseUnitTest extends \VuFindTest\Unit\DbTestCase
     public function testCreateDuplicateUsername()
     {
         // Fake services:
-        $table = $this->getMock(
-            'VuFind\Db\Table\Tags', ['getByUsername']
-        );
+        $table = $this->getMockTable(['getByUsername']);
         $table->expects($this->any())->method('getByUsername')
             ->with($this->equalTo('good'))
             ->will($this->returnValue(true));
@@ -156,10 +152,7 @@ class DatabaseUnitTest extends \VuFindTest\Unit\DbTestCase
     public function testSuccessfulCreation()
     {
         // Fake services:
-        $table = $this->getMock(
-            'VuFind\Db\Table\Tags', ['insert', 'getByEmail', 'getByUsername']
-        );
-        $table->expects($this->once())->method('insert');
+        $table = $this->getMockTable(['insert', 'getByEmail', 'getByUsername']);
         $table->expects($this->once())->method('getByEmail')
             ->with($this->equalTo('me@mysite.com'))
             ->will($this->returnValue(false));
@@ -167,9 +160,10 @@ class DatabaseUnitTest extends \VuFindTest\Unit\DbTestCase
             ->with($this->equalTo('good'))
             ->will($this->returnValue(false));
         $db = $this->getDatabase($table);
-        $this->assertEquals(
-            false, $db->create($this->getRequest($this->getCreateParams()))
-        );
+        $prototype = $table->getResultSetPrototype()->getArrayObjectPrototype();
+        $prototype->expects($this->once())->method('save');
+        $user = $db->create($this->getRequest($this->getCreateParams()));
+        $this->assertTrue(is_object($user));
     }
 
     // INTERNAL API
@@ -191,6 +185,43 @@ class DatabaseUnitTest extends \VuFindTest\Unit\DbTestCase
         ];
     }
 
+    /**
+     * Get a mock row object
+     *
+     * @return \VuFind\Db\Row\User
+     */
+    protected function getMockRow()
+    {
+        return $this->getMockBuilder('VuFind\Db\Row\User')
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
+
+    /**
+     * Get a mock table object
+     *
+     * @param array $methods Methods to mock
+     *
+     * @return \VuFind\Db\Table\User
+     */
+    protected function getMockTable($methods = [])
+    {
+        $methods[] = 'getResultSetPrototype';
+        $mock = $this->getMockBuilder('VuFind\Db\Table\User')
+            ->disableOriginalConstructor()
+            ->setMethods($methods)
+            ->getMock();
+        $mock->expects($this->any())->method('getResultSetPrototype')
+            ->will(
+                $this->returnValue(
+                    new ResultSet(
+                        ResultSet::TYPE_ARRAYOBJECT, $this->getMockRow()
+                    )
+                )
+            );
+        return $mock;
+    }
+
     /**
      * Get a fake HTTP request.
      *
-- 
GitLab