PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Db/Adapter/StaticTest.php

https://github.com/tanduy/zf
PHP | 397 lines | 293 code | 45 blank | 59 comment | 9 complexity | 73d0e3070acf2428342df53e34974282 MD5 | raw file
  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-2010 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-2010 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. protected static $_isCaseSensitiveFileSystem = null;
  51. public function testDbConstructor()
  52. {
  53. $db = new Zend_Db_Adapter_Static( array('dbname' => 'dummy') );
  54. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  55. $this->assertEquals('dummy', $db->config['dbname']);
  56. }
  57. public function testDbConstructorExceptionInvalidOptions()
  58. {
  59. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  60. if ($minor >= 2) {
  61. try {
  62. $db = new Zend_Db_Adapter_Static('scalar');
  63. $this->fail('Expected exception not thrown');
  64. } catch (Exception $e) {
  65. $this->assertContains('Adapter parameters must be in an array or a Zend_Config object', $e->getMessage());
  66. }
  67. } else {
  68. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  69. }
  70. }
  71. public function testDbConstructorZendConfig()
  72. {
  73. $configData1 = array(
  74. 'adapter' => 'Static',
  75. 'params' => array(
  76. 'dbname' => 'dummy'
  77. )
  78. );
  79. $config1 = new Zend_Config($configData1);
  80. $db = new Zend_Db_Adapter_Static($config1->params);
  81. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  82. $this->assertEquals('dummy', $db->config['dbname']);
  83. }
  84. public function testDbFactory()
  85. {
  86. $db = Zend_Db::factory('Static', array('dbname' => 'dummy') );
  87. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  88. $this->assertTrue(class_exists('Zend_Db_Adapter_Static'));
  89. $this->assertType('Zend_Db_Adapter_Static', $db);
  90. $this->assertEquals('dummy', $db->config['dbname']);
  91. }
  92. public function testDbFactoryAlternateNamespace()
  93. {
  94. $ip = get_include_path();
  95. $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
  96. $newIp = $dir . PATH_SEPARATOR . $ip;
  97. set_include_path($newIp);
  98. try {
  99. // this test used to read as 'TestNamespace', but due to ZF-5606 has been changed
  100. $db = Zend_Db::factory('Static', array('dbname' => 'dummy', 'adapterNamespace' => 'Testnamespace'));
  101. } catch (Zend_Exception $e) {
  102. set_include_path($ip);
  103. $this->fail('Caught exception of type '.get_class($e).' where none was expected: '.$e->getMessage());
  104. }
  105. set_include_path($ip);
  106. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  107. $this->assertTrue(class_exists('Zend_Db_Adapter_Static'));
  108. $this->assertType('Zend_Db_Adapter_Static', $db);
  109. $this->assertTrue(class_exists('TestNamespace_Static'));
  110. $this->assertType('TestNamespace_Static', $db);
  111. }
  112. public function testDbFactoryAlternateNamespaceExceptionInvalidAdapter()
  113. {
  114. $ip = get_include_path();
  115. $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
  116. $newIp = $dir . PATH_SEPARATOR . $ip;
  117. set_include_path($newIp);
  118. try {
  119. $db = Zend_Db::factory('Version', array('dbname' => 'dummy', 'adapterNamespace' => 'Zend'));
  120. set_include_path($ip);
  121. $this->fail('Expected to catch Zend_Db_Exception');
  122. } catch (Zend_Exception $e) {
  123. set_include_path($ip);
  124. $this->assertType('Zend_Db_Exception', $e,
  125. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  126. $this->assertEquals("Adapter class 'Zend_Version' does not extend Zend_Db_Adapter_Abstract", $e->getMessage());
  127. }
  128. }
  129. public function testDbFactoryExceptionInvalidDriverName()
  130. {
  131. try {
  132. $db = Zend_Db::factory(null);
  133. $this->fail('Expected to catch Zend_Db_Exception');
  134. } catch (Zend_Exception $e) {
  135. $this->assertType('Zend_Db_Exception', $e,
  136. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  137. $this->assertEquals($e->getMessage(), 'Adapter name must be specified in a string');
  138. }
  139. }
  140. public function testDbFactoryExceptionInvalidOptions()
  141. {
  142. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  143. if ($minor >= 2) {
  144. try {
  145. $db = Zend_Db::factory('Static', 'scalar');
  146. $this->fail('Expected exception not thrown');
  147. } catch (Exception $e) {
  148. $this->assertContains('Adapter parameters must be in an array or a Zend_Config object', $e->getMessage());
  149. }
  150. } else {
  151. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  152. }
  153. }
  154. public function testDbFactoryExceptionNoConfig()
  155. {
  156. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  157. if ($minor >= 2) {
  158. try {
  159. $db = Zend_Db::factory('Static');
  160. $this->fail('Expected exception not thrown');
  161. } catch (Exception $e) {
  162. $this->assertContains('Configuration must have a key for \'dbname\' that names the database instance', $e->getMessage());
  163. }
  164. } else {
  165. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  166. }
  167. }
  168. public function testDbFactoryExceptionNoDatabaseName()
  169. {
  170. try {
  171. $db = Zend_Db::factory('Static', array());
  172. $this->fail('Expected to catch Zend_Db_Adapter_Exception');
  173. } catch (Zend_Exception $e) {
  174. $this->assertType('Zend_Db_Adapter_Exception', $e,
  175. 'Expected exception of type Zend_Db_Adapter_Exception, got '.get_class($e));
  176. $this->assertEquals("Configuration must have a key for 'dbname' that names the database instance", $e->getMessage());
  177. }
  178. }
  179. public function testDbFactoryZendConfig()
  180. {
  181. $configData1 = array(
  182. 'adapter' => 'Static',
  183. 'params' => array(
  184. 'dbname' => 'dummy'
  185. )
  186. );
  187. $config1 = new Zend_Config($configData1);
  188. $db = Zend_Db::factory($config1);
  189. $this->assertType('Zend_Db_Adapter_Static', $db);
  190. $this->assertEquals('dummy', $db->config['dbname']);
  191. }
  192. public function testDbFactoryZendConfigExceptionNoAdapter()
  193. {
  194. $configData1 = array(
  195. 'params' => array(
  196. 'dbname' => 'dummy'
  197. )
  198. );
  199. $config1 = new Zend_Config($configData1);
  200. try {
  201. $db = Zend_Db::factory($config1);
  202. $this->fail('Expected to catch Zend_Db_Exception');
  203. } catch (Zend_Exception $e) {
  204. $this->assertType('Zend_Db_Exception', $e,
  205. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  206. $this->assertEquals($e->getMessage(), 'Adapter name must be specified in a string');
  207. }
  208. }
  209. public function testDbFactoryZendConfigOverrideArray()
  210. {
  211. $configData1 = array(
  212. 'adapter' => 'Static',
  213. 'params' => array(
  214. 'dbname' => 'dummy'
  215. )
  216. );
  217. $configData2 = array(
  218. 'dbname' => 'vanilla'
  219. );
  220. $config1 = new Zend_Config($configData1);
  221. $db = Zend_Db::factory($config1, $configData2);
  222. $this->assertType('Zend_Db_Adapter_Static', $db);
  223. // second arg should be ignored
  224. $this->assertEquals('dummy', $db->config['dbname']);
  225. }
  226. public function testDbFactoryZendConfigOverrideZendConfig()
  227. {
  228. $configData1 = array(
  229. 'adapter' => 'Static',
  230. 'params' => array(
  231. 'dbname' => 'dummy'
  232. )
  233. );
  234. $configData2 = array(
  235. 'dbname' => 'vanilla'
  236. );
  237. $config1 = new Zend_Config($configData1);
  238. $config2 = new Zend_Config($configData2);
  239. $db = Zend_Db::factory($config1, $config2);
  240. $this->assertType('Zend_Db_Adapter_Static', $db);
  241. // second arg should be ignored
  242. $this->assertEquals('dummy', $db->config['dbname']);
  243. }
  244. public function testDbGetConnection()
  245. {
  246. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  247. $conn = $db->getConnection();
  248. $this->assertType('Zend_Db_Adapter_Static', $conn);
  249. }
  250. public function testDbGetFetchMode()
  251. {
  252. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  253. $mode = $db->getFetchMode();
  254. $this->assertType('integer', $mode);
  255. }
  256. /**
  257. * @group ZF-5099
  258. */
  259. public function testDbGetServerVersion()
  260. {
  261. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  262. $version = $db->getServerVersion();
  263. $this->assertEquals($version, '5.6.7.8');
  264. $this->assertTrue(version_compare($version, '1.0.0', '>'));
  265. $this->assertTrue(version_compare($version, '99.0.0', '<'));
  266. }
  267. /**
  268. * @group ZF-5050
  269. */
  270. public function testDbCloseConnection()
  271. {
  272. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  273. $db->getConnection();
  274. $this->assertTrue($db->isConnected());
  275. $db->closeConnection();
  276. $this->assertFalse($db->isConnected());
  277. }
  278. /**
  279. * @group ZF-5606
  280. */
  281. public function testDbFactoryDoesNotNormalizeNamespace()
  282. {
  283. $newIncludePath = realpath(dirname(__FILE__) . '/_files/') . PATH_SEPARATOR . get_include_path();
  284. $oldIncludePath = set_include_path($newIncludePath);
  285. try {
  286. $adapter = Zend_Db::factory(
  287. 'Dbadapter',
  288. array('dbname' => 'dummy', 'adapterNamespace' => 'Test_MyCompany1')
  289. );
  290. } catch (Exception $e) {
  291. set_include_path($oldIncludePath);
  292. $this->fail('Could not load file for reason: ' . $e->getMessage());
  293. }
  294. $this->assertEquals('Test_MyCompany1_Dbadapter', get_class($adapter));
  295. set_include_path($oldIncludePath);
  296. }
  297. /**
  298. * @group ZF-5606
  299. */
  300. public function testDbFactoryWillThrowExceptionWhenAssumingBadBehavior()
  301. {
  302. $newIncludePath = realpath(dirname(__FILE__) . '/_files/') . PATH_SEPARATOR . get_include_path();
  303. $oldIncludePath = set_include_path($newIncludePath);
  304. if (!$this->_isCaseSensitiveFileSystem()) {
  305. set_include_path($oldIncludePath);
  306. $this->markTestSkipped('This test is irrelevant on case-inspecific file systems.');
  307. return;
  308. }
  309. try {
  310. $adapter = Zend_Db::factory(
  311. 'Dbadapter',
  312. array('dbname' => 'dummy', 'adapterNamespace' => 'Test_MyCompany2')
  313. );
  314. } catch (Exception $e) {
  315. set_include_path($oldIncludePath);
  316. $this->assertContains('failed to open stream', $e->getMessage());
  317. return;
  318. }
  319. $this->assertFalse($adapter instanceof Test_Mycompany2_Dbadapter);
  320. set_include_path($oldIncludePath);
  321. }
  322. /**
  323. * @group ZF-7924
  324. */
  325. public function testDbFactoryWillLoadCaseInsensitiveAdapterName()
  326. {
  327. $newIncludePath = realpath(dirname(__FILE__) . '/_files/') . PATH_SEPARATOR . get_include_path();
  328. $oldIncludePath = set_include_path($newIncludePath);
  329. try {
  330. $adapter = Zend_Db::factory(
  331. 'DB_ADAPTER',
  332. array('dbname' => 'dummy', 'adapterNamespace' => 'Test_MyCompany1')
  333. );
  334. } catch (Exception $e) {
  335. set_include_path($oldIncludePath);
  336. $this->fail('Could not load file for reason: ' . $e->getMessage());
  337. }
  338. $this->assertEquals('Test_MyCompany1_Db_Adapter', get_class($adapter));
  339. set_include_path($oldIncludePath);
  340. }
  341. protected function _isCaseSensitiveFileSystem()
  342. {
  343. if (self::$_isCaseSensitiveFileSystem === null) {
  344. self::$_isCaseSensitiveFileSystem = !(@include 'Test/MyCompany1/iscasespecific.php');
  345. }
  346. return self::$_isCaseSensitiveFileSystem;
  347. }
  348. public function getDriver()
  349. {
  350. return 'Static';
  351. }
  352. }