DbTest.php 7.9 KB

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