MultidbTest.php 8.7 KB

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