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

/src/Joomla/Database/Tests/DriverMysqliTest.php

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