PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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