/tests/vendors/core/ActiveRecord/QueryBuilder/MySQLQueryBuilderTest.php

https://github.com/gwutama/Core · PHP · 325 lines · 273 code · 52 blank · 0 comment · 0 complexity · aea20e23715322b1f905865baaf83593 MD5 · raw file

  1. <?php
  2. require_once 'vendors/Core/ActiveRecord/Adapter.php';
  3. require_once 'vendors/Core/ActiveRecord/Adapter/MySQL.php';
  4. require_once 'vendors/Core/ActiveRecord/Operatorable.php';
  5. require_once 'vendors/Core/ActiveRecord/Operator/MySQL.php';
  6. require_once 'vendors/Core/ActiveRecord/QueryBuilder.php';
  7. require_once 'vendors/Core/ActiveRecord/QueryBuilder/MySQL.php';
  8. require_once 'vendors/Core/exceptions.php';
  9. require_once 'vendors/Core/Utility/Inflector.php';
  10. use Core\ActiveRecord\QueryBuilder\MySQL as Builder;
  11. use Core\ActiveRecord\Operator\MySQL as Op;
  12. class MySQLQueryBuilderTest extends PHPUnit_Framework_TestCase
  13. {
  14. public function testInsert()
  15. {
  16. $q = Builder::insert("models", array("foo" => "bar"));
  17. $this->assertEquals("INSERT INTO `models`(`foo`) VALUES(:foo)", $q);
  18. $q = Builder::insert("people", array("foo" => "bar", "baz" => "blah"));
  19. $this->assertEquals("INSERT INTO `people`(`foo`, `baz`) VALUES(:foo, :baz)", $q);
  20. $q = Builder::insert("students", array("foo" => "bar", "baz" => "blah"), array(
  21. "on duplicate key update" => "`hello` = 'world'"
  22. ));
  23. $this->assertEquals("INSERT INTO `students`(`foo`, `baz`) VALUES(:foo, :baz) ON DUPLICATE KEY UPDATE `hello` = 'world'", $q);
  24. }
  25. public function testDelete()
  26. {
  27. $q = Builder::delete("models");
  28. $this->assertEquals("DELETE FROM `models`", $q);
  29. $q = Builder::delete("models", array(
  30. "conditions" => Op::eq("foo", "bar")
  31. ));
  32. $this->assertEquals("DELETE FROM `models` WHERE `foo` = :foo", $q);
  33. $q = Builder::delete("models", array(
  34. "conditions" => Op::eq("id", 42)
  35. ));
  36. $this->assertEquals("DELETE FROM `models` WHERE `id` = :id", $q);
  37. $q = Builder::delete("stuffs", array(
  38. "conditions" => Op::bAnd(
  39. Op::eq("foo", "bar"),
  40. Op::eq("baz", "blah")
  41. )
  42. ));
  43. $this->assertEquals("DELETE FROM `stuffs` WHERE (`foo` = :foo AND `baz` = :baz)", $q);
  44. $q = Builder::delete("models", array(
  45. "conditions" => Op::bOr(
  46. Op::eq("foo", "bar"),
  47. Op::eq("baz", "blah")
  48. )
  49. ));
  50. $this->assertEquals("DELETE FROM `models` WHERE (`foo` = :foo OR `baz` = :baz)", $q);
  51. $q = Builder::delete("objects",
  52. array("conditions" =>
  53. Op::bOr(
  54. Op::bAnd(
  55. Op::eq("foo", "bar"),
  56. Op::eq("baz", "blah")
  57. ),
  58. Op::eq("id", 42)
  59. )
  60. ));
  61. $this->assertEquals("DELETE FROM `objects` WHERE ((`foo` = :foo AND `baz` = :baz) OR `id` = :id)", $q);
  62. $q = Builder::delete("dates", array("limit" => 1));
  63. $this->assertEquals("DELETE FROM `dates` LIMIT :core_query_limit", $q);
  64. $q = Builder::delete("tests", array("order" => "`id` DESC"));
  65. $this->assertEquals("DELETE FROM `tests` ORDER BY `id` DESC", $q);
  66. $q = Builder::delete("people", array("order" => "`id` DESC", "limit" => 42));
  67. $this->assertEquals("DELETE FROM `people` ORDER BY `id` DESC LIMIT :core_query_limit", $q);
  68. $q = Builder::delete("people", array("order" => "`id` DESC, `name` ASC", "limit" => 42));
  69. $this->assertEquals("DELETE FROM `people` ORDER BY `id` DESC, `name` ASC LIMIT :core_query_limit", $q);
  70. }
  71. public function testUpdate()
  72. {
  73. $q = Builder::update("tables", array(
  74. "foo" => "bar",
  75. "baz" => "blah"
  76. ));
  77. $this->assertEquals("UPDATE `tables` SET `foo` = :foo, `baz` = :baz", $q);
  78. $q = Builder::update("models", array(
  79. "foo" => "bar",
  80. "baz" => "blah"
  81. ), array(
  82. "conditions" => Op::eq("id", 42)
  83. ));
  84. $this->assertEquals("UPDATE `models` SET `foo` = :foo, `baz` = :baz WHERE `id` = :id", $q);
  85. $q = Builder::update("things", array(
  86. "foo" => "bar",
  87. "baz" => "blah"
  88. ), array(
  89. "conditions" => Op::bOr(
  90. Op::eq("id", 42),
  91. Op::eq("hello", "world")
  92. )
  93. ));
  94. $this->assertEquals("UPDATE `things` SET `foo` = :foo, `baz` = :baz WHERE (`id` = :id OR `hello` = :hello)", $q);
  95. $q = Builder::update("tables", array(
  96. "foo" => "bar",
  97. "baz" => "blah"
  98. ), array(
  99. "limit" => 3
  100. ));
  101. $this->assertEquals("UPDATE `tables` SET `foo` = :foo, `baz` = :baz LIMIT :core_query_limit", $q);
  102. $q = Builder::update("dates", array("foo" => "bar"), array("limit" => 1));
  103. $this->assertEquals("UPDATE `dates` SET `foo` = :foo LIMIT :core_query_limit", $q);
  104. $q = Builder::update("tests", array("foo" => "bar"), array("order" => "`id` DESC"));
  105. $this->assertEquals("UPDATE `tests` SET `foo` = :foo ORDER BY `id` DESC", $q);
  106. $q = Builder::update("people", array("foo" => "bar"), array("order" => "`id` DESC", "limit" => 42));
  107. $this->assertEquals("UPDATE `people` SET `foo` = :foo ORDER BY `id` DESC LIMIT :core_query_limit", $q);
  108. $q = Builder::update("people", array("foo" => "bar"), array("order" => "`id` DESC, `name` ASC", "limit" => 42));
  109. $this->assertEquals("UPDATE `people` SET `foo` = :foo ORDER BY `id` DESC, `name` ASC LIMIT :core_query_limit", $q);
  110. }
  111. public function testSelect()
  112. {
  113. $q = Builder::select("cakes");
  114. $this->assertEquals("SELECT * FROM `cakes`", $q);
  115. $q = Builder::select("houses", array("conditions" => Op::eq("id", 42)));
  116. $this->assertEquals("SELECT * FROM `houses` WHERE `id` = :id", $q);
  117. $q = Builder::select("houses", array(
  118. "conditions" => Op::bOr(
  119. Op::eq("id", 42),
  120. Op::eq("foo", "bar")
  121. )
  122. ));
  123. $this->assertEquals("SELECT * FROM `houses` WHERE (`id` = :id OR `foo` = :foo)", $q);
  124. $q = Builder::select("movies", array(
  125. "fields" => array("id", "name", "lorem", "ipsum"),
  126. "conditions" => Op::bAnd(
  127. Op::eq("hello", "world"),
  128. Op::eq("foo", "bar")
  129. )
  130. ));
  131. $this->assertEquals("SELECT `id`, `name`, `lorem`, `ipsum` FROM `movies` WHERE (`hello` = :hello AND `foo` = :foo)", $q);
  132. $q = Builder::select("people", array(
  133. "fields" => array("id", "name", "lorem", "ipsum"),
  134. "conditions" => Op::bAnd(
  135. Op::eq("hello", "world"),
  136. Op::eq("foo", "bar")
  137. ),
  138. "limit" => 3
  139. ));
  140. $this->assertEquals("SELECT `id`, `name`, `lorem`, `ipsum` FROM `people` WHERE (`hello` = :hello AND `foo` = :foo) LIMIT :core_query_limit", $q);
  141. $q = Builder::select("people", array(
  142. "fields" => array("id", "name", "lorem", "ipsum"),
  143. "limit" => 3
  144. ));
  145. $this->assertEquals("SELECT `id`, `name`, `lorem`, `ipsum` FROM `people` LIMIT :core_query_limit", $q);
  146. $q = Builder::select("addresses", array(
  147. "fields" => array("id", "name", "lorem"),
  148. "limit" => 3,
  149. "offset" => 5
  150. ));
  151. $this->assertEquals("SELECT `id`, `name`, `lorem` FROM `addresses` LIMIT :core_query_limit OFFSET :core_query_offset", $q);
  152. $q = Builder::select("tables", array(
  153. "fields" => array("id", "name", "lorem"),
  154. "offset" => 5 // offset won't work without limit.
  155. ));
  156. $this->assertEquals("SELECT `id`, `name`, `lorem` FROM `tables`", $q);
  157. $q = Builder::select("names", array(
  158. "order" => "`id` DESC"
  159. ));
  160. $this->assertEquals("SELECT * FROM `names` ORDER BY `id` DESC", $q);
  161. $q = Builder::select("men", array(
  162. "order" => "`id` DESC, `name` ASC",
  163. "limit" => 42
  164. ));
  165. $this->assertEquals("SELECT * FROM `men` ORDER BY `id` DESC, `name` ASC LIMIT :core_query_limit", $q);
  166. $q = Builder::select("sites", array(
  167. "order" => "`id` DESC, `name` ASC",
  168. "limit" => 42
  169. ));
  170. $this->assertEquals("SELECT * FROM `sites` ORDER BY `id` DESC, `name` ASC LIMIT :core_query_limit", $q);
  171. $q = Builder::select("sites", array(
  172. "order" => "`id` DESC, `name` ASC",
  173. "limit" => 42,
  174. "offset" => 5
  175. ));
  176. $this->assertEquals("SELECT * FROM `sites` ORDER BY `id` DESC, `name` ASC LIMIT :core_query_limit OFFSET :core_query_offset", $q);
  177. $q = Builder::select("appointments", array(
  178. "group" => "year"
  179. ));
  180. $this->assertEquals("SELECT * FROM `appointments` GROUP BY `year`", $q);
  181. $q = Builder::select("appointments", array(
  182. "group" => "year",
  183. "order" => "`id` DESC"
  184. ));
  185. $this->assertEquals("SELECT * FROM `appointments` GROUP BY `year` ORDER BY `id` DESC", $q);
  186. $q = Builder::select("appointments", array(
  187. "group" => "year",
  188. "order" => "`id` DESC",
  189. "limit" => 10
  190. ));
  191. $this->assertEquals("SELECT * FROM `appointments` GROUP BY `year` ORDER BY `id` DESC LIMIT :core_query_limit", $q);
  192. $q = Builder::select("appointments", array(
  193. "having" => Op::eq("date", "NOW()")
  194. ));
  195. $this->assertEquals("SELECT * FROM `appointments` HAVING `date` = :date", $q);
  196. $q = Builder::select("appointments", array(
  197. "having" => Op::eq("date", "NOW()"),
  198. "order" => "`id` DESC"
  199. ));
  200. $this->assertEquals("SELECT * FROM `appointments` HAVING `date` = :date ORDER BY `id` DESC", $q);
  201. }
  202. public function testSimpleJoins() {
  203. $q = Builder::select("tests", array(
  204. "join" => array(
  205. "tables" => array("things"),
  206. "conditions" => "tests.id = things.tests_id"
  207. )
  208. ));
  209. $this->assertEquals("SELECT * FROM `tests` JOIN (`things`) ON (tests.id = things.tests_id)", $q);
  210. $q = Builder::select("families", array(
  211. "fields" => array("families.position", "families.meal"),
  212. "join" => array(
  213. "tables" => array("foods"),
  214. "conditions" => "families.position = foods.position",
  215. "type" => "LEFT JOIN"
  216. )
  217. ));
  218. $this->assertEquals("SELECT `families`.`position`, `families`.`meal` FROM `families` LEFT JOIN (`foods`)".
  219. " ON (families.position = foods.position)", $q);
  220. $q = Builder::select("families", array(
  221. "fields" => array("families.position", "families.meal"),
  222. "join" => array(
  223. "tables" => array("foods"),
  224. "conditions" => "families.position = foods.position",
  225. "type" => "LEFT JOIN"
  226. ),
  227. "conditions" => Op::eq("families.position", "father")
  228. ));
  229. $this->assertEquals("SELECT `families`.`position`, `families`.`meal` FROM `families` LEFT JOIN (`foods`)".
  230. " ON (families.position = foods.position) WHERE `families`.`position` = :families_position", $q);
  231. $q = Builder::select("families", array(
  232. "fields" => array("families.position", "families.meal"),
  233. "join" => array(
  234. "tables" => array("foods"),
  235. "conditions" => "families.position = foods.position",
  236. "type" => "LEFT JOIN"
  237. ),
  238. "conditions" => Op::eq("families.position", "father"),
  239. "limit" => 10,
  240. "offset" => 5
  241. ));
  242. $this->assertEquals("SELECT `families`.`position`, `families`.`meal` FROM `families` LEFT JOIN (`foods`)".
  243. " ON (families.position = foods.position) WHERE `families`.`position` = :families_position".
  244. " LIMIT :core_query_limit OFFSET :core_query_offset", $q);
  245. }
  246. public function testMultipleJoins() {
  247. $q = Builder::select("tests", array(
  248. "join" => array(
  249. "tables" => array("things", "foobars"),
  250. "conditions" => "tests.id = things.tests_id AND things.tests_id = foobars.things_id"
  251. )
  252. ));
  253. $this->assertEquals("SELECT * FROM `tests` JOIN (`things`, `foobars`) ON".
  254. " (tests.id = things.tests_id AND things.tests_id = foobars.things_id)", $q);
  255. $q = Builder::select("families", array(
  256. "fields" => array("families.position", "families.meal"),
  257. "join" => array(
  258. "tables" => array("foods", "restaurants"),
  259. "conditions" => "families.position = foods.position AND foods.restaurant = restaurants.id",
  260. "type" => "LEFT JOIN"
  261. ),
  262. "conditions" => Op::eq("families.position", "father"),
  263. "limit" => 10,
  264. "offset" => 5
  265. ));
  266. $this->assertEquals("SELECT `families`.`position`, `families`.`meal` FROM `families`".
  267. " LEFT JOIN (`foods`, `restaurants`)".
  268. " ON (families.position = foods.position AND foods.restaurant = restaurants.id)".
  269. " WHERE `families`.`position` = :families_position".
  270. " LIMIT :core_query_limit OFFSET :core_query_offset", $q);
  271. }
  272. }
  273. ?>