DbTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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-2009 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-2009 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. // Restore original autoloaders
  66. $loaders = spl_autoload_functions();
  67. foreach ($loaders as $loader) {
  68. spl_autoload_unregister($loader);
  69. }
  70. foreach ($this->loaders as $loader) {
  71. spl_autoload_register($loader);
  72. }
  73. // Reset autoloader instance so it doesn't affect other tests
  74. Zend_Loader_Autoloader::resetInstance();
  75. }
  76. public function testAdapterIsNullByDefault()
  77. {
  78. require_once 'Zend/Application/Resource/Db.php';
  79. $resource = new Zend_Application_Resource_Db();
  80. $this->assertNull($resource->getAdapter());
  81. }
  82. public function testDbIsNullByDefault()
  83. {
  84. require_once 'Zend/Application/Resource/Db.php';
  85. $resource = new Zend_Application_Resource_Db();
  86. $this->assertNull($resource->getDbAdapter());
  87. }
  88. public function testParamsAreEmptyByDefault()
  89. {
  90. require_once 'Zend/Application/Resource/Db.php';
  91. $resource = new Zend_Application_Resource_Db();
  92. $params = $resource->getParams();
  93. $this->assertTrue(empty($params));
  94. }
  95. public function testIsDefaultTableAdapter()
  96. {
  97. require_once 'Zend/Application/Resource/Db.php';
  98. $resource = new Zend_Application_Resource_Db();
  99. $this->assertTrue($resource->isDefaultTableAdapter());
  100. }
  101. public function testPassingDatabaseConfigurationSetsObjectState()
  102. {
  103. require_once 'Zend/Application/Resource/Db.php';
  104. $config = array(
  105. 'adapter' => 'Pdo_Sqlite',
  106. 'params' => array(
  107. 'dbname' => ':memory:',
  108. ),
  109. 'isDefaultTableAdapter' => false,
  110. );
  111. $resource = new Zend_Application_Resource_Db($config);
  112. $this->assertFalse($resource->isDefaultTableAdapter());
  113. $this->assertEquals($config['adapter'], $resource->getAdapter());
  114. $this->assertEquals($config['params'], $resource->getParams());
  115. }
  116. public function testInitShouldInitializeDbAdapter()
  117. {
  118. require_once 'Zend/Application/Resource/Db.php';
  119. $config = array(
  120. 'adapter' => 'Pdo_Sqlite',
  121. 'params' => array(
  122. 'dbname' => ':memory:',
  123. ),
  124. 'isDefaultTableAdapter' => false,
  125. );
  126. $resource = new Zend_Application_Resource_Db($config);
  127. $resource->init();
  128. $db = $resource->getDbAdapter();
  129. $this->assertTrue($db instanceof Zend_Db_Adapter_Pdo_Sqlite);
  130. }
  131. }
  132. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_DbTest::main') {
  133. Zend_Application_Resource_DbTest::main();
  134. }