PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Joomla/Database/Tests/DriverSqliteTest.php

https://github.com/piotr-cz/joomla-framework
PHP | 732 lines | 368 code | 85 blank | 279 comment | 2 complexity | 026293a598f25b54d43bfb84d4c07c87 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE
  5. */
  6. namespace Joomla\Database\Tests;
  7. use Joomla\Test\TestDatabase;
  8. /**
  9. * Test class for Joomla\Database\Sqlite\SqliteDriver.
  10. *
  11. * @since 1.0
  12. */
  13. class DriverSqliteTest extends TestDatabase
  14. {
  15. /**
  16. * Data for the testEscape test.
  17. *
  18. * @return array
  19. *
  20. * @since 1.0
  21. */
  22. public function dataTestEscape()
  23. {
  24. return array(
  25. array("'%_abc123", false, "''%_abc123"),
  26. array("'%_abc123", true, "''%_abc123"),
  27. array(3, false, 3)
  28. );
  29. }
  30. /**
  31. * Data for the testTransactionRollback test.
  32. *
  33. * @return array
  34. *
  35. * @since 1.0
  36. */
  37. public function dataTestTransactionRollback()
  38. {
  39. return array(array(null, 0), array('transactionSavepoint', 1));
  40. }
  41. /**
  42. * Test __destruct method.
  43. *
  44. * @return void
  45. *
  46. * @since 1.0
  47. */
  48. public function test__destruct()
  49. {
  50. $this->markTestIncomplete('This test has not been implemented yet.');
  51. }
  52. /**
  53. * Test connected method.
  54. *
  55. * @return void
  56. *
  57. * @since 1.0
  58. */
  59. public function testConnected()
  60. {
  61. $this->markTestIncomplete('This test has not been implemented yet.');
  62. }
  63. /**
  64. * Tests the dropTable method.
  65. *
  66. * @return void
  67. *
  68. * @since 1.0
  69. */
  70. public function testDropTable()
  71. {
  72. $this->assertThat(
  73. self::$driver->dropTable('#__bar', true),
  74. $this->isInstanceOf('\\Joomla\\Database\\Sqlite\\SqliteDriver'),
  75. 'The table is dropped if present.'
  76. );
  77. }
  78. /**
  79. * Tests the escape method.
  80. *
  81. * @param string $text The string to be escaped.
  82. * @param boolean $extra Optional parameter to provide extra escaping.
  83. * @param string $expected The expected result.
  84. *
  85. * @return void
  86. *
  87. * @dataProvider dataTestEscape
  88. * @since 1.0
  89. */
  90. public function testEscape($text, $extra, $expected)
  91. {
  92. $this->assertThat(
  93. self::$driver->escape($text, $extra),
  94. $this->equalTo($expected),
  95. 'The string was not escaped properly'
  96. );
  97. }
  98. /**
  99. * Test the execute method
  100. *
  101. * @return void
  102. *
  103. * @since 1.0
  104. */
  105. public function testExecute()
  106. {
  107. self::$driver->setQuery("REPLACE INTO `jos_dbtest` (`id`, `title`) VALUES (5, 'testTitle')");
  108. $this->assertThat(self::$driver->execute(), $this->isInstanceOf('\\PDOStatement'), __LINE__);
  109. $this->assertThat(self::$driver->insertid(), $this->equalTo(5), __LINE__);
  110. }
  111. /**
  112. * Test getAffectedRows method.
  113. *
  114. * @return void
  115. *
  116. * @since 1.0
  117. */
  118. public function testGetAffectedRows()
  119. {
  120. $query = self::$driver->getQuery(true);
  121. $query->delete();
  122. $query->from('jos_dbtest');
  123. self::$driver->setQuery($query);
  124. self::$driver->execute();
  125. $this->assertThat(self::$driver->getAffectedRows(), $this->equalTo(4), __LINE__);
  126. }
  127. /**
  128. * Test getExporter method.
  129. *
  130. * @return void
  131. *
  132. * @since 1.0
  133. * @todo Implement testGetExporter().
  134. */
  135. public function testGetExporter()
  136. {
  137. // Remove the following lines when you implement this test.
  138. $this->markTestIncomplete('Implement this test when the exporter is added.');
  139. }
  140. /**
  141. * Test getImporter method.
  142. *
  143. * @return void
  144. *
  145. * @since 1.0
  146. * @todo Implement testGetImporter().
  147. */
  148. public function testGetImporter()
  149. {
  150. // Remove the following lines when you implement this test.
  151. $this->markTestIncomplete('Implement this test when the importer is added.');
  152. }
  153. /**
  154. * Test getNumRows method.
  155. *
  156. * @return void
  157. *
  158. * @since 1.0
  159. */
  160. public function testGetNumRows()
  161. {
  162. $query = self::$driver->getQuery(true);
  163. $query->select('*');
  164. $query->from('jos_dbtest');
  165. $query->where('description = ' . self::$driver->quote('one'));
  166. self::$driver->setQuery($query);
  167. $res = self::$driver->execute();
  168. $this->assertThat(self::$driver->getNumRows($res), $this->equalTo(0), __LINE__);
  169. }
  170. /**
  171. * Tests the getTableCreate method.
  172. *
  173. * @return void
  174. *
  175. * @since 1.0
  176. */
  177. public function testGetTableCreate()
  178. {
  179. $this->assertThat(
  180. self::$driver->getTableCreate('#__dbtest'),
  181. $this->isType('array'),
  182. 'The statement to create the table is returned in an array.'
  183. );
  184. }
  185. /**
  186. * Test getTableColumns function.
  187. *
  188. * @return void
  189. *
  190. * @since 1.0
  191. */
  192. public function testGetTableColumns()
  193. {
  194. $tableCol = array('id' => 'INTEGER', 'title' => 'TEXT', 'start_date' => 'TEXT', 'description' => 'TEXT');
  195. $this->assertThat(
  196. self::$driver->getTableColumns('jos_dbtest'),
  197. $this->equalTo($tableCol),
  198. __LINE__
  199. );
  200. /* not only type field */
  201. $id = new \stdClass;
  202. $id->Default = null;
  203. $id->Field = 'id';
  204. $id->Type = 'INTEGER';
  205. $id->Null = 'YES';
  206. $id->Key = 'PRI';
  207. $title = new \stdClass;
  208. $title->Default = '\'\'';
  209. $title->Field = 'title';
  210. $title->Type = 'TEXT';
  211. $title->Null = 'NO';
  212. $title->Key = '';
  213. $start_date = new \stdClass;
  214. $start_date->Default = '\'\'';
  215. $start_date->Field = 'start_date';
  216. $start_date->Type = 'TEXT';
  217. $start_date->Null = 'NO';
  218. $start_date->Key = '';
  219. $description = new \stdClass;
  220. $description->Default = '\'\'';
  221. $description->Field = 'description';
  222. $description->Type = 'TEXT';
  223. $description->Null = 'NO';
  224. $description->Key = '';
  225. $this->assertThat(
  226. self::$driver->getTableColumns('jos_dbtest', false),
  227. $this->equalTo(
  228. array(
  229. 'id' => $id,
  230. 'title' => $title,
  231. 'start_date' => $start_date,
  232. 'description' => $description
  233. )
  234. ),
  235. __LINE__
  236. );
  237. }
  238. /**
  239. * Tests the getTableKeys method.
  240. *
  241. * @return void
  242. *
  243. * @since 1.0
  244. */
  245. public function testGetTableKeys()
  246. {
  247. $this->assertThat(
  248. self::$driver->getTableKeys('#__dbtest'),
  249. $this->isType('array'),
  250. 'The list of keys for the table is returned in an array.'
  251. );
  252. }
  253. /**
  254. * Tests the getTableList method.
  255. *
  256. * @return void
  257. *
  258. * @since 1.0
  259. */
  260. public function testGetTableList()
  261. {
  262. $this->assertThat(
  263. self::$driver->getTableList(),
  264. $this->isType('array'),
  265. 'The list of tables for the database is returned in an array.'
  266. );
  267. }
  268. /**
  269. * Test getVersion method.
  270. *
  271. * @return void
  272. *
  273. * @since 1.0
  274. */
  275. public function testGetVersion()
  276. {
  277. $this->assertThat(
  278. strlen(self::$driver->getVersion()),
  279. $this->greaterThan(0),
  280. 'Line:' . __LINE__ . ' The getVersion method should return something without error.'
  281. );
  282. }
  283. /**
  284. * Test insertid method.
  285. *
  286. * @return void
  287. *
  288. * @since 1.0
  289. */
  290. public function testInsertid()
  291. {
  292. $this->markTestIncomplete('This test has not been implemented yet.');
  293. }
  294. /**
  295. * Test insertObject method.
  296. *
  297. * @return void
  298. *
  299. * @since 1.0
  300. */
  301. public function testInsertObject()
  302. {
  303. $this->markTestIncomplete('This test has not been implemented yet.');
  304. }
  305. /**
  306. * Test isSupported method.
  307. *
  308. * @return void
  309. *
  310. * @since 1.0
  311. */
  312. public function testIsSupported()
  313. {
  314. $this->assertThat(\Joomla\Database\Sqlite\SqliteDriver::isSupported(), $this->isTrue(), __LINE__);
  315. }
  316. /**
  317. * Test loadAssoc method.
  318. *
  319. * @return void
  320. *
  321. * @since 1.0
  322. */
  323. public function testLoadAssoc()
  324. {
  325. $query = self::$driver->getQuery(true);
  326. $query->select('title');
  327. $query->from('jos_dbtest');
  328. self::$driver->setQuery($query);
  329. $result = self::$driver->loadAssoc();
  330. $this->assertThat($result, $this->equalTo(array('title' => 'Testing')), __LINE__);
  331. }
  332. /**
  333. * Test loadAssocList method.
  334. *
  335. * @return void
  336. *
  337. * @since 1.0
  338. */
  339. public function testLoadAssocList()
  340. {
  341. $query = self::$driver->getQuery(true);
  342. $query->select('title');
  343. $query->from('jos_dbtest');
  344. self::$driver->setQuery($query);
  345. $result = self::$driver->loadAssocList();
  346. $this->assertThat(
  347. $result,
  348. $this->equalTo(
  349. array(
  350. array('title' => 'Testing'),
  351. array('title' => 'Testing2'),
  352. array('title' => 'Testing3'),
  353. array('title' => 'Testing4')
  354. )
  355. ),
  356. __LINE__
  357. );
  358. }
  359. /**
  360. * Test loadColumn method
  361. *
  362. * @return void
  363. *
  364. * @since 1.0
  365. */
  366. public function testLoadColumn()
  367. {
  368. $query = self::$driver->getQuery(true);
  369. $query->select('title');
  370. $query->from('jos_dbtest');
  371. self::$driver->setQuery($query);
  372. $result = self::$driver->loadColumn();
  373. $this->assertThat($result, $this->equalTo(array('Testing', 'Testing2', 'Testing3', 'Testing4')), __LINE__);
  374. }
  375. /**
  376. * Test loadObject method
  377. *
  378. * @return void
  379. *
  380. * @since 1.0
  381. */
  382. public function testLoadObject()
  383. {
  384. $query = self::$driver->getQuery(true);
  385. $query->select('*');
  386. $query->from('jos_dbtest');
  387. $query->where('description=' . self::$driver->quote('three'));
  388. self::$driver->setQuery($query);
  389. $result = self::$driver->loadObject();
  390. $objCompare = new \stdClass;
  391. $objCompare->id = 3;
  392. $objCompare->title = 'Testing3';
  393. $objCompare->start_date = '1980-04-18 00:00:00';
  394. $objCompare->description = 'three';
  395. $this->assertThat($result, $this->equalTo($objCompare), __LINE__);
  396. }
  397. /**
  398. * Test loadObjectList method
  399. *
  400. * @return void
  401. *
  402. * @since 1.0
  403. */
  404. public function testLoadObjectList()
  405. {
  406. $query = self::$driver->getQuery(true);
  407. $query->select('*');
  408. $query->from('jos_dbtest');
  409. $query->order('id');
  410. self::$driver->setQuery($query);
  411. $result = self::$driver->loadObjectList();
  412. $expected = array();
  413. $objCompare = new \stdClass;
  414. $objCompare->id = 1;
  415. $objCompare->title = 'Testing';
  416. $objCompare->start_date = '1980-04-18 00:00:00';
  417. $objCompare->description = 'one';
  418. $expected[] = clone $objCompare;
  419. $objCompare = new \stdClass;
  420. $objCompare->id = 2;
  421. $objCompare->title = 'Testing2';
  422. $objCompare->start_date = '1980-04-18 00:00:00';
  423. $objCompare->description = 'one';
  424. $expected[] = clone $objCompare;
  425. $objCompare = new \stdClass;
  426. $objCompare->id = 3;
  427. $objCompare->title = 'Testing3';
  428. $objCompare->start_date = '1980-04-18 00:00:00';
  429. $objCompare->description = 'three';
  430. $expected[] = clone $objCompare;
  431. $objCompare = new \stdClass;
  432. $objCompare->id = 4;
  433. $objCompare->title = 'Testing4';
  434. $objCompare->start_date = '1980-04-18 00:00:00';
  435. $objCompare->description = 'four';
  436. $expected[] = clone $objCompare;
  437. $this->assertThat($result, $this->equalTo($expected), __LINE__);
  438. }
  439. /**
  440. * Test loadResult method
  441. *
  442. * @return void
  443. *
  444. * @since 1.0
  445. */
  446. public function testLoadResult()
  447. {
  448. $query = self::$driver->getQuery(true);
  449. $query->select('id');
  450. $query->from('jos_dbtest');
  451. $query->where('title=' . self::$driver->quote('Testing2'));
  452. self::$driver->setQuery($query);
  453. $result = self::$driver->loadResult();
  454. $this->assertThat($result, $this->equalTo(2), __LINE__);
  455. }
  456. /**
  457. * Test loadRow method
  458. *
  459. * @return void
  460. *
  461. * @since 1.0
  462. */
  463. public function testLoadRow()
  464. {
  465. $query = self::$driver->getQuery(true);
  466. $query->select('*');
  467. $query->from('jos_dbtest');
  468. $query->where('description=' . self::$driver->quote('three'));
  469. self::$driver->setQuery($query);
  470. $result = self::$driver->loadRow();
  471. $expected = array(3, 'Testing3', '1980-04-18 00:00:00', 'three');
  472. $this->assertThat($result, $this->equalTo($expected), __LINE__);
  473. }
  474. /**
  475. * Test loadRowList method
  476. *
  477. * @return void
  478. *
  479. * @since 1.0
  480. */
  481. public function testLoadRowList()
  482. {
  483. $query = self::$driver->getQuery(true);
  484. $query->select('*');
  485. $query->from('jos_dbtest');
  486. $query->where('description=' . self::$driver->quote('one'));
  487. self::$driver->setQuery($query);
  488. $result = self::$driver->loadRowList();
  489. $expected = array(array(1, 'Testing', '1980-04-18 00:00:00', 'one'), array(2, 'Testing2', '1980-04-18 00:00:00', 'one'));
  490. $this->assertThat($result, $this->equalTo($expected), __LINE__);
  491. }
  492. /**
  493. * Tests the lockTable method.
  494. *
  495. * @return void
  496. *
  497. * @since 1.0
  498. */
  499. public function testLockTable()
  500. {
  501. $this->assertThat(
  502. self::$driver->lockTable('#__dbtest'),
  503. $this->isInstanceOf('\\Joomla\\Database\\Sqlite\\SqliteDriver'),
  504. 'Method returns the current instance of the driver object.'
  505. );
  506. }
  507. /**
  508. * Tests the renameTable method.
  509. *
  510. * @return void
  511. *
  512. * @since 1.0
  513. */
  514. public function testRenameTable()
  515. {
  516. $newTableName = 'bak_jos_dbtest';
  517. self::$driver->renameTable('jos_dbtest', $newTableName);
  518. // Check name change
  519. $tableList = self::$driver->getTableList();
  520. $this->assertThat(in_array($newTableName, $tableList), $this->isTrue(), __LINE__);
  521. // Restore initial state
  522. self::$driver->renameTable($newTableName, 'jos_dbtest');
  523. }
  524. /**
  525. * Test select method.
  526. *
  527. * @return void
  528. *
  529. * @since 1.0
  530. */
  531. public function testSelect()
  532. {
  533. $this->markTestIncomplete('This test has not been implemented yet.');
  534. }
  535. /**
  536. * Test setUTF method.
  537. *
  538. * @return void
  539. *
  540. * @since 1.0
  541. */
  542. public function testSetUTF()
  543. {
  544. $this->markTestIncomplete('This test has not been implemented yet.');
  545. }
  546. /**
  547. * Tests the transactionCommit method.
  548. *
  549. * @return void
  550. *
  551. * @since 1.0
  552. */
  553. public function testTransactionCommit()
  554. {
  555. self::$driver->transactionStart();
  556. $queryIns = self::$driver->getQuery(true);
  557. $queryIns->insert('#__dbtest')
  558. ->columns('id, title, start_date, description')
  559. ->values("6, 'testTitle', '1970-01-01', 'testDescription'");
  560. self::$driver->setQuery($queryIns)->execute();
  561. self::$driver->transactionCommit();
  562. /* check if value is present */
  563. $queryCheck = self::$driver->getQuery(true);
  564. $queryCheck->select('*')
  565. ->from('#__dbtest')
  566. ->where('id = 6');
  567. self::$driver->setQuery($queryCheck);
  568. $result = self::$driver->loadRow();
  569. $expected = array('6', 'testTitle', '1970-01-01', 'testDescription');
  570. $this->assertThat($result, $this->equalTo($expected), __LINE__);
  571. }
  572. /**
  573. * Tests the transactionRollback method, with and without savepoint.
  574. *
  575. * @param string $toSavepoint Savepoint name to rollback transaction to
  576. * @param int $tupleCount Number of tuple found after insertion and rollback
  577. *
  578. * @return void
  579. *
  580. * @since 1.0
  581. * @dataProvider dataTestTransactionRollback
  582. */
  583. public function testTransactionRollback($toSavepoint, $tupleCount)
  584. {
  585. self::$driver->transactionStart();
  586. /* try to insert this tuple, inserted only when savepoint != null */
  587. $queryIns = self::$driver->getQuery(true);
  588. $queryIns->insert('#__dbtest')
  589. ->columns('id, title, start_date, description')
  590. ->values("7, 'testRollback', '1970-01-01', 'testRollbackSp'");
  591. self::$driver->setQuery($queryIns)->execute();
  592. /* create savepoint only if is passed by data provider */
  593. if (!is_null($toSavepoint))
  594. {
  595. self::$driver->transactionStart((boolean) $toSavepoint);
  596. }
  597. /* try to insert this tuple, always rolled back */
  598. $queryIns = self::$driver->getQuery(true);
  599. $queryIns->insert('#__dbtest')
  600. ->columns('id, title, start_date, description')
  601. ->values("8, 'testRollback', '1972-01-01', 'testRollbackSp'");
  602. self::$driver->setQuery($queryIns)->execute();
  603. self::$driver->transactionRollback((boolean) $toSavepoint);
  604. /* release savepoint and commit only if a savepoint exists */
  605. if (!is_null($toSavepoint))
  606. {
  607. self::$driver->transactionCommit();
  608. }
  609. /* find how many rows have description='testRollbackSp' :
  610. * - 0 if a savepoint doesn't exist
  611. * - 1 if a savepoint exists
  612. */
  613. $queryCheck = self::$driver->getQuery(true);
  614. $queryCheck->select('*')
  615. ->from('#__dbtest')
  616. ->where("description = 'testRollbackSp'");
  617. self::$driver->setQuery($queryCheck);
  618. $result = self::$driver->loadRowList();
  619. $this->assertThat(count($result), $this->equalTo($tupleCount), __LINE__);
  620. }
  621. /**
  622. * Tests the unlockTables method.
  623. *
  624. * @return void
  625. *
  626. * @since 1.0
  627. */
  628. public function testUnlockTables()
  629. {
  630. $this->assertThat(
  631. self::$driver->unlockTables(),
  632. $this->isInstanceOf('\\Joomla\\Database\\Sqlite\\SqliteDriver'),
  633. 'Method returns the current instance of the driver object.'
  634. );
  635. }
  636. /**
  637. * Test updateObject method.
  638. *
  639. * @return void
  640. *
  641. * @since 1.0
  642. */
  643. public function testUpdateObject()
  644. {
  645. $this->markTestIncomplete('This test has not been implemented yet.');
  646. }
  647. }