From 918ff5335de4dc78a584056ad336aadf2dd53600 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 28 Feb 2013 15:27:39 -0500
Subject: [PATCH] Improved ILS\Connection dependency injection and related unit
 tests.

---
 module/VuFind/config/module.config.php        | 13 ++--
 module/VuFind/src/VuFind/ILS/Connection.php   | 62 +++++++++----------
 .../VuFind/src/VuFindTest/Unit/TestCase.php   | 17 +++--
 .../tests/unit-tests/src/Auth/ILSTest.php     | 17 ++++-
 4 files changed, 56 insertions(+), 53 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index d8caa8dafbf..7908a726c22 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -183,13 +183,12 @@ $config = array(
                 return new \VuFindHttp\HttpService($options);
             },
             'VuFind\ILSConnection' => function ($sm) {
-                $catalog = new \VuFind\ILS\Connection();
-                return $catalog
-                    ->setConfig($sm->get('VuFind\Config')->get('config')->Catalog)
-                    ->setHoldConfig($sm->get('VuFind\ILSHoldSettings'))
-                    ->initWithDriverManager(
-                        $sm->get('VuFind\ILSDriverPluginManager')
-                    );
+                $catalog = new \VuFind\ILS\Connection(
+                    $sm->get('VuFind\Config')->get('config')->Catalog,
+                    $sm->get('VuFind\ILSDriverPluginManager'),
+                    $sm->get('VuFind\Config')
+                );
+                return $catalog->setHoldConfig($sm->get('VuFind\ILSHoldSettings'));
             },
             'VuFind\ILSHoldLogic' => function ($sm) {
                 return new \VuFind\ILS\Logic\Holds(
diff --git a/module/VuFind/src/VuFind/ILS/Connection.php b/module/VuFind/src/VuFind/ILS/Connection.php
index 12bc74e738a..8d0f88bdb78 100644
--- a/module/VuFind/src/VuFind/ILS/Connection.php
+++ b/module/VuFind/src/VuFind/ILS/Connection.php
@@ -30,8 +30,7 @@
  * @link     http://vufind.org/wiki/vufind2:building_an_ils_driver Wiki
  */
 namespace VuFind\ILS;
-use VuFind\Config\Reader as ConfigReader, VuFind\Exception\ILS as ILSException,
-    VuFind\ILS\Driver\DriverInterface;
+use VuFind\Exception\ILS as ILSException, VuFind\ILS\Driver\DriverInterface;
 
 /**
  * Catalog Connection Class
@@ -84,44 +83,26 @@ class Connection
     protected $titleHoldsMode = 'disabled';
 
     /**
-     * Set the configuration of the connection.
+     * Configuration loader
      *
-     * @param \Zend\Config\Config $config Configuration representing the [Catalog]
-     * section of config.ini
-     *
-     * @return Connection
+     * @var \VuFind\Config\PluginManager
      */
-    public function setConfig($config)
-    {
-        $this->config = $config;
-        return $this;
-    }
+    protected $configReader;
 
     /**
-     * Set the hold configuration for the connection.
-     *
-     * @param \VuFind\ILS\HoldSettings $settings Hold settings
-     *
-     * @return Connection
-     */
-    public function setHoldConfig($settings)
-    {
-        $this->holdsMode = $settings->getHoldsMode();
-        $this->titleHoldsMode = $settings->getTitleHoldsMode();
-        return $this;
-    }
-
-    /**
-     * Initialize the driver using the ILS driver plugin manager.
+     * Constructor
      *
+     * @param \Zend\Config\Config              $config        Configuration
+     * representing the [Catalog] section of config.ini
      * @param \VuFind\ILS\Driver\PluginManager $driverManager Driver plugin manager
-     *
-     * @throws \Exception
-     * @return Connection
+     * @param \VuFind\Config\PluginManager     $configReader  Configuration loader
      */
-    public function initWithDriverManager(
-        \VuFind\ILS\Driver\PluginManager $driverManager
+    public function __construct(\Zend\Config\Config $config,
+        \VuFind\ILS\Driver\PluginManager $driverManager,
+        \VuFind\Config\PluginManager $configReader
     ) {
+        $this->config = $config;
+        $this->configReader = $configReader;
         if (!isset($this->config->driver)) {
             throw new \Exception('ILS driver setting missing.');
         }
@@ -142,6 +123,19 @@ class Connection
                 $this->setDriver($driverManager->get('NoILS'));
             }
         }
+    }
+
+    /**
+     * Set the hold configuration for the connection.
+     *
+     * @param \VuFind\ILS\HoldSettings $settings Hold settings
+     *
+     * @return Connection
+     */
+    public function setHoldConfig($settings)
+    {
+        $this->holdsMode = $settings->getHoldsMode();
+        $this->titleHoldsMode = $settings->getTitleHoldsMode();
         return $this;
     }
 
@@ -200,13 +194,13 @@ class Connection
         // Determine config file name based on class name:
         $parts = explode('\\', $this->getDriverClass());
         try {
-            $config = ConfigReader::getConfig(end($parts));
+            $config = $this->configReader->get(end($parts));
         } catch (\Zend\Config\Exception\RuntimeException $e) {
             // Configuration loading failed; probably means file does not
             // exist -- just return an empty array in that case:
             return array();
         }
-        return $config->toArray();
+        return is_object($config) ? $config->toArray() : array();
     }
 
     /**
diff --git a/module/VuFind/src/VuFindTest/Unit/TestCase.php b/module/VuFind/src/VuFindTest/Unit/TestCase.php
index c7bc70dd657..04a6e1a9923 100644
--- a/module/VuFind/src/VuFindTest/Unit/TestCase.php
+++ b/module/VuFind/src/VuFindTest/Unit/TestCase.php
@@ -136,6 +136,12 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
             $this->serviceManager->setService(
                 'VuFind\Http', new \VuFindHttp\HttpService()
             );
+            $cfg = new \Zend\ServiceManager\Config(
+                array('abstract_factories' => array('VuFind\Config\PluginFactory'))
+            );
+            $this->serviceManager->setService(
+                'VuFind\Config', new \VuFind\Config\PluginManager($cfg)
+            );
             \VuFind\Connection\Manager::setServiceLocator($this->serviceManager);
         }
         return $this->serviceManager;
@@ -153,16 +159,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
             $authManager = new \VuFind\Auth\PluginManager(
                 new \Zend\ServiceManager\Config(
                     array(
-                        'abstract_factories' =>
-                            array('VuFind\Auth\PluginFactory'),
-                        'factories' => array(
-                            'ils' => 
-                                function ($sm) {
-                                    return new \VuFind\Auth\ILS(
-                                        new \VuFind\ILS\Connection()
-                                    );
-                                },
-                        ),
+                        'abstract_factories' => array('VuFind\Auth\PluginFactory'),
                     )
                 )
             );
diff --git a/module/VuFind/tests/unit-tests/src/Auth/ILSTest.php b/module/VuFind/tests/unit-tests/src/Auth/ILSTest.php
index 4d9cd4da1a0..53745533b7e 100644
--- a/module/VuFind/tests/unit-tests/src/Auth/ILSTest.php
+++ b/module/VuFind/tests/unit-tests/src/Auth/ILSTest.php
@@ -48,8 +48,21 @@ class ILSTest extends \VuFindTest\Unit\DbTestCase
     public function __construct()
     {
         $this->driver = $this->getMock('VuFind\ILS\Driver\Sample');
-        $this->auth = $this->getAuthManager()->get('ILS');
-        $this->auth->getCatalog()->setDriver($this->driver);;
+        $driverManager = new \VuFind\ILS\Driver\PluginManager();
+        $driverManager->setService('Sample', $this->driver);
+        $mockConfigReader = $this->getMock('VuFind\Config\PluginManager');
+        $mockConfigReader->expects($this->any())->method('get')
+            ->will($this->returnValue(new \Zend\Config\Config(array())));
+        $this->auth = new \VuFind\Auth\ILS(
+            new \VuFind\ILS\Connection(
+                new \Zend\Config\Config(array('driver' => 'Sample')),
+                $driverManager, $mockConfigReader
+            )
+        );
+        $this->auth->setDbTableManager(
+            $this->getServiceManager()->get('VuFind\DbTablePluginManager')
+        );
+        $this->auth->getCatalog()->setDriver($this->driver);
     }
 
     /**
-- 
GitLab