StaticTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_Db
  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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  27. /**
  28. * @see Zend_Db
  29. */
  30. require_once 'Zend/Db.php';
  31. /**
  32. * @see Zend_Config
  33. */
  34. require_once 'Zend/Config.php';
  35. /**
  36. * @see Zend_Db_Adapter_Static
  37. */
  38. require_once 'Zend/Db/Adapter/Static.php';
  39. /**
  40. * @category Zend
  41. * @package Zend_Db
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. */
  46. class Zend_Db_Adapter_StaticTest extends PHPUnit_Framework_TestCase
  47. {
  48. public function testDbConstructor()
  49. {
  50. $db = new Zend_Db_Adapter_Static( array('dbname' => 'dummy') );
  51. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  52. $this->assertEquals('dummy', $db->config['dbname']);
  53. }
  54. public function testDbConstructorExceptionInvalidOptions()
  55. {
  56. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  57. if ($minor >= 2) {
  58. try {
  59. $db = new Zend_Db_Adapter_Static('scalar');
  60. $this->fail('Expected exception not thrown');
  61. } catch (Exception $e) {
  62. $this->assertContains('Adapter parameters must be in an array or a Zend_Config object', $e->getMessage());
  63. }
  64. } else {
  65. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  66. }
  67. }
  68. public function testDbConstructorZendConfig()
  69. {
  70. $configData1 = array(
  71. 'adapter' => 'Static',
  72. 'params' => array(
  73. 'dbname' => 'dummy'
  74. )
  75. );
  76. $config1 = new Zend_Config($configData1);
  77. $db = new Zend_Db_Adapter_Static($config1->params);
  78. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  79. $this->assertEquals('dummy', $db->config['dbname']);
  80. }
  81. public function testDbFactory()
  82. {
  83. $db = Zend_Db::factory('Static', array('dbname' => 'dummy') );
  84. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  85. $this->assertTrue(class_exists('Zend_Db_Adapter_Static'));
  86. $this->assertType('Zend_Db_Adapter_Static', $db);
  87. $this->assertEquals('dummy', $db->config['dbname']);
  88. }
  89. public function testDbFactoryAlternateNamespace()
  90. {
  91. $ip = get_include_path();
  92. $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
  93. $newIp = $dir . PATH_SEPARATOR . $ip;
  94. set_include_path($newIp);
  95. try {
  96. $db = Zend_Db::factory('Static', array('dbname' => 'dummy', 'adapterNamespace' => 'TestNamespace'));
  97. } catch (Zend_Exception $e) {
  98. set_include_path($ip);
  99. $this->fail('Caught exception of type '.get_class($e).' where none was expected: '.$e->getMessage());
  100. }
  101. set_include_path($ip);
  102. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  103. $this->assertTrue(class_exists('Zend_Db_Adapter_Static'));
  104. $this->assertType('Zend_Db_Adapter_Static', $db);
  105. $this->assertTrue(class_exists('TestNamespace_Static'));
  106. $this->assertType('TestNamespace_Static', $db);
  107. }
  108. public function testDbFactoryAlternateNamespaceExceptionInvalidAdapter()
  109. {
  110. $ip = get_include_path();
  111. $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
  112. $newIp = $dir . PATH_SEPARATOR . $ip;
  113. set_include_path($newIp);
  114. try {
  115. $db = Zend_Db::factory('Version', array('dbname' => 'dummy', 'adapterNamespace' => 'Zend'));
  116. set_include_path($ip);
  117. $this->fail('Expected to catch Zend_Db_Exception');
  118. } catch (Zend_Exception $e) {
  119. set_include_path($ip);
  120. $this->assertType('Zend_Db_Exception', $e,
  121. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  122. $this->assertEquals("Adapter class 'Zend_Version' does not extend Zend_Db_Adapter_Abstract", $e->getMessage());
  123. }
  124. }
  125. public function testDbFactoryExceptionInvalidDriverName()
  126. {
  127. try {
  128. $db = Zend_Db::factory(null);
  129. $this->fail('Expected to catch Zend_Db_Exception');
  130. } catch (Zend_Exception $e) {
  131. $this->assertType('Zend_Db_Exception', $e,
  132. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  133. $this->assertEquals($e->getMessage(), 'Adapter name must be specified in a string');
  134. }
  135. }
  136. public function testDbFactoryExceptionInvalidOptions()
  137. {
  138. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  139. if ($minor >= 2) {
  140. try {
  141. $db = Zend_Db::factory('Static', 'scalar');
  142. $this->fail('Expected exception not thrown');
  143. } catch (Exception $e) {
  144. $this->assertContains('Adapter parameters must be in an array or a Zend_Config object', $e->getMessage());
  145. }
  146. } else {
  147. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  148. }
  149. }
  150. public function testDbFactoryExceptionNoConfig()
  151. {
  152. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  153. if ($minor >= 2) {
  154. try {
  155. $db = Zend_Db::factory('Static');
  156. $this->fail('Expected exception not thrown');
  157. } catch (Exception $e) {
  158. $this->assertContains('Configuration must have a key for \'dbname\' that names the database instance', $e->getMessage());
  159. }
  160. } else {
  161. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  162. }
  163. }
  164. public function testDbFactoryExceptionNoDatabaseName()
  165. {
  166. try {
  167. $db = Zend_Db::factory('Static', array());
  168. $this->fail('Expected to catch Zend_Db_Adapter_Exception');
  169. } catch (Zend_Exception $e) {
  170. $this->assertType('Zend_Db_Adapter_Exception', $e,
  171. 'Expected exception of type Zend_Db_Adapter_Exception, got '.get_class($e));
  172. $this->assertEquals("Configuration must have a key for 'dbname' that names the database instance", $e->getMessage());
  173. }
  174. }
  175. public function testDbFactoryZendConfig()
  176. {
  177. $configData1 = array(
  178. 'adapter' => 'Static',
  179. 'params' => array(
  180. 'dbname' => 'dummy'
  181. )
  182. );
  183. $config1 = new Zend_Config($configData1);
  184. $db = Zend_Db::factory($config1);
  185. $this->assertType('Zend_Db_Adapter_Static', $db);
  186. $this->assertEquals('dummy', $db->config['dbname']);
  187. }
  188. public function testDbFactoryZendConfigExceptionNoAdapter()
  189. {
  190. $configData1 = array(
  191. 'params' => array(
  192. 'dbname' => 'dummy'
  193. )
  194. );
  195. $config1 = new Zend_Config($configData1);
  196. try {
  197. $db = Zend_Db::factory($config1);
  198. $this->fail('Expected to catch Zend_Db_Exception');
  199. } catch (Zend_Exception $e) {
  200. $this->assertType('Zend_Db_Exception', $e,
  201. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  202. $this->assertEquals($e->getMessage(), 'Adapter name must be specified in a string');
  203. }
  204. }
  205. public function testDbFactoryZendConfigOverrideArray()
  206. {
  207. $configData1 = array(
  208. 'adapter' => 'Static',
  209. 'params' => array(
  210. 'dbname' => 'dummy'
  211. )
  212. );
  213. $configData2 = array(
  214. 'dbname' => 'vanilla'
  215. );
  216. $config1 = new Zend_Config($configData1);
  217. $db = Zend_Db::factory($config1, $configData2);
  218. $this->assertType('Zend_Db_Adapter_Static', $db);
  219. // second arg should be ignored
  220. $this->assertEquals('dummy', $db->config['dbname']);
  221. }
  222. public function testDbFactoryZendConfigOverrideZendConfig()
  223. {
  224. $configData1 = array(
  225. 'adapter' => 'Static',
  226. 'params' => array(
  227. 'dbname' => 'dummy'
  228. )
  229. );
  230. $configData2 = array(
  231. 'dbname' => 'vanilla'
  232. );
  233. $config1 = new Zend_Config($configData1);
  234. $config2 = new Zend_Config($configData2);
  235. $db = Zend_Db::factory($config1, $config2);
  236. $this->assertType('Zend_Db_Adapter_Static', $db);
  237. // second arg should be ignored
  238. $this->assertEquals('dummy', $db->config['dbname']);
  239. }
  240. public function testDbGetConnection()
  241. {
  242. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  243. $conn = $db->getConnection();
  244. $this->assertType('Zend_Db_Adapter_Static', $conn);
  245. }
  246. public function testDbGetFetchMode()
  247. {
  248. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  249. $mode = $db->getFetchMode();
  250. $this->assertType('integer', $mode);
  251. }
  252. /**
  253. * @group ZF-5099
  254. */
  255. public function testDbGetServerVersion()
  256. {
  257. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  258. $version = $db->getServerVersion();
  259. $this->assertEquals($version, '5.6.7.8');
  260. $this->assertTrue(version_compare($version, '1.0.0', '>'));
  261. $this->assertTrue(version_compare($version, '99.0.0', '<'));
  262. }
  263. /**
  264. * @group ZF-5050
  265. */
  266. public function testDbCloseConnection()
  267. {
  268. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  269. $db->getConnection();
  270. $this->assertTrue($db->isConnected());
  271. $db->closeConnection();
  272. $this->assertFalse($db->isConnected());
  273. }
  274. public function getDriver()
  275. {
  276. return 'Static';
  277. }
  278. }