PageRenderTime 66ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/mysql/functional_tests.php

http://github.com/ihumanable/prosper-lib
PHP | 205 lines | 160 code | 43 blank | 2 comment | 12 complexity | 450c9b6065ec570080572c3b37c1efed MD5 | raw file
Possible License(s): Unlicense, LGPL-2.1
  1. <?php
  2. use Prosper\Query;
  3. class MySqlFunctionalTests extends UnitTestCase {
  4. private $record = array('bar' => 1, 'baz' => 2, 'zap' => 3);
  5. function MySqlFunctionalTests() {
  6. $this->UnitTestCase('MySQL - Functional Test');
  7. }
  8. function setUp() {
  9. Query::configure(Query::MYSQL_MODE, 'unittest', 'unittest', 'localhost', 'unittest');
  10. //Start with a clean slate
  11. Query::delete()->from('foo')->execute();
  12. }
  13. function tearDown() {
  14. //Clean out anything the test might have inserted
  15. Query::delete()->from('foo')->execute();
  16. }
  17. function populateRow($values) {
  18. Query::insert()->into('foo')->values( $values )->execute();
  19. }
  20. function test_insert_single() {
  21. $this->populateRow($this->record);
  22. $result = Query::select()->from('foo')->execute();
  23. $this->assertEqual($result[0], $this->record);
  24. }
  25. function test_insert_multiple() {
  26. for($i = 0; $i < 3; ++$i) {
  27. $this->populateRow($this->record);
  28. }
  29. $result = Query::select()->from('foo')->execute();
  30. $this->assertEqual(3, count($result));
  31. }
  32. function test_update_single() {
  33. $this->populateRow($this->record);
  34. Query::update('foo')->set( array('bar' => 5) )->execute();
  35. $result = Query::select()->from('foo')->execute();
  36. $expected = $this->record;
  37. $expected['bar'] = 5;
  38. $this->assertEqual($result[0], $expected);
  39. }
  40. function test_update_multiple() {
  41. for($i = 0; $i < 3; ++$i) {
  42. $this->populateRow($this->record);
  43. }
  44. Query::update('foo')->set( array('bar' => 5) )->execute();
  45. $result = Query::select()->from('foo')->execute();
  46. $expected = $this->record;
  47. $expected['bar'] = 5;
  48. $this->assertEqual($result, array($expected, $expected, $expected));
  49. }
  50. function test_update_targeted() {
  51. $this->populateRow($this->record);
  52. $different = $this->record;
  53. $different['bar'] = 7;
  54. $this->populateRow($different);
  55. Query::update('foo')->set( array('baz' => 5) )->where('bar = ?', 7)->execute();
  56. $result = Query::select()->from('foo')->order('bar')->execute();
  57. $expected = $different;
  58. $expected['baz'] = 5;
  59. $this->assertEqual($result, array($this->record, $expected));
  60. }
  61. function test_delete() {
  62. $this->populateRow($this->record);
  63. Query::delete()->from('foo')->execute();
  64. $result = Query::select()->from('foo')->execute();
  65. $this->assertEqual($result, array());
  66. }
  67. function test_delete_targeted() {
  68. $this->populateRow($this->record);
  69. $different = $this->record;
  70. $different['bar'] = 7;
  71. $this->populateRow($different);
  72. Query::delete()->from('foo')->where('bar = ?', 7)->execute();
  73. $result = Query::select()->from('foo')->execute();
  74. $this->assertEqual($result, array($this->record));
  75. }
  76. function test_limit() {
  77. for($i = 0; $i < 100; ++$i) {
  78. $this->populateRow($this->record);
  79. }
  80. $result = Query::select()->from('foo')->limit(10)->execute();
  81. $this->assertEqual(10, count($result));
  82. }
  83. function test_limit_offset() {
  84. for($i = 0; $i < 100; ++$i) {
  85. $record = $this->record;
  86. $record['bar'] = ($i < 10 ? "0$i" : $i);
  87. if($i == 20) {
  88. $first = $record;
  89. } else if($i == 21) {
  90. $second = $record;
  91. } else if($i == 22) {
  92. $third = $record;
  93. }
  94. $this->populateRow($record);
  95. }
  96. $result = Query::select()->from('foo')->order('bar')->limit(3, 20)->execute();
  97. $this->assertEqual($result, array($first, $second, $third));
  98. }
  99. function test_rollback() {
  100. Query::begin(); {
  101. $this->populateRow($this->record);
  102. } Query::rollback();
  103. $result = Query::select()->from('foo')->execute();
  104. $this->assertEqual($result, array());
  105. }
  106. function test_commit() {
  107. Query::begin(); {
  108. $this->populateRow($this->record);
  109. } Query::commit();
  110. $result = Query::select()->from('foo')->execute();
  111. $this->assertEqual($result, array($this->record));
  112. }
  113. function test_concurrent() {
  114. $different = $this->record;
  115. $different['bar'] = 5;
  116. $this->populateRow($different);
  117. $this->populateRow($this->record);
  118. $q1 = Query::update('foo')->set(array('baz' => 10));
  119. $q2 = Query::update('foo')->set(array('baz' => 20));
  120. $q1->where('bar = ?', 1)->execute();
  121. $q2->where('bar = ?', 5)->execute();
  122. $result = Query::select()->from('foo')->order('bar')->execute();
  123. $this->assertEqual($result, array( array( 'bar' => 1, 'baz' => 10, 'zap' => 3 ),
  124. array( 'bar' => 5, 'baz' => 20, 'zap' => 3 ) ));
  125. }
  126. function test_repeatable() {
  127. $this->populateRow($this->record);
  128. $query = Query::select()->from('foo');
  129. $result1 = $query->execute();
  130. $result2 = $query->execute();
  131. $this->assertEqual($result1, $result2);
  132. }
  133. function test_repeatable_prepared() {
  134. $this->populateRow($this->record);
  135. $different = $this->record;
  136. $different['bar'] = 5;
  137. $this->populateRow($different);
  138. $query = Query::select()->from('foo')->where('bar = ?', 5);
  139. $result1 = $query->execute();
  140. $result2 = $query->execute();
  141. $this->assertEqual($result1, $result2);
  142. }
  143. function test_rebinding() {
  144. $this->populateRow($this->record);
  145. $query = Query::select()->from('foo')->where('bar = ?', 5);
  146. $result1 = $query->execute();
  147. $query->rebind(1);
  148. $result2 = $query->execute();
  149. $this->assertEqual($result1, array());
  150. $this->assertEqual($result2, array($this->record));
  151. }
  152. function test_iterable() {
  153. $this->populateRow($this->record);
  154. $this->populateRow($this->record);
  155. $this->populateRow($this->record);
  156. $query = Query::select()->from('foo');
  157. foreach($query as $row) {
  158. $this->assertEqual($row, $this->record);
  159. }
  160. }
  161. }
  162. ?>