/vendor/php-activerecord/php-activerecord/test/SQLBuilderTest.php

https://gitlab.com/bandana/Astro-Veda · PHP · 282 lines · 230 code · 45 blank · 7 comment · 3 complexity · e138296b89c36e980096e798f67a1be6 MD5 · raw file

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