Parcourir la source

[ZF-9131] Properly handle isDefaultTableAdapter in MultiDb resource

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22259 44c647ce-9c0f-0410-b52a-842ac1e357ba
hobodave il y a 15 ans
Parent
commit
c45b65fe5e

+ 29 - 26
library/Zend/Application/Resource/Multidb.php

@@ -38,7 +38,7 @@ require_once 'Zend/Db/Table.php';
  *   resources.multidb.db1.password = "XXXX"
  *   resources.multidb.db1.dbname = "db1"
  *   resources.multidb.db1.default = true
- *   
+ *
  *   resources.multidb.db2.adapter = "pdo_pgsql"
  *   resources.multidb.db2.host = "example.com"
  *   resources.multidb.db2.username = "dba"
@@ -56,14 +56,14 @@ class Zend_Application_Resource_Multidb extends Zend_Application_Resource_Resour
 {
     /**
      * Associative array containing all configured db's
-     * 
+     *
      * @var array
-     */   
+     */
     protected $_dbs = array();
-    
+
     /**
      * An instance of the default db, if set
-     * 
+     *
      * @var null|Zend_Db_Adapter_Abstract
      */
     protected $_defaultDb;
@@ -72,27 +72,30 @@ class Zend_Application_Resource_Multidb extends Zend_Application_Resource_Resour
      * Initialize the Database Connections (instances of Zend_Db_Table_Abstract)
      *
      * @return Zend_Application_Resource_Multidb
-     */    
-    public function init() 
+     */
+    public function init()
     {
         $options = $this->getOptions();
-        
+
         foreach ($options as $id => $params) {
         	$adapter = $params['adapter'];
-            $default = isset($params['default'])?(int)$params['default']:false;
-            unset($params['adapter'], $params['default']);
-        	
+            $default = (int) (
+                isset($params['isDefaultTableAdapter']) && $params['isDefaultTableAdapter']
+                || isset($params['default']) && $params['default']
+            );
+            unset(
+                $params['adapter'],
+                $params['default'],
+                $params['isDefaultTableAdapter']
+            );
+
             $this->_dbs[$id] = Zend_Db::factory($adapter, $params);
 
-            if ($default
-                // For consistency with the Db Resource Plugin
-                || (isset($params['isDefaultTableAdapter']) 
-                    && $params['isDefaultTableAdapter'] == true)
-            ) {
+            if ($default) {
                 $this->_setDefault($this->_dbs[$id]);
             }
         }
-        
+
         return $this;
     }
 
@@ -113,22 +116,22 @@ class Zend_Application_Resource_Multidb extends Zend_Application_Resource_Resour
 
     /**
      * Retrieve the specified database connection
-     * 
+     *
      * @param  null|string|Zend_Db_Adapter_Abstract $db The adapter to retrieve.
      *                                               Null to retrieve the default connection
      * @return Zend_Db_Adapter_Abstract
      * @throws Zend_Application_Resource_Exception if the given parameter could not be found
      */
-    public function getDb($db = null) 
+    public function getDb($db = null)
     {
         if ($db === null) {
             return $this->getDefaultDb();
         }
-        
+
         if (isset($this->_dbs[$db])) {
             return $this->_dbs[$db];
         }
-        
+
         throw new Zend_Application_Resource_Exception(
             'A DB adapter was tried to retrieve, but was not configured'
         );
@@ -136,13 +139,13 @@ class Zend_Application_Resource_Multidb extends Zend_Application_Resource_Resour
 
     /**
      * Get the default db connection
-     * 
+     *
      * @param  boolean $justPickOne If true, a random (the first one in the stack)
      *                           connection is returned if no default was set.
      *                           If false, null is returned if no default was set.
      * @return null|Zend_Db_Adapter_Abstract
      */
-    public function getDefaultDb($justPickOne = true) 
+    public function getDefaultDb($justPickOne = true)
     {
         if ($this->_defaultDb !== null) {
             return $this->_defaultDb;
@@ -151,16 +154,16 @@ class Zend_Application_Resource_Multidb extends Zend_Application_Resource_Resour
         if ($justPickOne) {
             return reset($this->_dbs); // Return first db in db pool
         }
-        
+
         return null;
     }
 
     /**
      * Set the default db adapter
-     * 
+     *
      * @var Zend_Db_Adapter_Abstract $adapter Adapter to set as default
      */
-    protected function _setDefault(Zend_Db_Adapter_Abstract $adapter) 
+    protected function _setDefault(Zend_Db_Adapter_Abstract $adapter)
     {
         Zend_Db_Table::setDefaultAdapter($adapter);
         $this->_defaultDb = $adapter;

+ 22 - 14
tests/Zend/Application/Resource/MultidbTest.php

@@ -54,7 +54,7 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
 {
     protected $_dbOptions = array('db1' => array('adapter' => 'pdo_mysql','dbname' => 'db1','password' => 'XXXX','username' => 'webuser'),
                                 'db2' => array('adapter' => 'pdo_pgsql', 'dbname' => 'db2', 'password' => 'notthatpublic', 'username' => 'dba'));
-    
+
     public static function main()
     {
         $suite  = new PHPUnit_Framework_TestSuite(__CLASS__);
@@ -82,7 +82,7 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
     public function tearDown()
     {
         Zend_Db_Table::setDefaultAdapter(null);
-        
+
         // Restore original autoloaders
         $loaders = spl_autoload_functions();
         foreach ($loaders as $loader) {
@@ -105,7 +105,7 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
         $res = $resource->init();
         $this->assertTrue($res instanceof Zend_Application_Resource_Multidb);
     }
-    
+
     public function testDbsAreSetupCorrectlyObject()
     {
         $resource = new Zend_Application_Resource_Multidb(array());
@@ -115,12 +115,12 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($res->getDb('db1') instanceof Zend_Db_Adapter_Pdo_Mysql);
         $this->assertTrue($res->getDb('db2') instanceof Zend_Db_Adapter_Pdo_Pgsql);
     }
-    
+
     public function testGetDefaultIsSetAndReturnedObject()
     {
         $options = $this->_dbOptions;
         $options['db2']['default'] = true;
-        
+
         $resource = new Zend_Application_Resource_Multidb(array());
         $resource->setBootstrap($this->bootstrap);
         $resource->setOptions($options);
@@ -128,10 +128,10 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($res->getDb() instanceof Zend_Db_Adapter_Pdo_Pgsql);
         $this->assertTrue($res->isDefault($res->getDb('db2')));
         $this->assertTrue($res->isDefault('db2'));
-        
+
         $options = $this->_dbOptions;
         $options['db2']['isDefaultTableAdapter'] = true;
-        
+
         $resource = new Zend_Application_Resource_Multidb(array());
         $resource->setBootstrap($this->bootstrap);
         $resource->setOptions($options);
@@ -140,9 +140,9 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($res->isDefault($res->getDb('db2')));
         $this->assertTrue($res->isDefault('db2'));
         $this->assertTrue(Zend_Db_Table::getDefaultAdapter() instanceof Zend_Db_Adapter_Pdo_Pgsql);
-        
+
     }
-    
+
     public function testGetDefaultRandomWhenNoDefaultWasSetObject()
     {
         $resource = new Zend_Application_Resource_Multidb(array());
@@ -153,7 +153,7 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($res->getDefaultDb(true) instanceof Zend_Db_Adapter_Pdo_Mysql);
         $this->assertNull($res->getDefaultDb(false));
     }
-    
+
     public function testGetDbWithFaultyDbNameThrowsException()
     {
         $resource = new Zend_Application_Resource_Multidb(array());
@@ -168,7 +168,7 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
             $this->assertEquals($e->getMessage(), 'A DB adapter was tried to retrieve, but was not configured');
         }
     }
-    
+
     /**
      * @group ZF-9131
      */
@@ -176,9 +176,11 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
     {
         $resource = new Zend_Application_Resource_Multidb(array());
         $resource->setBootstrap($this->bootstrap);
-        $resource->setOptions($this->_dbOptions);
+        $options = $this->_dbOptions;
+        $options['db2']['isDefaultTableAdapter'] = true;
+        $resource->setOptions($options);
         $res = $resource->init();
-        
+
         $expected = array(
             'dbname' => 'db2',
             'password' => 'notthatpublic',
@@ -188,9 +190,15 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
             'options' => array('caseFolding' => 0, 'autoQuoteIdentifiers' => true),
             'driver_options' => array());
         $this->assertEquals($expected, $res->getDb('db2')->getConfig());
+
+        $options = $this->_dbOptions;
+        $options['db2']['default'] = true;
+        $resource->setOptions($options);
+        $res = $resource->init();
+        $this->assertEquals($expected, $res->getDb('db2')->getConfig());
     }
 
-    
+
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LogTest::main') {