StaticTest.php 10 KB

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