DbTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Application_Resource_DbTest extends PHPUnit_Framework_TestCase
  41. {
  42. public static function main()
  43. {
  44. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  45. $result = PHPUnit_TextUI_TestRunner::run($suite);
  46. }
  47. public function setUp()
  48. {
  49. // Store original autoloaders
  50. $this->loaders = spl_autoload_functions();
  51. if (!is_array($this->loaders)) {
  52. // spl_autoload_functions does not return empty array when no
  53. // autoloaders registered...
  54. $this->loaders = array();
  55. }
  56. Zend_Loader_Autoloader::resetInstance();
  57. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  58. $this->application = new Zend_Application('testing');
  59. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  60. $this->bootstrap = new ZfAppBootstrap($this->application);
  61. }
  62. public function tearDown()
  63. {
  64. // Restore original autoloaders
  65. $loaders = spl_autoload_functions();
  66. foreach ($loaders as $loader) {
  67. spl_autoload_unregister($loader);
  68. }
  69. foreach ($this->loaders as $loader) {
  70. spl_autoload_register($loader);
  71. }
  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. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_DbTest::main') {
  130. Zend_Application_Resource_DbTest::main();
  131. }