PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/suites/unit/joomla/database/JDatabaseTest.php

https://github.com/Hackwar/joomla-platform
PHP | 513 lines | 246 code | 45 blank | 222 comment | 0 complexity | 6f893e236986cdb63275e18ebe7822a4 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Database
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. require_once JPATH_PLATFORM . '/joomla/database/database.php';
  10. require_once __DIR__ . '/stubs/nosqldriver.php';
  11. /**
  12. * Test class for JDatabase.
  13. * Generated by PHPUnit on 2009-10-08 at 22:00:38.
  14. *
  15. * @package Joomla.UnitTest
  16. * @subpackage Database
  17. *
  18. * @since 11.1
  19. */
  20. class JDatabaseTest extends TestCaseDatabase
  21. {
  22. /**
  23. * @var JDatabase
  24. * @since 11.4
  25. */
  26. protected $db;
  27. /**
  28. * Sets up the fixture, for example, opens a network connection.
  29. * This method is called before a test is executed.
  30. *
  31. * @return void
  32. */
  33. protected function setUp()
  34. {
  35. $this->db = JDatabase::getInstance(
  36. array(
  37. 'driver' => 'nosql',
  38. 'database' => 'europa',
  39. 'prefix' => '&',
  40. )
  41. );
  42. }
  43. /**
  44. * Tears down the fixture, for example, closes a network connection.
  45. * This method is called after a test is executed.
  46. *
  47. * @return void
  48. */
  49. protected function tearDown()
  50. {
  51. }
  52. /**
  53. * Test for the JDatabase::__call method.
  54. *
  55. * @return void
  56. *
  57. * @since 12.1
  58. */
  59. public function test__callQuote()
  60. {
  61. $this->assertThat(
  62. $this->db->q('foo'),
  63. $this->equalTo($this->db->quote('foo')),
  64. 'Tests the q alias of quote.'
  65. );
  66. }
  67. /**
  68. * Test for the JDatabase::__call method.
  69. *
  70. * @return void
  71. *
  72. * @since 12.1
  73. */
  74. public function test__callQuoteName()
  75. {
  76. $this->assertThat(
  77. $this->db->qn('foo'),
  78. $this->equalTo($this->db->quoteName('foo')),
  79. 'Tests the qn alias of quoteName.'
  80. );
  81. }
  82. /**
  83. * Test for the JDatabase::__call method.
  84. *
  85. * @return void
  86. *
  87. * @since 12.1
  88. */
  89. public function test__callUnknown()
  90. {
  91. $this->assertThat(
  92. $this->db->foo(),
  93. $this->isNull(),
  94. 'Tests for an unknown method.'
  95. );
  96. }
  97. /**
  98. * Test...
  99. *
  100. * @todo Implement test__construct().
  101. *
  102. * @return void
  103. */
  104. public function test__construct()
  105. {
  106. // Remove the following lines when you implement this test.
  107. $this->markTestIncomplete('This test has not been implemented yet.');
  108. }
  109. /**
  110. * Test...
  111. *
  112. * @todo Implement testGetInstance().
  113. *
  114. * @return void
  115. */
  116. public function testGetInstance()
  117. {
  118. // Remove the following lines when you implement this test.
  119. $this->markTestIncomplete('This test has not been implemented yet.');
  120. }
  121. /**
  122. * Test...
  123. *
  124. * @todo Implement test__destruct().
  125. *
  126. * @return void
  127. */
  128. public function test__destruct()
  129. {
  130. // Remove the following lines when you implement this test.
  131. $this->markTestIncomplete('This test has not been implemented yet.');
  132. }
  133. /**
  134. * Tests the JDatabase::getConnection method.
  135. *
  136. * @return void
  137. *
  138. * @since 11.4
  139. */
  140. public function testGetConnection()
  141. {
  142. TestReflection::setValue($this->db, 'connection', 'foo');
  143. $this->assertThat(
  144. $this->db->getConnection(),
  145. $this->equalTo('foo')
  146. );
  147. }
  148. /**
  149. * Test...
  150. *
  151. * @todo Implement testGetConnectors().
  152. *
  153. * @return void
  154. */
  155. public function testGetConnectors()
  156. {
  157. // Remove the following lines when you implement this test.
  158. $this->markTestIncomplete('This test has not been implemented yet.');
  159. }
  160. /**
  161. * Tests the JDatabase::getCount method.
  162. *
  163. * @return void
  164. *
  165. * @since 11.4
  166. */
  167. public function testGetCount()
  168. {
  169. TestReflection::setValue($this->db, 'count', 42);
  170. $this->assertThat(
  171. $this->db->getCount(),
  172. $this->equalTo(42)
  173. );
  174. }
  175. /**
  176. * Tests the JDatabase::getDatabase method.
  177. *
  178. * @return void
  179. *
  180. * @since 11.4
  181. */
  182. public function testGetDatabase()
  183. {
  184. $this->assertThat(
  185. TestReflection::invoke($this->db, 'getDatabase'),
  186. $this->equalTo('europa')
  187. );
  188. }
  189. /**
  190. * Tests the JDatabase::getDateFormat method.
  191. *
  192. * @return void
  193. *
  194. * @since 11.4
  195. */
  196. public function testGetDateFormat()
  197. {
  198. $this->assertThat(
  199. $this->db->getDateFormat(),
  200. $this->equalTo('Y-m-d H:i:s')
  201. );
  202. }
  203. /**
  204. * Tests the JDatabase::splitSql method.
  205. *
  206. * @return void
  207. *
  208. * @since 12.1
  209. */
  210. public function testSplitSql()
  211. {
  212. $this->assertThat(
  213. $this->db->splitSql('SELECT * FROM #__foo;SELECT * FROM #__bar;'),
  214. $this->equalTo(
  215. array(
  216. 'SELECT * FROM #__foo;',
  217. 'SELECT * FROM #__bar;'
  218. )
  219. ),
  220. 'splitSql method should split a string of multiple queries into an array.'
  221. );
  222. }
  223. /**
  224. * Test...
  225. *
  226. * @todo Implement testGetErrorNum().
  227. *
  228. * @return void
  229. */
  230. public function testGetErrorNum()
  231. {
  232. // Remove the following lines when you implement this test.
  233. $this->markTestIncomplete('This test has not been implemented yet.');
  234. }
  235. /**
  236. * Test...
  237. *
  238. * @todo Implement testGetErrorMsg().
  239. *
  240. * @return void
  241. */
  242. public function testGetErrorMsg()
  243. {
  244. // Remove the following lines when you implement this test.
  245. $this->markTestIncomplete('This test has not been implemented yet.');
  246. }
  247. /**
  248. * Tests the JDatabase::getLog method.
  249. *
  250. * @return void
  251. *
  252. * @since 11.4
  253. */
  254. public function testGetLog()
  255. {
  256. TestReflection::setValue($this->db, 'log', 'foo');
  257. $this->assertThat(
  258. $this->db->getLog(),
  259. $this->equalTo('foo')
  260. );
  261. }
  262. /**
  263. * Tests the JDatabase::getPrefix method.
  264. *
  265. * @return void
  266. *
  267. * @since 11.4
  268. */
  269. public function testGetPrefix()
  270. {
  271. $this->assertThat(
  272. $this->db->getPrefix(),
  273. $this->equalTo('&')
  274. );
  275. }
  276. /**
  277. * Tests the JDatabase::getNullDate method.
  278. *
  279. * @return void
  280. *
  281. * @since 11.4
  282. */
  283. public function testGetNullDate()
  284. {
  285. $this->assertThat(
  286. $this->db->getNullDate(),
  287. $this->equalTo('1BC')
  288. );
  289. }
  290. /**
  291. * Tests the JDatabase::getMinimum method.
  292. *
  293. * @return void
  294. *
  295. * @since 12.1
  296. */
  297. public function testGetMinimum()
  298. {
  299. $this->assertThat(
  300. $this->db->getMinimum(),
  301. $this->equalTo('12.1'),
  302. 'getMinimum should return a string with the minimum supported database version number'
  303. );
  304. }
  305. /**
  306. * Tests the JDatabase::isMinimumVersion method.
  307. *
  308. * @return void
  309. *
  310. * @since 12.1
  311. */
  312. public function testIsMinimumVersion()
  313. {
  314. $this->assertThat(
  315. $this->db->isMinimumVersion(),
  316. $this->isTrue(),
  317. 'isMinimumVersion should return a boolean true if the database version is supported by the driver'
  318. );
  319. }
  320. /**
  321. * Tests the JDatabase::setDebug method.
  322. *
  323. * @return void
  324. *
  325. * @since 12.1
  326. */
  327. public function testSetDebug()
  328. {
  329. $this->assertThat(
  330. $this->db->setDebug(true),
  331. $this->isType('boolean'),
  332. 'setDebug should return a boolean value containing the previous debug state.'
  333. );
  334. }
  335. /**
  336. * Tests the JDatabase::setQuery method.
  337. *
  338. * @return void
  339. *
  340. * @since 12.1
  341. */
  342. public function testSetQuery()
  343. {
  344. $this->assertThat(
  345. $this->db->setQuery('SELECT * FROM #__dbtest'),
  346. $this->isInstanceOf('JDatabase'),
  347. 'setQuery method should return an instance of JDatabase.'
  348. );
  349. }
  350. /**
  351. * Tests the JDatabase::replacePrefix method.
  352. *
  353. * @return void
  354. *
  355. * @since 12.1
  356. */
  357. public function testReplacePrefix()
  358. {
  359. $this->assertThat(
  360. $this->db->replacePrefix('SELECT * FROM #__dbtest'),
  361. $this->equalTo('SELECT * FROM &dbtest'),
  362. 'replacePrefix method should return the query string with the #__ prefix replaced by the actual table prefix.'
  363. );
  364. }
  365. /**
  366. * Test...
  367. *
  368. * @todo Implement testStderr().
  369. *
  370. * @return void
  371. */
  372. public function testStderr()
  373. {
  374. // Remove the following lines when you implement this test.
  375. $this->markTestSkipped('Deprecated method');
  376. }
  377. /**
  378. * Tests the JDatabase::quote method.
  379. *
  380. * @return void
  381. *
  382. * @covers JDatabaseDriver::quote
  383. * @since 11.4
  384. */
  385. public function testQuote()
  386. {
  387. $this->assertThat(
  388. $this->db->quote('test', false),
  389. $this->equalTo("'test'"),
  390. 'Tests the without escaping.'
  391. );
  392. $this->assertThat(
  393. $this->db->quote('test'),
  394. $this->equalTo("'-test-'"),
  395. 'Tests the with escaping (default).'
  396. );
  397. $this->assertEquals(
  398. array("'-test1-'", "'-test2-'"),
  399. $this->db->quote(array('test1', 'test2')),
  400. 'Check that the array is quoted.'
  401. );
  402. }
  403. /**
  404. * Tests the JDatabase::quoteName method.
  405. *
  406. * @return void
  407. *
  408. * @since 11.4
  409. */
  410. public function testQuoteName()
  411. {
  412. $this->assertThat(
  413. $this->db->quoteName('test'),
  414. $this->equalTo('[test]'),
  415. 'Tests the left-right quotes on a string.'
  416. );
  417. $this->assertThat(
  418. $this->db->quoteName('a.test'),
  419. $this->equalTo('[a].[test]'),
  420. 'Tests the left-right quotes on a dotted string.'
  421. );
  422. $this->assertThat(
  423. $this->db->quoteName(array('a', 'test')),
  424. $this->equalTo(array('[a]', '[test]')),
  425. 'Tests the left-right quotes on an array.'
  426. );
  427. $this->assertThat(
  428. $this->db->quoteName(array('a.b', 'test.quote')),
  429. $this->equalTo(array('[a].[b]', '[test].[quote]')),
  430. 'Tests the left-right quotes on an array.'
  431. );
  432. $this->assertThat(
  433. $this->db->quoteName(array('a.b', 'test.quote'), array(null, 'alias')),
  434. $this->equalTo(array('[a].[b]', '[test].[quote] AS [alias]')),
  435. 'Tests the left-right quotes on an array.'
  436. );
  437. $this->assertThat(
  438. $this->db->quoteName(array('a.b', 'test.quote'), array('alias1', 'alias2')),
  439. $this->equalTo(array('[a].[b] AS [alias1]', '[test].[quote] AS [alias2]')),
  440. 'Tests the left-right quotes on an array.'
  441. );
  442. $this->assertThat(
  443. $this->db->quoteName((object) array('a', 'test')),
  444. $this->equalTo(array('[a]', '[test]')),
  445. 'Tests the left-right quotes on an object.'
  446. );
  447. TestReflection::setValue($this->db, 'nameQuote', '/');
  448. $this->assertThat(
  449. $this->db->quoteName('test'),
  450. $this->equalTo('/test/'),
  451. 'Tests the uni-quotes on a string.'
  452. );
  453. }
  454. /**
  455. * Tests the JDatabase::truncateTable method.
  456. *
  457. * @return void
  458. *
  459. * @since 12.1
  460. */
  461. public function testTruncateTable()
  462. {
  463. $this->assertNull(
  464. $this->db->truncateTable('#__dbtest'),
  465. 'truncateTable should not return anything if successful.'
  466. );
  467. }
  468. }