PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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