SQLBuilderTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. include 'helpers/config.php';
  3. use ActiveRecord\SQLBuilder;
  4. use ActiveRecord\Table;
  5. class SQLBuilderTest extends DatabaseTest
  6. {
  7. protected $table_name = 'authors';
  8. protected $class_name = 'Author';
  9. protected $table;
  10. public function set_up($connection_name=null)
  11. {
  12. parent::set_up($connection_name);
  13. $this->sql = new SQLBuilder($this->conn,$this->table_name);
  14. $this->table = Table::load($this->class_name);
  15. }
  16. protected function cond_from_s($name, $values=null, $map=null)
  17. {
  18. return SQLBuilder::create_conditions_from_underscored_string($this->table->conn, $name, $values, $map);
  19. }
  20. public function assert_conditions($expected_sql, $values, $underscored_string, $map=null)
  21. {
  22. $cond = SQLBuilder::create_conditions_from_underscored_string($this->table->conn,$underscored_string,$values,$map);
  23. $this->assert_sql_has($expected_sql,array_shift($cond));
  24. if ($values)
  25. $this->assert_equals(array_values(array_filter($values,function($s) { return $s !== null; })),array_values($cond));
  26. else
  27. $this->assert_equals(array(),$cond);
  28. }
  29. /**
  30. * @expectedException ActiveRecord\ActiveRecordException
  31. */
  32. public function test_no_connection()
  33. {
  34. new SQLBuilder(null,'authors');
  35. }
  36. public function test_nothing()
  37. {
  38. $this->assert_equals('SELECT * FROM authors',(string)$this->sql);
  39. }
  40. public function test_where_with_array()
  41. {
  42. $this->sql->where("id=? AND name IN(?)",1,array('Tito','Mexican'));
  43. $this->assert_sql_has("SELECT * FROM authors WHERE id=? AND name IN(?,?)",(string)$this->sql);
  44. $this->assert_equals(array(1,'Tito','Mexican'),$this->sql->get_where_values());
  45. }
  46. public function test_where_with_hash()
  47. {
  48. $this->sql->where(array('id' => 1, 'name' => 'Tito'));
  49. $this->assert_sql_has("SELECT * FROM authors WHERE id=? AND name=?",(string)$this->sql);
  50. $this->assert_equals(array(1,'Tito'),$this->sql->get_where_values());
  51. }
  52. public function test_where_with_hash_and_array()
  53. {
  54. $this->sql->where(array('id' => 1, 'name' => array('Tito','Mexican')));
  55. $this->assert_sql_has("SELECT * FROM authors WHERE id=? AND name IN(?,?)",(string)$this->sql);
  56. $this->assert_equals(array(1,'Tito','Mexican'),$this->sql->get_where_values());
  57. }
  58. public function test_gh134_where_with_hash_and_null()
  59. {
  60. $this->sql->where(array('id' => 1, 'name' => null));
  61. $this->assert_sql_has("SELECT * FROM authors WHERE id=? AND name IS ?",(string)$this->sql);
  62. $this->assert_equals(array(1, null),$this->sql->get_where_values());
  63. }
  64. public function test_where_with_null()
  65. {
  66. $this->sql->where(null);
  67. $this->assert_equals('SELECT * FROM authors',(string)$this->sql);
  68. }
  69. public function test_where_with_no_args()
  70. {
  71. $this->sql->where();
  72. $this->assert_equals('SELECT * FROM authors',(string)$this->sql);
  73. }
  74. public function test_order()
  75. {
  76. $this->sql->order('name');
  77. $this->assert_equals('SELECT * FROM authors ORDER BY name',(string)$this->sql);
  78. }
  79. public function test_limit()
  80. {
  81. $this->sql->limit(10)->offset(1);
  82. $this->assert_equals($this->conn->limit('SELECT * FROM authors',1,10),(string)$this->sql);
  83. }
  84. public function test_select()
  85. {
  86. $this->sql->select('id,name');
  87. $this->assert_equals('SELECT id,name FROM authors',(string)$this->sql);
  88. }
  89. public function test_joins()
  90. {
  91. $join = 'inner join books on(authors.id=books.author_id)';
  92. $this->sql->joins($join);
  93. $this->assert_equals("SELECT * FROM authors $join",(string)$this->sql);
  94. }
  95. public function test_group()
  96. {
  97. $this->sql->group('name');
  98. $this->assert_equals('SELECT * FROM authors GROUP BY name',(string)$this->sql);
  99. }
  100. public function test_having()
  101. {
  102. $this->sql->having("created_at > '2009-01-01'");
  103. $this->assert_equals("SELECT * FROM authors HAVING created_at > '2009-01-01'", (string)$this->sql);
  104. }
  105. public function test_all_clauses_after_where_should_be_correctly_ordered()
  106. {
  107. $this->sql->limit(10)->offset(1);
  108. $this->sql->having("created_at > '2009-01-01'");
  109. $this->sql->order('name');
  110. $this->sql->group('name');
  111. $this->sql->where(array('id' => 1));
  112. $this->assert_sql_has($this->conn->limit("SELECT * FROM authors WHERE id=? GROUP BY name HAVING created_at > '2009-01-01' ORDER BY name",1,10), (string)$this->sql);
  113. }
  114. /**
  115. * @expectedException ActiveRecord\ActiveRecordException
  116. */
  117. public function test_insert_requires_hash()
  118. {
  119. $this->sql->insert(array(1));
  120. }
  121. public function test_insert()
  122. {
  123. $this->sql->insert(array('id' => 1, 'name' => 'Tito'));
  124. $this->assert_sql_has("INSERT INTO authors(id,name) VALUES(?,?)",(string)$this->sql);
  125. }
  126. public function test_insert_with_null()
  127. {
  128. $this->sql->insert(array('id' => 1, 'name' => null));
  129. $this->assert_sql_has("INSERT INTO authors(id,name) VALUES(?,?)",$this->sql->to_s());
  130. }
  131. public function test_update_with_hash()
  132. {
  133. $this->sql->update(array('id' => 1, 'name' => 'Tito'))->where('id=1 AND name IN(?)',array('Tito','Mexican'));
  134. $this->assert_sql_has("UPDATE authors SET id=?, name=? WHERE id=1 AND name IN(?,?)",(string)$this->sql);
  135. $this->assert_equals(array(1,'Tito','Tito','Mexican'),$this->sql->bind_values());
  136. }
  137. public function test_update_with_limit_and_order()
  138. {
  139. if (!$this->conn->accepts_limit_and_order_for_update_and_delete())
  140. $this->mark_test_skipped('Only MySQL & Sqlite accept limit/order with UPDATE operation');
  141. $this->sql->update(array('id' => 1))->order('name asc')->limit(1);
  142. $this->assert_sql_has("UPDATE authors SET id=? ORDER BY name asc LIMIT 1", $this->sql->to_s());
  143. }
  144. public function test_update_with_string()
  145. {
  146. $this->sql->update("name='Bob'");
  147. $this->assert_sql_has("UPDATE authors SET name='Bob'", $this->sql->to_s());
  148. }
  149. public function test_update_with_null()
  150. {
  151. $this->sql->update(array('id' => 1, 'name' => null))->where('id=1');
  152. $this->assert_sql_has("UPDATE authors SET id=?, name=? WHERE id=1",$this->sql->to_s());
  153. }
  154. public function test_delete()
  155. {
  156. $this->sql->delete();
  157. $this->assert_equals('DELETE FROM authors',$this->sql->to_s());
  158. }
  159. public function test_delete_with_where()
  160. {
  161. $this->sql->delete('id=? or name in(?)',1,array('Tito','Mexican'));
  162. $this->assert_equals('DELETE FROM authors WHERE id=? or name in(?,?)',$this->sql->to_s());
  163. $this->assert_equals(array(1,'Tito','Mexican'),$this->sql->bind_values());
  164. }
  165. public function test_delete_with_hash()
  166. {
  167. $this->sql->delete(array('id' => 1, 'name' => array('Tito','Mexican')));
  168. $this->assert_sql_has("DELETE FROM authors WHERE id=? AND name IN(?,?)",$this->sql->to_s());
  169. $this->assert_equals(array(1,'Tito','Mexican'),$this->sql->get_where_values());
  170. }
  171. public function test_delete_with_limit_and_order()
  172. {
  173. if (!$this->conn->accepts_limit_and_order_for_update_and_delete())
  174. $this->mark_test_skipped('Only MySQL & Sqlite accept limit/order with DELETE operation');
  175. $this->sql->delete(array('id' => 1))->order('name asc')->limit(1);
  176. $this->assert_sql_has("DELETE FROM authors WHERE id=? ORDER BY name asc LIMIT 1",$this->sql->to_s());
  177. }
  178. public function test_reverse_order()
  179. {
  180. $this->assert_equals('id ASC, name DESC', SQLBuilder::reverse_order('id DESC, name ASC'));
  181. $this->assert_equals('id ASC, name DESC , zzz ASC', SQLBuilder::reverse_order('id DESC, name ASC , zzz DESC'));
  182. $this->assert_equals('id DESC, name DESC', SQLBuilder::reverse_order('id, name'));
  183. $this->assert_equals('id DESC', SQLBuilder::reverse_order('id'));
  184. $this->assert_equals('', SQLBuilder::reverse_order(''));
  185. $this->assert_equals(' ', SQLBuilder::reverse_order(' '));
  186. $this->assert_equals(null, SQLBuilder::reverse_order(null));
  187. }
  188. public function test_create_conditions_from_underscored_string()
  189. {
  190. $this->assert_conditions('id=? AND name=? OR z=?',array(1,'Tito','X'),'id_and_name_or_z');
  191. $this->assert_conditions('id=?',array(1),'id');
  192. $this->assert_conditions('id IN(?)',array(array(1,2)),'id');
  193. }
  194. public function test_create_conditions_from_underscored_string_with_nulls()
  195. {
  196. $this->assert_conditions('id=? AND name IS NULL',array(1,null),'id_and_name');
  197. }
  198. public function test_create_conditions_from_underscored_string_with_missing_args()
  199. {
  200. $this->assert_conditions('id=? AND name IS NULL OR z IS NULL',array(1,null),'id_and_name_or_z');
  201. $this->assert_conditions('id IS NULL',null,'id');
  202. }
  203. public function test_create_conditions_from_underscored_string_with_blank()
  204. {
  205. $this->assert_conditions('id=? AND name IS NULL OR z=?',array(1,null,''),'id_and_name_or_z');
  206. }
  207. public function test_create_conditions_from_underscored_string_invalid()
  208. {
  209. $this->assert_equals(null,$this->cond_from_s(''));
  210. $this->assert_equals(null,$this->cond_from_s(null));
  211. }
  212. public function test_create_conditions_from_underscored_string_with_mapped_columns()
  213. {
  214. $this->assert_conditions('id=? AND name=?',array(1,'Tito'),'id_and_my_name',array('my_name' => 'name'));
  215. }
  216. public function test_create_hash_from_underscored_string()
  217. {
  218. $values = array(1,'Tito');
  219. $hash = SQLBuilder::create_hash_from_underscored_string('id_and_my_name',$values);
  220. $this->assert_equals(array('id' => 1, 'my_name' => 'Tito'),$hash);
  221. }
  222. public function test_create_hash_from_underscored_string_with_mapped_columns()
  223. {
  224. $values = array(1,'Tito');
  225. $map = array('my_name' => 'name');
  226. $hash = SQLBuilder::create_hash_from_underscored_string('id_and_my_name',$values,$map);
  227. $this->assert_equals(array('id' => 1, 'name' => 'Tito'),$hash);
  228. }
  229. public function test_where_with_joins_prepends_table_name_to_fields()
  230. {
  231. $joins = 'INNER JOIN books ON (books.id = authors.id)';
  232. // joins needs to be called prior to where
  233. $this->sql->joins($joins);
  234. $this->sql->where(array('id' => 1, 'name' => 'Tito'));
  235. $this->assert_sql_has("SELECT * FROM authors $joins WHERE authors.id=? AND authors.name=?",(string)$this->sql);
  236. }
  237. };
  238. ?>