StaticTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. // this test used to read as 'TestNamespace', but due to ZF-5606 has been changed
  99. $db = Zend_Db::factory('Static', array('dbname' => 'dummy', 'adapterNamespace' => 'Testnamespace'));
  100. } catch (Zend_Exception $e) {
  101. set_include_path($ip);
  102. $this->fail('Caught exception of type '.get_class($e).' where none was expected: '.$e->getMessage());
  103. }
  104. set_include_path($ip);
  105. $this->assertType('Zend_Db_Adapter_Abstract', $db);
  106. $this->assertTrue(class_exists('Zend_Db_Adapter_Static'));
  107. $this->assertType('Zend_Db_Adapter_Static', $db);
  108. $this->assertTrue(class_exists('TestNamespace_Static'));
  109. $this->assertType('TestNamespace_Static', $db);
  110. }
  111. public function testDbFactoryAlternateNamespaceExceptionInvalidAdapter()
  112. {
  113. $ip = get_include_path();
  114. $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
  115. $newIp = $dir . PATH_SEPARATOR . $ip;
  116. set_include_path($newIp);
  117. try {
  118. $db = Zend_Db::factory('Version', array('dbname' => 'dummy', 'adapterNamespace' => 'Zend'));
  119. set_include_path($ip);
  120. $this->fail('Expected to catch Zend_Db_Exception');
  121. } catch (Zend_Exception $e) {
  122. set_include_path($ip);
  123. $this->assertType('Zend_Db_Exception', $e,
  124. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  125. $this->assertEquals("Adapter class 'Zend_Version' does not extend Zend_Db_Adapter_Abstract", $e->getMessage());
  126. }
  127. }
  128. public function testDbFactoryExceptionInvalidDriverName()
  129. {
  130. try {
  131. $db = Zend_Db::factory(null);
  132. $this->fail('Expected to catch Zend_Db_Exception');
  133. } catch (Zend_Exception $e) {
  134. $this->assertType('Zend_Db_Exception', $e,
  135. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  136. $this->assertEquals($e->getMessage(), 'Adapter name must be specified in a string');
  137. }
  138. }
  139. public function testDbFactoryExceptionInvalidOptions()
  140. {
  141. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  142. if ($minor >= 2) {
  143. try {
  144. $db = Zend_Db::factory('Static', 'scalar');
  145. $this->fail('Expected exception not thrown');
  146. } catch (Exception $e) {
  147. $this->assertContains('Adapter parameters must be in an array or a Zend_Config object', $e->getMessage());
  148. }
  149. } else {
  150. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  151. }
  152. }
  153. public function testDbFactoryExceptionNoConfig()
  154. {
  155. list($major, $minor, $revision) = explode('.', PHP_VERSION);
  156. if ($minor >= 2) {
  157. try {
  158. $db = Zend_Db::factory('Static');
  159. $this->fail('Expected exception not thrown');
  160. } catch (Exception $e) {
  161. $this->assertContains('Configuration must have a key for \'dbname\' that names the database instance', $e->getMessage());
  162. }
  163. } else {
  164. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  165. }
  166. }
  167. public function testDbFactoryExceptionNoDatabaseName()
  168. {
  169. try {
  170. $db = Zend_Db::factory('Static', array());
  171. $this->fail('Expected to catch Zend_Db_Adapter_Exception');
  172. } catch (Zend_Exception $e) {
  173. $this->assertType('Zend_Db_Adapter_Exception', $e,
  174. 'Expected exception of type Zend_Db_Adapter_Exception, got '.get_class($e));
  175. $this->assertEquals("Configuration must have a key for 'dbname' that names the database instance", $e->getMessage());
  176. }
  177. }
  178. public function testDbFactoryZendConfig()
  179. {
  180. $configData1 = array(
  181. 'adapter' => 'Static',
  182. 'params' => array(
  183. 'dbname' => 'dummy'
  184. )
  185. );
  186. $config1 = new Zend_Config($configData1);
  187. $db = Zend_Db::factory($config1);
  188. $this->assertType('Zend_Db_Adapter_Static', $db);
  189. $this->assertEquals('dummy', $db->config['dbname']);
  190. }
  191. public function testDbFactoryZendConfigExceptionNoAdapter()
  192. {
  193. $configData1 = array(
  194. 'params' => array(
  195. 'dbname' => 'dummy'
  196. )
  197. );
  198. $config1 = new Zend_Config($configData1);
  199. try {
  200. $db = Zend_Db::factory($config1);
  201. $this->fail('Expected to catch Zend_Db_Exception');
  202. } catch (Zend_Exception $e) {
  203. $this->assertType('Zend_Db_Exception', $e,
  204. 'Expected exception of type Zend_Db_Exception, got '.get_class($e));
  205. $this->assertEquals($e->getMessage(), 'Adapter name must be specified in a string');
  206. }
  207. }
  208. public function testDbFactoryZendConfigOverrideArray()
  209. {
  210. $configData1 = array(
  211. 'adapter' => 'Static',
  212. 'params' => array(
  213. 'dbname' => 'dummy'
  214. )
  215. );
  216. $configData2 = array(
  217. 'dbname' => 'vanilla'
  218. );
  219. $config1 = new Zend_Config($configData1);
  220. $db = Zend_Db::factory($config1, $configData2);
  221. $this->assertType('Zend_Db_Adapter_Static', $db);
  222. // second arg should be ignored
  223. $this->assertEquals('dummy', $db->config['dbname']);
  224. }
  225. public function testDbFactoryZendConfigOverrideZendConfig()
  226. {
  227. $configData1 = array(
  228. 'adapter' => 'Static',
  229. 'params' => array(
  230. 'dbname' => 'dummy'
  231. )
  232. );
  233. $configData2 = array(
  234. 'dbname' => 'vanilla'
  235. );
  236. $config1 = new Zend_Config($configData1);
  237. $config2 = new Zend_Config($configData2);
  238. $db = Zend_Db::factory($config1, $config2);
  239. $this->assertType('Zend_Db_Adapter_Static', $db);
  240. // second arg should be ignored
  241. $this->assertEquals('dummy', $db->config['dbname']);
  242. }
  243. public function testDbGetConnection()
  244. {
  245. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  246. $conn = $db->getConnection();
  247. $this->assertType('Zend_Db_Adapter_Static', $conn);
  248. }
  249. public function testDbGetFetchMode()
  250. {
  251. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  252. $mode = $db->getFetchMode();
  253. $this->assertType('integer', $mode);
  254. }
  255. /**
  256. * @group ZF-5099
  257. */
  258. public function testDbGetServerVersion()
  259. {
  260. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  261. $version = $db->getServerVersion();
  262. $this->assertEquals($version, '5.6.7.8');
  263. $this->assertTrue(version_compare($version, '1.0.0', '>'));
  264. $this->assertTrue(version_compare($version, '99.0.0', '<'));
  265. }
  266. /**
  267. * @group ZF-5050
  268. */
  269. public function testDbCloseConnection()
  270. {
  271. $db = Zend_Db::factory('Static', array('dbname' => 'dummy'));
  272. $db->getConnection();
  273. $this->assertTrue($db->isConnected());
  274. $db->closeConnection();
  275. $this->assertFalse($db->isConnected());
  276. }
  277. /**
  278. * @group ZF-5606
  279. */
  280. public function testDbFactoryDoesNotNormalizeNamespace()
  281. {
  282. $newIncludePath = realpath(dirname(__FILE__) . '/_files/') . PATH_SEPARATOR . get_include_path();
  283. $oldIncludePath = set_include_path($newIncludePath);
  284. try {
  285. $adapter = Zend_Db::factory(
  286. 'Dbadapter',
  287. array('dbname' => 'dummy', 'adapterNamespace' => 'Test_MyCompany1')
  288. );
  289. } catch (Exception $e) {
  290. set_include_path($oldIncludePath);
  291. $this->fail('Could not load file for reason: ' . $e->getMessage());
  292. }
  293. $this->assertEquals('Test_MyCompany1_Dbadapter', get_class($adapter));
  294. set_include_path($oldIncludePath);
  295. }
  296. /**
  297. * @group ZF-5606
  298. */
  299. public function testDbFactoryWillThrowExceptionWhenAssumingBadBehavior()
  300. {
  301. $newIncludePath = realpath(dirname(__FILE__) . '/_files/') . PATH_SEPARATOR . get_include_path();
  302. $oldIncludePath = set_include_path($newIncludePath);
  303. try {
  304. $adapter = Zend_Db::factory(
  305. 'Dbadapter',
  306. array('dbname' => 'dummy', 'adapterNamespace' => 'Test_MyCompany2')
  307. );
  308. } catch (Exception $e) {
  309. set_include_path($oldIncludePath);
  310. $this->assertContains('failed to open stream', $e->getMessage());
  311. return;
  312. }
  313. $this->assertFalse(class_exists('Test_MyCompany2_Dbadapter'));
  314. set_include_path($oldIncludePath);
  315. }
  316. public function getDriver()
  317. {
  318. return 'Static';
  319. }
  320. }