MultidbTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Application
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_MultidbTest::main');
  24. }
  25. /**
  26. * Zend_Loader_Autoloader
  27. */
  28. require_once 'Zend/Loader/Autoloader.php';
  29. /**
  30. * @see Zend_Application_Resource_Multidb
  31. */
  32. require_once 'Zend/Application/Resource/Multidb.php';
  33. require_once 'Zend/Db/Table.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Application
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Application
  41. */
  42. class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_dbOptions = array('db1' => array('adapter' => 'pdo_mysql','dbname' => 'db1','password' => 'XXXX','username' => 'webuser'),
  45. 'db2' => array('adapter' => 'pdo_pgsql', 'dbname' => 'db2', 'password' => 'notthatpublic', 'username' => 'dba'));
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. public function setUp()
  52. {
  53. // Store original autoloaders
  54. $this->loaders = spl_autoload_functions();
  55. if (!is_array($this->loaders)) {
  56. // spl_autoload_functions does not return empty array when no
  57. // autoloaders registered...
  58. $this->loaders = array();
  59. }
  60. Zend_Loader_Autoloader::resetInstance();
  61. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  62. $this->application = new Zend_Application('testing');
  63. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  64. Zend_Controller_Front::getInstance()->resetInstance();
  65. }
  66. public function tearDown()
  67. {
  68. Zend_Db_Table::setDefaultAdapter(null);
  69. Zend_Db_Table::setDefaultMetadataCache();
  70. // Restore original autoloaders
  71. $loaders = spl_autoload_functions();
  72. foreach ($loaders as $loader) {
  73. spl_autoload_unregister($loader);
  74. }
  75. foreach ($this->loaders as $loader) {
  76. spl_autoload_register($loader);
  77. }
  78. // Reset autoloader instance so it doesn't affect other tests
  79. Zend_Loader_Autoloader::resetInstance();
  80. }
  81. public function testInitializationInitializesResourcePluginObject()
  82. {
  83. $resource = new Zend_Application_Resource_Multidb(array());
  84. $resource->setBootstrap($this->bootstrap);
  85. $resource->setOptions($this->_dbOptions);
  86. $res = $resource->init();
  87. $this->assertTrue($res instanceof Zend_Application_Resource_Multidb);
  88. }
  89. public function testDbsAreSetupCorrectlyObject()
  90. {
  91. $resource = new Zend_Application_Resource_Multidb(array());
  92. $resource->setBootstrap($this->bootstrap);
  93. $resource->setOptions($this->_dbOptions);
  94. $res = $resource->init();
  95. $this->assertTrue($res->getDb('db1') instanceof Zend_Db_Adapter_Pdo_Mysql);
  96. $this->assertTrue($res->getDb('db2') instanceof Zend_Db_Adapter_Pdo_Pgsql);
  97. }
  98. public function testGetDefaultIsSetAndReturnedObject()
  99. {
  100. $options = $this->_dbOptions;
  101. $options['db2']['default'] = true;
  102. $resource = new Zend_Application_Resource_Multidb(array());
  103. $resource->setBootstrap($this->bootstrap);
  104. $resource->setOptions($options);
  105. $res = $resource->init();
  106. $this->assertTrue($res->getDb() instanceof Zend_Db_Adapter_Pdo_Pgsql);
  107. $this->assertTrue($res->isDefault($res->getDb('db2')));
  108. $this->assertTrue($res->isDefault('db2'));
  109. $options = $this->_dbOptions;
  110. $options['db2']['isDefaultTableAdapter'] = true;
  111. $resource = new Zend_Application_Resource_Multidb(array());
  112. $resource->setBootstrap($this->bootstrap);
  113. $resource->setOptions($options);
  114. $res = $resource->init();
  115. $this->assertTrue($res->getDb() instanceof Zend_Db_Adapter_Pdo_Pgsql);
  116. $this->assertTrue($res->isDefault($res->getDb('db2')));
  117. $this->assertTrue($res->isDefault('db2'));
  118. $this->assertTrue(Zend_Db_Table::getDefaultAdapter() instanceof Zend_Db_Adapter_Pdo_Pgsql);
  119. }
  120. public function testGetDefaultRandomWhenNoDefaultWasSetObject()
  121. {
  122. $resource = new Zend_Application_Resource_Multidb(array());
  123. $resource->setBootstrap($this->bootstrap);
  124. $resource->setOptions($this->_dbOptions);
  125. $res = $resource->init();
  126. $this->assertTrue($res->getDefaultDb() instanceof Zend_Db_Adapter_Pdo_Mysql);
  127. $this->assertTrue($res->getDefaultDb(true) instanceof Zend_Db_Adapter_Pdo_Mysql);
  128. $this->assertNull($res->getDefaultDb(false));
  129. }
  130. public function testGetDbWithFaultyDbNameThrowsException()
  131. {
  132. $resource = new Zend_Application_Resource_Multidb(array());
  133. $resource->setBootstrap($this->bootstrap);
  134. $resource->setOptions($this->_dbOptions);
  135. $res = $resource->init();
  136. try {
  137. $res->getDb('foobar');
  138. $this->fail('An exception should have been thrown');
  139. } catch(Zend_Application_Resource_Exception $e) {
  140. $this->assertEquals($e->getMessage(), 'A DB adapter was tried to retrieve, but was not configured');
  141. }
  142. }
  143. /**
  144. * @group ZF-9131
  145. */
  146. public function testParamDefaultAndAdapterAreNotPassedOnAsParameter()
  147. {
  148. $resource = new Zend_Application_Resource_Multidb(array());
  149. $resource->setBootstrap($this->bootstrap);
  150. $options = $this->_dbOptions;
  151. $options['db2']['isDefaultTableAdapter'] = true;
  152. $resource->setOptions($options);
  153. $res = $resource->init();
  154. $expected = array(
  155. 'dbname' => 'db2',
  156. 'password' => 'notthatpublic',
  157. 'username' => 'dba',
  158. 'charset' => null,
  159. 'persistent' => false,
  160. 'options' => array(
  161. 'caseFolding' => 0,
  162. 'autoQuoteIdentifiers' => true,
  163. 'fetchMode' => 2),
  164. 'driver_options' => array());
  165. $this->assertEquals($expected, $res->getDb('db2')->getConfig());
  166. $options = $this->_dbOptions;
  167. $options['db2']['default'] = true;
  168. $resource->setOptions($options);
  169. $res = $resource->init();
  170. $this->assertEquals($expected, $res->getDb('db2')->getConfig());
  171. }
  172. /**
  173. * @group ZF-10049
  174. */
  175. public function testSetDefaultMetadataCache()
  176. {
  177. $cache = Zend_Cache::factory('Core', 'Black Hole', array(
  178. 'lifetime' => 120,
  179. 'automatic_serialization' => true
  180. ));
  181. $options = $this->_dbOptions;
  182. $options['defaultMetadataCache'] = $cache;
  183. $resource = new Zend_Application_Resource_Multidb($options);
  184. $resource->init();
  185. $this->assertTrue(Zend_Db_Table::getDefaultMetadataCache() instanceof Zend_Cache_Core);
  186. }
  187. /**
  188. * @group ZF-10049
  189. */
  190. public function testSetDefaultMetadataCacheFromCacheManager()
  191. {
  192. $configCache = array(
  193. 'database' => array(
  194. 'frontend' => array(
  195. 'name' => 'Core',
  196. 'options' => array(
  197. 'lifetime' => 120,
  198. 'automatic_serialization' => true
  199. )
  200. ),
  201. 'backend' => array(
  202. 'name' => 'Black Hole'
  203. )
  204. )
  205. );
  206. $this->bootstrap->registerPluginResource('cachemanager', $configCache);
  207. $options = $this->_dbOptions;
  208. $options['defaultMetadataCache'] = 'database';
  209. $resource = new Zend_Application_Resource_Multidb($options);
  210. $resource->setBootstrap($this->bootstrap);
  211. $resource->init();
  212. $this->assertTrue(Zend_Db_Table::getDefaultMetadataCache() instanceof Zend_Cache_Core);
  213. }
  214. }
  215. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_MultidbTest::main') {
  216. Zend_Application_Resource_MultidbTest::main();
  217. }