/tests/classes/fStatement/fStatementTest.php

https://github.com/netcarver/flourish · PHP · 681 lines · 603 code · 78 blank · 0 comment · 6 complexity · 2d75289c85e034aa910abe02bfa43243 MD5 · raw file

  1. <?php
  2. require_once('./support/init.php');
  3. class fStatementTest extends PHPUnit_Framework_TestSuite
  4. {
  5. public static function suite()
  6. {
  7. $suite = new fStatementTest();
  8. $suite->addTestSuite('fStatementTestNoModifications');
  9. $suite->addTestSuite('fStatementTestModifications');
  10. return $suite;
  11. }
  12. }
  13. class fStatementTestModifications extends PHPUnit_Framework_TestCase
  14. {
  15. public $db;
  16. public function setUp()
  17. {
  18. if (defined('SKIPPING')) {
  19. $this->markTestSkipped();
  20. }
  21. $this->db = new fDatabase(DB_TYPE, DB, DB_USERNAME, DB_PASSWORD, DB_HOST, DB_PORT);
  22. $this->db->execute(file_get_contents(DB_SETUP_FILE));
  23. }
  24. public function tearDown()
  25. {
  26. if (defined('SKIPPING')) {
  27. return;
  28. }
  29. teardown($this->db, DB_TEARDOWN_FILE);
  30. }
  31. public function testTransactionRollback()
  32. {
  33. $statement = $this->db->prepare("SELECT user_id FROM users");
  34. $this->db->query("BEGIN");
  35. $this->db->query("DELETE FROM users WHERE user_id = %i", 4);
  36. $res = $this->db->query($statement);
  37. $this->assertEquals(3, $res->countReturnedRows());
  38. $this->db->query("ROLLBACK");
  39. $res = $this->db->query($statement);
  40. $this->assertEquals(4, $res->countReturnedRows());
  41. }
  42. public function testTransactionCommit()
  43. {
  44. $statement = $this->db->prepare("SELECT user_id FROM users");
  45. $this->db->query("BEGIN");
  46. $this->db->query("DELETE FROM users WHERE user_id = %i", 4);
  47. $res = $this->db->query($statement);
  48. $this->assertEquals(3, $res->countReturnedRows());
  49. $this->db->query("COMMIT");
  50. $res = $this->db->query($statement);
  51. $this->assertEquals(3, $res->countReturnedRows());
  52. }
  53. }
  54. class fStatementTestNoModifications extends PHPUnit_Framework_TestCase
  55. {
  56. protected static $db;
  57. public static function setUpBeforeClass()
  58. {
  59. if (defined('SKIPPING')) {
  60. return;
  61. }
  62. $db = new fDatabase(DB_TYPE, DB, DB_USERNAME, DB_PASSWORD, DB_HOST, DB_PORT);
  63. $db->execute(file_get_contents(DB_SETUP_FILE));
  64. self::$db = $db;
  65. }
  66. public static function tearDownAfterClass()
  67. {
  68. if (defined('SKIPPING')) {
  69. return;
  70. }
  71. teardown(self::$db, DB_TEARDOWN_FILE);
  72. }
  73. public function setUp()
  74. {
  75. if (defined('SKIPPING')) {
  76. $this->markTestSkipped();
  77. }
  78. self::$db->execute('BEGIN');
  79. }
  80. public function tearDown()
  81. {
  82. if (defined('SKIPPING')) {
  83. return;
  84. }
  85. self::$db->execute('ROLLBACK');
  86. }
  87. public function testInsertAutoIncrementedValue()
  88. {
  89. $statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  90. $res = self::$db->query(
  91. $statement,
  92. 'John',
  93. '',
  94. 'Doe',
  95. 'john@doe.com',
  96. 'Active',
  97. 5,
  98. new fTimestamp(),
  99. new fDate(),
  100. new fTime(),
  101. TRUE,
  102. 'password'
  103. );
  104. $this->assertEquals(5, $res->getAutoIncrementedValue());
  105. }
  106. public function testInsertAffectedRows()
  107. {
  108. $statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  109. $res = self::$db->query(
  110. $statement,
  111. 'John',
  112. '',
  113. 'Doe',
  114. 'john@doe.com',
  115. 'Active',
  116. 5,
  117. new fTimestamp(),
  118. new fDate(),
  119. new fTime(),
  120. TRUE,
  121. 'password'
  122. );
  123. $this->assertEquals(1, $res->countAffectedRows());
  124. }
  125. public function testInsertReturnedRows()
  126. {
  127. $statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  128. $res = self::$db->query(
  129. $statement,
  130. 'John',
  131. '',
  132. 'Doe',
  133. 'john@doe.com',
  134. 'Active',
  135. 5,
  136. new fTimestamp(),
  137. new fDate(),
  138. new fTime(),
  139. TRUE,
  140. 'password'
  141. );
  142. $this->assertEquals(0, $res->countReturnedRows());
  143. }
  144. public function testInsertFetchRow()
  145. {
  146. $this->setExpectedException('fNoRowsException');
  147. $statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  148. $res = self::$db->query(
  149. $statement,
  150. 'John',
  151. '',
  152. 'Doe',
  153. 'john@doe.com',
  154. 'Active',
  155. 5,
  156. new fTimestamp(),
  157. new fDate(),
  158. new fTime(),
  159. TRUE,
  160. 'password'
  161. );
  162. $res->fetchRow();
  163. }
  164. public function testInsertFetchScalar()
  165. {
  166. $this->setExpectedException('fNoRowsException');
  167. $statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  168. $res = self::$db->query(
  169. $statement,
  170. 'John',
  171. '',
  172. 'Doe',
  173. 'john@doe.com',
  174. 'Active',
  175. 5,
  176. new fTimestamp(),
  177. new fDate(),
  178. new fTime(),
  179. TRUE,
  180. 'password'
  181. );
  182. $res->fetchScalar();
  183. }
  184. public function testInsertFetchAllRows()
  185. {
  186. $statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  187. $res = self::$db->query(
  188. $statement,
  189. 'John',
  190. '',
  191. 'Doe',
  192. 'john@doe.com',
  193. 'Active',
  194. 5,
  195. new fTimestamp(),
  196. new fDate(),
  197. new fTime(),
  198. TRUE,
  199. 'password'
  200. );
  201. $this->assertEquals(array(), $res->fetchAllRows());
  202. }
  203. public function testDeleteAffectedRows()
  204. {
  205. $res = self::$db->query(self::$db->prepare("DELETE FROM users WHERE user_id > %i AND user_id < %i"), 2, 5);
  206. $this->assertEquals(2, $res->countAffectedRows());
  207. }
  208. public function testDeleteReturnedRows()
  209. {
  210. $res = self::$db->query(self::$db->prepare("DELETE FROM users WHERE user_id > %i AND user_id < %i"), 2, 5);
  211. $this->assertEquals(0, $res->countReturnedRows());
  212. }
  213. public function testUpdateAffectedRows()
  214. {
  215. $res = self::$db->query(self::$db->prepare("UPDATE users SET first_name = %s"), 'First');
  216. $this->assertEquals(4, $res->countAffectedRows());
  217. }
  218. public function testUpdateReturnedRows()
  219. {
  220. $res = self::$db->query(self::$db->prepare("UPDATE users SET first_name = %s"), 'First');
  221. $this->assertEquals(0, $res->countReturnedRows());
  222. }
  223. public function testMultipleExecuteInsert()
  224. {
  225. $insert_statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  226. $select_statement = self::$db->prepare("SELECT email_address FROM users ORDER BY user_id");
  227. self::$db->execute(
  228. $insert_statement,
  229. 'John',
  230. '',
  231. 'Doe',
  232. 'john@doe.com',
  233. 'Active',
  234. 5,
  235. new fTimestamp(),
  236. new fDate(),
  237. new fTime(),
  238. TRUE,
  239. 'password'
  240. );
  241. $this->assertEquals(
  242. array(
  243. array('email_address' => 'will@flourishlib.com'),
  244. array('email_address' => 'john@smith.com'),
  245. array('email_address' => 'bar@example.com'),
  246. array('email_address' => 'foo@example.com'),
  247. array('email_address' => 'john@doe.com')
  248. ),
  249. self::$db->query($select_statement)->fetchAllRows()
  250. );
  251. self::$db->execute(
  252. $insert_statement,
  253. 'John',
  254. '',
  255. 'Doe',
  256. 'john2@doe.com',
  257. 'Active',
  258. 5,
  259. new fTimestamp(),
  260. new fDate(),
  261. new fTime(),
  262. TRUE,
  263. 'password'
  264. );
  265. $this->assertEquals(
  266. array(
  267. array('email_address' => 'will@flourishlib.com'),
  268. array('email_address' => 'john@smith.com'),
  269. array('email_address' => 'bar@example.com'),
  270. array('email_address' => 'foo@example.com'),
  271. array('email_address' => 'john@doe.com'),
  272. array('email_address' => 'john2@doe.com')
  273. ),
  274. self::$db->query($select_statement)->fetchAllRows()
  275. );
  276. }
  277. public function testMultipleExecuteDelete()
  278. {
  279. $delete_statement = self::$db->prepare("DELETE FROM users WHERE user_id = %i");
  280. $select_statement = self::$db->prepare("SELECT email_address FROM users ORDER BY user_id");
  281. $insert_statement = self::$db->prepare("INSERT INTO users (first_name, middle_initial, last_name, email_address, status, times_logged_in, date_created, birthday, time_of_last_login, is_validated, hashed_password) VALUES (%s, %s, %s, %s, %s, %i, %p, %d, %t, %b, %s)");
  282. $res = self::$db->query(
  283. $insert_statement,
  284. 'John',
  285. '',
  286. 'Doe',
  287. 'john@doe.com',
  288. 'Active',
  289. 5,
  290. new fTimestamp(),
  291. new fDate(),
  292. new fTime(),
  293. TRUE,
  294. 'password'
  295. );
  296. $res2 = self::$db->query(
  297. $insert_statement,
  298. 'John',
  299. '',
  300. 'Doe',
  301. 'john2@doe.com',
  302. 'Active',
  303. 5,
  304. new fTimestamp(),
  305. new fDate(),
  306. new fTime(),
  307. TRUE,
  308. 'password'
  309. );
  310. self::$db->execute($delete_statement, $res2->getAutoIncrementedValue());
  311. $this->assertEquals(
  312. array(
  313. array('email_address' => 'will@flourishlib.com'),
  314. array('email_address' => 'john@smith.com'),
  315. array('email_address' => 'bar@example.com'),
  316. array('email_address' => 'foo@example.com'),
  317. array('email_address' => 'john@doe.com')
  318. ),
  319. self::$db->query($select_statement)->fetchAllRows()
  320. );
  321. self::$db->execute($delete_statement, $res->getAutoIncrementedValue());
  322. $this->assertEquals(
  323. array(
  324. array('email_address' => 'will@flourishlib.com'),
  325. array('email_address' => 'john@smith.com'),
  326. array('email_address' => 'bar@example.com'),
  327. array('email_address' => 'foo@example.com')
  328. ),
  329. self::$db->query($select_statement)->fetchAllRows()
  330. );
  331. self::$db->execute($delete_statement, 4);
  332. $this->assertEquals(
  333. array(
  334. array('email_address' => 'will@flourishlib.com'),
  335. array('email_address' => 'john@smith.com'),
  336. array('email_address' => 'bar@example.com')
  337. ),
  338. self::$db->query($select_statement)->fetchAllRows()
  339. );
  340. self::$db->execute($delete_statement, 3);
  341. $this->assertEquals(
  342. array(
  343. array('email_address' => 'will@flourishlib.com'),
  344. array('email_address' => 'john@smith.com')
  345. ),
  346. self::$db->query($select_statement)->fetchAllRows()
  347. );
  348. }
  349. public function testSQLFail()
  350. {
  351. $this->setExpectedException('fSQLException');
  352. $statement = self::$db->prepare("DELETE FROM usrs");
  353. $res = self::$db->query($statement);
  354. }
  355. public function testGetSql()
  356. {
  357. $res = self::$db->query(self::$db->prepare("SELECT user_id FROM users"));
  358. $this->assertEquals('SELECT user_id FROM users', $res->getSQL());
  359. }
  360. public function testGetUntranslatedSql()
  361. {
  362. $res = self::$db->query(self::$db->prepare("SELECT user_id FROM users"));
  363. $this->assertEquals(NULL, $res->getUntranslatedSQL());
  364. }
  365. public function testCountAffectedRows()
  366. {
  367. $res = self::$db->query(self::$db->prepare("SELECT user_id FROM users"));
  368. $this->assertEquals(0, $res->countAffectedRows());
  369. }
  370. public function testCountReturnedRows()
  371. {
  372. $res = self::$db->query(self::$db->prepare("SELECT user_id FROM users"));
  373. $this->assertEquals(4, $res->countReturnedRows());
  374. }
  375. public function testNoAutoIncrementedValue()
  376. {
  377. $res = self::$db->query(self::$db->prepare("SELECT user_id FROM users"));
  378. $this->assertEquals(NULL, $res->getAutoIncrementedValue());
  379. }
  380. public function testCountReturnedRows2()
  381. {
  382. $res = self::$db->query(self::$db->prepare("SELECT user_id FROM users WHERE user_id = %i"), 99);
  383. $this->assertEquals(0, $res->countReturnedRows());
  384. }
  385. public function testFetchScalar()
  386. {
  387. $res = self::$db->query(self::$db->prepare("SELECT first_name FROM users WHERE user_id = %i"), 1);
  388. $this->assertEquals('Will', $res->fetchScalar());
  389. }
  390. public function testProvideArray()
  391. {
  392. $res = self::$db->query(self::$db->prepare("SELECT first_name FROM users WHERE user_id = %i OR user_id = %i"), array(1, 2));
  393. $this->assertEquals('Will', $res->fetchScalar());
  394. }
  395. public function testInvalidValueToNull()
  396. {
  397. $res = self::$db->query(self::$db->prepare("SELECT first_name FROM users WHERE user_id = %i"), 'test');
  398. $this->assertEquals(0, $res->countReturnedRows());
  399. }
  400. public function testFetchRow()
  401. {
  402. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id = %i"), 1);
  403. $this->assertEquals(
  404. array(
  405. 'first_name' => 'Will',
  406. 'last_name' => 'Bond',
  407. 'email_address' => 'will@flourishlib.com'
  408. ),
  409. $res->fetchRow()
  410. );
  411. }
  412. public function testFetchRowException()
  413. {
  414. $this->setExpectedException('fNoRowsException');
  415. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id = %i"), 25);
  416. $res->fetchRow();
  417. }
  418. public function testFetchAllRows()
  419. {
  420. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id < %i ORDER BY user_id"), 3);
  421. $this->assertEquals(
  422. array(
  423. array(
  424. 'first_name' => 'Will',
  425. 'last_name' => 'Bond',
  426. 'email_address' => 'will@flourishlib.com'
  427. ),
  428. array(
  429. 'first_name' => 'John',
  430. 'last_name' => 'Smith',
  431. 'email_address' => 'john@smith.com'
  432. )
  433. ),
  434. $res->fetchAllRows()
  435. );
  436. }
  437. public function testFetchAllRows2()
  438. {
  439. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id IN (%i) ORDER BY user_id"), 25);
  440. $this->assertEquals(array(), $res->fetchAllRows());
  441. }
  442. public function testIteration()
  443. {
  444. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id < %i ORDER BY user_id"), 3);
  445. $i = 0;
  446. foreach ($res as $row) {
  447. $this->assertEquals(
  448. array(
  449. 'first_name',
  450. 'last_name',
  451. 'email_address'
  452. ),
  453. array_keys($row)
  454. );
  455. $i++;
  456. }
  457. $this->assertEquals(2, $i);
  458. }
  459. public function testRepeatIteration()
  460. {
  461. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id < %i ORDER BY user_id"), 3);
  462. $i = 0;
  463. foreach ($res as $row) {
  464. $this->assertEquals(
  465. array(
  466. 'first_name',
  467. 'last_name',
  468. 'email_address'
  469. ),
  470. array_keys($row)
  471. );
  472. $i++;
  473. }
  474. $this->assertEquals(2, $i);
  475. $i = 0;
  476. foreach ($res as $row) {
  477. $this->assertEquals(
  478. array(
  479. 'first_name',
  480. 'last_name',
  481. 'email_address'
  482. ),
  483. array_keys($row)
  484. );
  485. $i++;
  486. }
  487. $this->assertEquals(2, $i);
  488. }
  489. public function testEmptyIteration()
  490. {
  491. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id IN (%i) ORDER BY user_id"), 25);
  492. $i = 0;
  493. foreach ($res as $row) {
  494. $i++;
  495. }
  496. $this->assertEquals(0, $i);
  497. }
  498. public function testTossIfEmpty()
  499. {
  500. $this->setExpectedException('fNoRowsException');
  501. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id IN (%i) ORDER BY user_id"), 25);
  502. $res->tossIfNoRows();
  503. }
  504. public function testTossIfEmpty2()
  505. {
  506. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id IN (%i) ORDER BY user_id"), 1);
  507. $res->tossIfNoRows();
  508. }
  509. public function testSeek()
  510. {
  511. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users ORDER BY user_id"));
  512. $res->seek(3);
  513. $this->assertEquals(
  514. array(
  515. 'first_name' => 'Foo',
  516. 'last_name' => 'Barish',
  517. 'email_address' => 'foo@example.com'
  518. ),
  519. $res->fetchRow()
  520. );
  521. $res->seek(0);
  522. $this->assertEquals(
  523. array(
  524. 'first_name' => 'Will',
  525. 'last_name' => 'Bond',
  526. 'email_address' => 'will@flourishlib.com'
  527. ),
  528. $res->fetchRow()
  529. );
  530. }
  531. public function testSeekFailure()
  532. {
  533. $this->setExpectedException('fProgrammerException');
  534. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users ORDER BY user_id"));
  535. $res->seek(4);
  536. }
  537. public function testConcurrentResults()
  538. {
  539. $res = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id < %i ORDER BY user_id"), 3);
  540. $res2 = self::$db->query(self::$db->prepare("SELECT first_name, last_name, email_address FROM users WHERE user_id > %i AND user_id < %i ORDER BY user_id"), 2, 5);
  541. $this->assertEquals(
  542. array(
  543. 'first_name' => 'Will',
  544. 'last_name' => 'Bond',
  545. 'email_address' => 'will@flourishlib.com'
  546. ),
  547. $res->fetchRow()
  548. );
  549. $this->assertEquals(
  550. array(
  551. 'first_name' => 'Bar',
  552. 'last_name' => 'Sheba',
  553. 'email_address' => 'bar@example.com'
  554. ),
  555. $res2->fetchRow()
  556. );
  557. $this->assertEquals(
  558. array(
  559. 'first_name' => 'John',
  560. 'last_name' => 'Smith',
  561. 'email_address' => 'john@smith.com'
  562. ),
  563. $res->fetchRow()
  564. );
  565. $this->assertEquals(
  566. array(
  567. 'first_name' => 'Foo',
  568. 'last_name' => 'Barish',
  569. 'email_address' => 'foo@example.com'
  570. ),
  571. $res2->fetchRow()
  572. );
  573. }
  574. public function testReuse()
  575. {
  576. $statement = self::$db->prepare("SELECT user_id, email_address FROM users WHERE user_id = %i");
  577. $this->assertEquals(
  578. array('user_id' => 1, 'email_address' => 'will@flourishlib.com'),
  579. self::$db->query($statement, 1)->fetchRow()
  580. );
  581. $this->assertEquals(
  582. array('user_id' => 2, 'email_address' => 'john@smith.com'),
  583. self::$db->query($statement, 2)->fetchRow()
  584. );
  585. $this->assertEquals(
  586. array('user_id' => 3, 'email_address' => 'bar@example.com'),
  587. self::$db->query($statement, 3)->fetchRow()
  588. );
  589. $this->assertEquals(
  590. array('user_id' => 4, 'email_address' => 'foo@example.com'),
  591. self::$db->query($statement, 4)->fetchRow()
  592. );
  593. $this->assertEquals(
  594. array(
  595. array('email_address' => 'will@flourishlib.com'),
  596. array('email_address' => 'john@smith.com'),
  597. array('email_address' => 'bar@example.com'),
  598. array('email_address' => 'foo@example.com')
  599. ),
  600. self::$db->query("SELECT email_address FROM users ORDER BY user_id ASC")->fetchAllRows()
  601. );
  602. }
  603. }