PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/SQL/Test/UpdateQueryBuilderTest.php

https://github.com/csanquer/QueryBuilder
PHP | 326 lines | 273 code | 34 blank | 19 comment | 8 complexity | 8b0b984ad9be6ba6c6982ebcd24f354b MD5 | raw file
  1. <?php
  2. namespace SQL\Test;
  3. use SQL\Test\Fixtures\PDOTestCase;
  4. use SQL\UpdateQueryBuilder;
  5. use SQL\SelectQueryBuilder;
  6. class UpdateQueryBuilderTest extends PDOTestCase
  7. {
  8. /**
  9. * @var \SQL\UpdateQueryBuilder
  10. */
  11. protected $queryBuilder;
  12. /**
  13. * Sets up the fixture, for example, opens a network connection.
  14. * This method is called before a test is executed.
  15. */
  16. protected function setUp()
  17. {
  18. $this->queryBuilder = new UpdateQueryBuilder(self::$pdo);
  19. }
  20. public function testUpdate()
  21. {
  22. $this->assertInstanceOf('SQL\UpdateQueryBuilder', $this->queryBuilder->update('book'));
  23. $this->assertEquals('book', $this->queryBuilder->getTablePart());
  24. $this->assertEquals('book', $this->queryBuilder->getTable());
  25. }
  26. /**
  27. * @dataProvider getTableStringProvider
  28. */
  29. public function testGetTableString($table, $options, $expected, $expectedFormatted)
  30. {
  31. $this->queryBuilder->update($table);
  32. foreach ($options as $option) {
  33. $this->queryBuilder->addOption($option);
  34. }
  35. $this->assertEquals($expected, $this->queryBuilder->getTableString());
  36. $this->assertEquals($expectedFormatted, $this->queryBuilder->getTableString(true));
  37. }
  38. public function getTableStringProvider()
  39. {
  40. return array(
  41. array(
  42. 'book',
  43. array(),
  44. 'UPDATE book ',
  45. 'UPDATE book '."\n"
  46. ),
  47. array(
  48. 'book',
  49. array('LOW PRIORITY'),
  50. 'UPDATE LOW PRIORITY book ',
  51. 'UPDATE LOW PRIORITY book '."\n"
  52. ),
  53. );
  54. }
  55. /**
  56. * @dataProvider setProvider
  57. */
  58. public function testSet($column, $expression, $value, $expected)
  59. {
  60. $this->assertInstanceOf('SQL\UpdateQueryBuilder', $this->queryBuilder->set($column, $expression, $value));
  61. $this->assertEquals($expected, $this->queryBuilder->getSetParts());
  62. $this->assertEquals($expected, $this->queryBuilder->getSet());
  63. }
  64. public function setProvider()
  65. {
  66. $select1 = new SelectQueryBuilder();
  67. $select1->select('AVG(price)');
  68. $select1->from('OldBook', 'o');
  69. return array(
  70. array(
  71. 'score',
  72. '',
  73. 5,
  74. array(
  75. array(
  76. 'column' => 'score',
  77. 'expression' => '',
  78. 'values' => 5,
  79. ),
  80. ),
  81. ),
  82. array(
  83. 'price',
  84. 'score*2',
  85. null,
  86. array(
  87. array(
  88. 'column' => 'price',
  89. 'expression' => 'score*2',
  90. 'values' => null,
  91. ),
  92. ),
  93. ),
  94. array(
  95. 'price',
  96. $select1,
  97. '3',
  98. array(
  99. array(
  100. 'column' => 'price',
  101. 'expression' => $select1,
  102. 'values' => null,
  103. ),
  104. ),
  105. ),
  106. );
  107. }
  108. /**
  109. * @dataProvider getSetStringProvider
  110. */
  111. public function testGetSetString($sets, $expectedBoundParameters, $expected, $expectedFormatted)
  112. {
  113. foreach ($sets as $set) {
  114. $this->queryBuilder->set($set[0], $set[1], $set[2]);
  115. }
  116. $this->assertEquals($expected, $this->queryBuilder->getSetString());
  117. $this->assertEquals($expectedFormatted, $this->queryBuilder->getSetString(true));
  118. $this->assertEquals($expectedBoundParameters, $this->queryBuilder->getBoundParameters());
  119. }
  120. public function getSetStringProvider()
  121. {
  122. $select1 = new SelectQueryBuilder();
  123. $select1->select('AVG(price)');
  124. $select1->from('OldBook', 'o');
  125. return array(
  126. array(
  127. array(
  128. array('score', null, 5)
  129. ),
  130. array(5),
  131. 'SET score = ? ',
  132. 'SET '."\n".'score = ? '."\n",
  133. ),
  134. array(
  135. array(
  136. array('score', null, 5),
  137. array('price', null, 8),
  138. ),
  139. array(5, 8),
  140. 'SET score = ?, price = ? ',
  141. 'SET '."\n".'score = ?, '."\n".'price = ? '."\n",
  142. ),
  143. array(
  144. array(
  145. array('price', 'score*2', null)
  146. ),
  147. array(),
  148. 'SET price = score*2 ',
  149. 'SET '."\n".'price = score*2 '."\n",
  150. ),
  151. array(
  152. array(
  153. array('price', 'score*?', 5)
  154. ),
  155. array(5),
  156. 'SET price = score*? ',
  157. 'SET '."\n".'price = score*? '."\n",
  158. ),
  159. array(
  160. array(
  161. array('price', $select1, 5)
  162. ),
  163. array(),
  164. 'SET price = (SELECT AVG(price) FROM OldBook AS o ) ',
  165. 'SET '."\n".'price = ('."\n".'SELECT AVG(price) '."\n".'FROM OldBook AS o '."\n".') '."\n",
  166. ),
  167. );
  168. }
  169. public function testWhere()
  170. {
  171. $this->assertInstanceOf('SQL\UpdateQueryBuilder', $this->queryBuilder->Where('id', 1, SelectQueryBuilder::EQUALS, SelectQueryBuilder::LOGICAL_AND));
  172. }
  173. public function testAndWhere()
  174. {
  175. $this->assertInstanceOf('SQL\UpdateQueryBuilder', $this->queryBuilder->andWhere('id', 1, SelectQueryBuilder::EQUALS));
  176. }
  177. public function testOrWhere()
  178. {
  179. $this->assertInstanceOf('SQL\UpdateQueryBuilder', $this->queryBuilder->orWhere('id', 1, SelectQueryBuilder::EQUALS));
  180. }
  181. public function testOr()
  182. {
  183. $expected = array(Array(
  184. 'bracket' => UpdateQueryBuilder::BRACKET_OPEN,
  185. 'connector' => UpdateQueryBuilder::LOGICAL_OR,
  186. ));
  187. $this->assertInstanceOf('SQL\UpdateQueryBuilder', $this->queryBuilder->_or());
  188. $this->assertEquals($expected, $this->queryBuilder->getWhereParts());
  189. }
  190. public function testAnd()
  191. {
  192. $expected = array(Array(
  193. 'bracket' => UpdateQueryBuilder::BRACKET_OPEN,
  194. 'connector' => UpdateQueryBuilder::LOGICAL_AND,
  195. ));
  196. $this->assertInstanceOf('SQL\UpdateQueryBuilder', $this->queryBuilder->_and());
  197. $this->assertEquals($expected, $this->queryBuilder->getWhereParts());
  198. }
  199. public function testMergeWhere()
  200. {
  201. $this->queryBuilder->where('id', 5 , UpdateQueryBuilder::LESS_THAN);
  202. $qb = new SelectQueryBuilder();
  203. $qb
  204. ->_open(SelectQueryBuilder::LOGICAL_OR)
  205. ->where('title', 'Dune' , SelectQueryBuilder::NOT_EQUALS, null)
  206. ->_close();
  207. $this->queryBuilder->mergeWhere($qb);
  208. $expected = array(
  209. array (
  210. 'column' => 'id',
  211. 'value' => 5,
  212. 'operator' => '<',
  213. 'connector' => 'AND',
  214. ),
  215. array (
  216. 'bracket' => '(',
  217. 'connector' => 'OR',
  218. ),
  219. array (
  220. 'column' => 'title',
  221. 'value' => 'Dune',
  222. 'operator' => '!=',
  223. 'connector' => 'AND',
  224. ),
  225. array (
  226. 'bracket' => ')',
  227. 'connector' => NULL,
  228. ),
  229. );
  230. $this->assertEquals($expected, $this->queryBuilder->getWhereParts());
  231. }
  232. /**
  233. * @dataProvider getQueryStringProvider
  234. */
  235. public function testGetQueryString($table, $sets, $wheres, $expectedBoundParameters, $expected, $expectedFormatted)
  236. {
  237. $this->queryBuilder->update($table);
  238. foreach ($sets as $set) {
  239. $this->queryBuilder->set($set[0], $set[1], $set[2]);
  240. }
  241. foreach ($wheres as $where) {
  242. $nbWhere = count($where);
  243. if ($nbWhere == 4) {
  244. $this->queryBuilder->where($where[0], $where[1], $where[2], $where[3]);
  245. } elseif ($nbWhere >= 1 && $nbWhere <= 2) {
  246. if ($where[0] == '(') {
  247. if (isset($where[1])) {
  248. $this->queryBuilder->_open($where[1]);
  249. } else {
  250. $this->queryBuilder->_open();
  251. }
  252. } elseif ($where[0] == ')') {
  253. $this->queryBuilder->_close();
  254. }
  255. }
  256. }
  257. $this->assertEquals($expected, $this->queryBuilder->getQueryString());
  258. $this->assertEquals($expectedFormatted, $this->queryBuilder->getQueryString(true));
  259. $this->assertEquals($expectedBoundParameters, $this->queryBuilder->getBoundParameters());
  260. }
  261. public function getQueryStringProvider()
  262. {
  263. $select1 = new SelectQueryBuilder();
  264. $select1->select('AVG(price)');
  265. $select1->from('OldBook', 'o');
  266. return array(
  267. array(
  268. '',
  269. array(
  270. ),
  271. array(
  272. ),
  273. array(),
  274. '',
  275. '',
  276. ),
  277. array(
  278. 'book',
  279. array(
  280. array('score', null, 5),
  281. array('price', null, 8),
  282. ),
  283. array(
  284. array('title', 'Dune', UpdateQueryBuilder::EQUALS, null),
  285. ),
  286. array(5, 8, 'Dune'),
  287. 'UPDATE book SET score = ?, price = ? WHERE title = ? ',
  288. 'UPDATE book '."\n".'SET '."\n".'score = ?, '."\n".'price = ? '."\n".'WHERE title = ? '."\n",
  289. ),
  290. );
  291. }
  292. }