MultidbTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // Restore original autoloaders
  74. $loaders = spl_autoload_functions();
  75. foreach ($loaders as $loader) {
  76. spl_autoload_unregister($loader);
  77. }
  78. foreach ($this->loaders as $loader) {
  79. spl_autoload_register($loader);
  80. }
  81. // Reset autoloader instance so it doesn't affect other tests
  82. Zend_Loader_Autoloader::resetInstance();
  83. }
  84. public function testInitializationInitializesResourcePluginObject()
  85. {
  86. $resource = new Zend_Application_Resource_Multidb(array());
  87. $resource->setBootstrap($this->bootstrap);
  88. $resource->setOptions($this->_dbOptions);
  89. $res = $resource->init();
  90. $this->assertTrue($res instanceof Zend_Application_Resource_Multidb);
  91. }
  92. public function testDbsAreSetupCorrectlyObject()
  93. {
  94. $resource = new Zend_Application_Resource_Multidb(array());
  95. $resource->setBootstrap($this->bootstrap);
  96. $resource->setOptions($this->_dbOptions);
  97. $res = $resource->init();
  98. $this->assertTrue($res->getDb('db1') instanceof Zend_Db_Adapter_Pdo_Mysql);
  99. $this->assertTrue($res->getDb('db2') instanceof Zend_Db_Adapter_Pdo_Pgsql);
  100. }
  101. public function testGetDefaultIsSetAndReturnedObject()
  102. {
  103. $options = $this->_dbOptions;
  104. $options['db2']['default'] = true;
  105. $resource = new Zend_Application_Resource_Multidb(array());
  106. $resource->setBootstrap($this->bootstrap);
  107. $resource->setOptions($options);
  108. $res = $resource->init();
  109. $this->assertTrue($res->getDb() instanceof Zend_Db_Adapter_Pdo_Pgsql);
  110. $this->assertTrue($res->isDefault($res->getDb('db2')));
  111. $this->assertTrue($res->isDefault('db2'));
  112. $options = $this->_dbOptions;
  113. $options['db2']['isDefaultTableAdapter'] = true;
  114. $resource = new Zend_Application_Resource_Multidb(array());
  115. $resource->setBootstrap($this->bootstrap);
  116. $resource->setOptions($options);
  117. $res = $resource->init();
  118. $this->assertTrue($res->getDb() instanceof Zend_Db_Adapter_Pdo_Pgsql);
  119. $this->assertTrue($res->isDefault($res->getDb('db2')));
  120. $this->assertTrue($res->isDefault('db2'));
  121. $this->assertTrue(Zend_Db_Table::getDefaultAdapter() instanceof Zend_Db_Adapter_Pdo_Pgsql);
  122. }
  123. public function testGetDefaultRandomWhenNoDefaultWasSetObject()
  124. {
  125. $resource = new Zend_Application_Resource_Multidb(array());
  126. $resource->setBootstrap($this->bootstrap);
  127. $resource->setOptions($this->_dbOptions);
  128. $res = $resource->init();
  129. $this->assertTrue($res->getDefaultDb() instanceof Zend_Db_Adapter_Pdo_Mysql);
  130. $this->assertTrue($res->getDefaultDb(true) instanceof Zend_Db_Adapter_Pdo_Mysql);
  131. $this->assertNull($res->getDefaultDb(false));
  132. }
  133. public function testGetDbWithFaultyDbNameThrowsException()
  134. {
  135. $resource = new Zend_Application_Resource_Multidb(array());
  136. $resource->setBootstrap($this->bootstrap);
  137. $resource->setOptions($this->_dbOptions);
  138. $res = $resource->init();
  139. try {
  140. $res->getDb('foobar');
  141. $this->fail('An exception should have been thrown');
  142. } catch(Zend_Application_Resource_Exception $e) {
  143. $this->assertEquals($e->getMessage(), 'A DB adapter was tried to retrieve, but was not configured');
  144. }
  145. }
  146. /**
  147. * @group ZF-9131
  148. */
  149. public function testParamDefaultAndAdapterAreNotPassedOnAsParameter()
  150. {
  151. $resource = new Zend_Application_Resource_Multidb(array());
  152. $resource->setBootstrap($this->bootstrap);
  153. $options = $this->_dbOptions;
  154. $options['db2']['isDefaultTableAdapter'] = true;
  155. $resource->setOptions($options);
  156. $res = $resource->init();
  157. $expected = array(
  158. 'dbname' => 'db2',
  159. 'password' => 'notthatpublic',
  160. 'username' => 'dba',
  161. 'charset' => null,
  162. 'persistent' => false,
  163. 'options' => array('caseFolding' => 0, 'autoQuoteIdentifiers' => true),
  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. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LogTest::main') {
  174. Zend_Application_Resource_LogTest::main();
  175. }