DbTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_DbTest::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. * @category Zend
  35. * @package Zend_Application
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Application
  40. */
  41. class Zend_Application_Resource_DbTest extends PHPUnit_Framework_TestCase
  42. {
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. // Store original autoloaders
  51. $this->loaders = spl_autoload_functions();
  52. if (!is_array($this->loaders)) {
  53. // spl_autoload_functions does not return empty array when no
  54. // autoloaders registered...
  55. $this->loaders = array();
  56. }
  57. Zend_Loader_Autoloader::resetInstance();
  58. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  59. $this->application = new Zend_Application('testing');
  60. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  61. $this->bootstrap = new ZfAppBootstrap($this->application);
  62. }
  63. public function tearDown()
  64. {
  65. Zend_Db_Table::setDefaultMetadataCache();
  66. // Restore original autoloaders
  67. $loaders = spl_autoload_functions();
  68. foreach ($loaders as $loader) {
  69. spl_autoload_unregister($loader);
  70. }
  71. foreach ($this->loaders as $loader) {
  72. spl_autoload_register($loader);
  73. }
  74. // Reset autoloader instance so it doesn't affect other tests
  75. Zend_Loader_Autoloader::resetInstance();
  76. }
  77. public function testAdapterIsNullByDefault()
  78. {
  79. require_once 'Zend/Application/Resource/Db.php';
  80. $resource = new Zend_Application_Resource_Db();
  81. $this->assertNull($resource->getAdapter());
  82. }
  83. public function testDbIsNullByDefault()
  84. {
  85. require_once 'Zend/Application/Resource/Db.php';
  86. $resource = new Zend_Application_Resource_Db();
  87. $this->assertNull($resource->getDbAdapter());
  88. }
  89. public function testParamsAreEmptyByDefault()
  90. {
  91. require_once 'Zend/Application/Resource/Db.php';
  92. $resource = new Zend_Application_Resource_Db();
  93. $params = $resource->getParams();
  94. $this->assertTrue(empty($params));
  95. }
  96. public function testIsDefaultTableAdapter()
  97. {
  98. require_once 'Zend/Application/Resource/Db.php';
  99. $resource = new Zend_Application_Resource_Db();
  100. $this->assertTrue($resource->isDefaultTableAdapter());
  101. }
  102. public function testPassingDatabaseConfigurationSetsObjectState()
  103. {
  104. require_once 'Zend/Application/Resource/Db.php';
  105. $config = array(
  106. 'adapter' => 'Pdo_Sqlite',
  107. 'params' => array(
  108. 'dbname' => ':memory:',
  109. ),
  110. 'isDefaultTableAdapter' => false,
  111. );
  112. $resource = new Zend_Application_Resource_Db($config);
  113. $this->assertFalse($resource->isDefaultTableAdapter());
  114. $this->assertEquals($config['adapter'], $resource->getAdapter());
  115. $this->assertEquals($config['params'], $resource->getParams());
  116. }
  117. public function testInitShouldInitializeDbAdapter()
  118. {
  119. require_once 'Zend/Application/Resource/Db.php';
  120. $config = array(
  121. 'adapter' => 'Pdo_Sqlite',
  122. 'params' => array(
  123. 'dbname' => ':memory:',
  124. ),
  125. 'isDefaultTableAdapter' => false,
  126. );
  127. $resource = new Zend_Application_Resource_Db($config);
  128. $resource->init();
  129. $db = $resource->getDbAdapter();
  130. $this->assertTrue($db instanceof Zend_Db_Adapter_Pdo_Sqlite);
  131. }
  132. /**
  133. * @group ZF-10033
  134. */
  135. public function testSetDefaultMetadataCache()
  136. {
  137. $cache = Zend_Cache::factory('Core', 'Black Hole', array(
  138. 'lifetime' => 120,
  139. 'automatic_serialization' => true
  140. ));
  141. $config = array(
  142. 'adapter' => 'PDO_SQLite',
  143. 'params' => array(
  144. 'dbname' => ':memory:',
  145. ),
  146. 'defaultMetadataCache' => $cache,
  147. );
  148. $resource = new Zend_Application_Resource_Db($config);
  149. $resource->init();
  150. $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache());
  151. }
  152. /**
  153. * @group ZF-10033
  154. */
  155. public function testSetDefaultMetadataCacheFromCacheManager()
  156. {
  157. $configCache = array(
  158. 'database' => array(
  159. 'frontend' => array(
  160. 'name' => 'Core',
  161. 'options' => array(
  162. 'lifetime' => 120,
  163. 'automatic_serialization' => true
  164. )
  165. ),
  166. 'backend' => array(
  167. 'name' => 'Black Hole'
  168. )
  169. )
  170. );
  171. $this->bootstrap->registerPluginResource('cachemanager', $configCache);
  172. $config = array(
  173. 'bootstrap' => $this->bootstrap,
  174. 'adapter' => 'PDO_SQLite',
  175. 'params' => array(
  176. 'dbname' => ':memory:',
  177. ),
  178. 'defaultMetadataCache' => 'database',
  179. );
  180. $resource = new Zend_Application_Resource_Db($config);
  181. $resource->init();
  182. $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache());
  183. }
  184. /**
  185. * @group ZF-6620
  186. */
  187. public function testSetOptionFetchMode()
  188. {
  189. $config = array(
  190. 'bootstrap' => $this->bootstrap,
  191. 'adapter' => 'PDO_SQLite',
  192. 'params' => array(
  193. 'dbname' => ':memory:',
  194. 'options' => array(
  195. 'fetchMode' => 'obj'
  196. )
  197. ),
  198. );
  199. $resource = new Zend_Application_Resource_Db($config);
  200. $db = $resource->init();
  201. $this->assertEquals($db->getFetchMode(), Zend_Db::FETCH_OBJ);
  202. }
  203. /**
  204. * @group ZF-10543
  205. */
  206. public function testSetDefaultMetadataCacheThroughBootstrap()
  207. {
  208. $options = array(
  209. 'resources' => array(
  210. 'db' => array(
  211. 'adapter' => 'Pdo_Sqlite',
  212. 'params' => array(
  213. 'dbname' => ':memory:'
  214. ),
  215. 'defaultMetadataCache' => 'default'
  216. ),
  217. 'cachemanager' => array(
  218. 'default' => array(
  219. 'backend' => array('name' => 'black-hole')
  220. )
  221. )
  222. )
  223. );
  224. $this->bootstrap->setOptions($options);
  225. $this->bootstrap->bootstrap();
  226. $resource = $this->bootstrap->getResource('cachemanager');
  227. $this->assertEquals($resource->getCache('default'), Zend_Db_Table::getDefaultMetadataCache());
  228. }
  229. }
  230. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_DbTest::main') {
  231. Zend_Application_Resource_DbTest::main();
  232. }