ExpressionsTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. include 'helpers/config.php';
  3. require '../lib/Expressions.php';
  4. use ActiveRecord\Expressions;
  5. use ActiveRecord\ConnectionManager;
  6. use ActiveRecord\DatabaseException;
  7. class ExpressionsTest extends SnakeCase_PHPUnit_Framework_TestCase
  8. {
  9. public function test_values()
  10. {
  11. $c = new Expressions(null,'a=? and b=?',1,2);
  12. $this->assert_equals(array(1,2), $c->values());
  13. }
  14. public function test_one_variable()
  15. {
  16. $c = new Expressions(null,'name=?','Tito');
  17. $this->assert_equals('name=?',$c->to_s());
  18. $this->assert_equals(array('Tito'),$c->values());
  19. }
  20. public function test_array_variable()
  21. {
  22. $c = new Expressions(null,'name IN(?) and id=?',array('Tito','George'),1);
  23. $this->assert_equals(array(array('Tito','George'),1),$c->values());
  24. }
  25. public function test_multiple_variables()
  26. {
  27. $c = new Expressions(null,'name=? and book=?','Tito','Sharks');
  28. $this->assert_equals('name=? and book=?',$c->to_s());
  29. $this->assert_equals(array('Tito','Sharks'),$c->values());
  30. }
  31. public function test_to_string()
  32. {
  33. $c = new Expressions(null,'name=? and book=?','Tito','Sharks');
  34. $this->assert_equals('name=? and book=?',$c->to_s());
  35. }
  36. public function test_to_string_with_array_variable()
  37. {
  38. $c = new Expressions(null,'name IN(?) and id=?',array('Tito','George'),1);
  39. $this->assert_equals('name IN(?,?) and id=?',$c->to_s());
  40. }
  41. public function test_to_string_with_null_options()
  42. {
  43. $c = new Expressions(null,'name=? and book=?','Tito','Sharks');
  44. $x = null;
  45. $this->assert_equals('name=? and book=?',$c->to_s(false,$x));
  46. }
  47. /**
  48. * @expectedException ActiveRecord\ExpressionsException
  49. */
  50. public function test_insufficient_variables()
  51. {
  52. $c = new Expressions(null,'name=? and id=?','Tito');
  53. $c->to_s();
  54. }
  55. public function test_no_values()
  56. {
  57. $c = new Expressions(null,"name='Tito'");
  58. $this->assert_equals("name='Tito'",$c->to_s());
  59. $this->assert_equals(0,count($c->values()));
  60. }
  61. public function test_null_variable()
  62. {
  63. $a = new Expressions(null,'name=?',null);
  64. $this->assert_equals('name=?',$a->to_s());
  65. $this->assert_equals(array(null),$a->values());
  66. }
  67. public function test_zero_variable()
  68. {
  69. $a = new Expressions(null,'name=?',0);
  70. $this->assert_equals('name=?',$a->to_s());
  71. $this->assert_equals(array(0),$a->values());
  72. }
  73. public function test_ignore_invalid_parameter_marker()
  74. {
  75. $a = new Expressions(null,"question='Do you love backslashes?' and id in(?)",array(1,2));
  76. $this->assert_equals("question='Do you love backslashes?' and id in(?,?)",$a->to_s());
  77. }
  78. public function test_ignore_parameter_marker_with_escaped_quote()
  79. {
  80. $a = new Expressions(null,"question='Do you love''s backslashes?' and id in(?)",array(1,2));
  81. $this->assert_equals("question='Do you love''s backslashes?' and id in(?,?)",$a->to_s());
  82. }
  83. public function test_ignore_parameter_marker_with_backspace_escaped_quote()
  84. {
  85. $a = new Expressions(null,"question='Do you love\\'s backslashes?' and id in(?)",array(1,2));
  86. $this->assert_equals("question='Do you love\\'s backslashes?' and id in(?,?)",$a->to_s());
  87. }
  88. public function test_substitute()
  89. {
  90. $a = new Expressions(null,'name=? and id=?','Tito',1);
  91. $this->assert_equals("name='Tito' and id=1",$a->to_s(true));
  92. }
  93. public function test_substitute_quotes_scalars_but_not_others()
  94. {
  95. $a = new Expressions(null,'id in(?)',array(1,'2',3.5));
  96. $this->assert_equals("id in(1,'2',3.5)",$a->to_s(true));
  97. }
  98. public function test_substitute_where_value_has_question_mark()
  99. {
  100. $a = new Expressions(null,'name=? and id=?','??????',1);
  101. $this->assert_equals("name='??????' and id=1",$a->to_s(true));
  102. }
  103. public function test_substitute_array_value()
  104. {
  105. $a = new Expressions(null,'id in(?)',array(1,2));
  106. $this->assert_equals("id in(1,2)",$a->to_s(true));
  107. }
  108. public function test_substitute_escapes_quotes()
  109. {
  110. $a = new Expressions(null,'name=? or name in(?)',"Tito's Guild",array(1,"Tito's Guild"));
  111. $this->assert_equals("name='Tito''s Guild' or name in(1,'Tito''s Guild')",$a->to_s(true));
  112. }
  113. public function test_substitute_escape_quotes_with_connections_escape_method()
  114. {
  115. try {
  116. $conn = ConnectionManager::get_connection();
  117. } catch (DatabaseException $e) {
  118. $this->mark_test_skipped('failed to connect. '.$e->getMessage());
  119. }
  120. $a = new Expressions(null,'name=?',"Tito's Guild");
  121. $a->set_connection($conn);
  122. $escaped = $conn->escape("Tito's Guild");
  123. $this->assert_equals("name=$escaped",$a->to_s(true));
  124. }
  125. public function test_bind()
  126. {
  127. $a = new Expressions(null,'name=? and id=?','Tito');
  128. $a->bind(2,1);
  129. $this->assert_equals(array('Tito',1),$a->values());
  130. }
  131. public function test_bind_overwrite_existing()
  132. {
  133. $a = new Expressions(null,'name=? and id=?','Tito',1);
  134. $a->bind(2,99);
  135. $this->assert_equals(array('Tito',99),$a->values());
  136. }
  137. /**
  138. * @expectedException ActiveRecord\ExpressionsException
  139. */
  140. public function test_bind_invalid_parameter_number()
  141. {
  142. $a = new Expressions(null,'name=?');
  143. $a->bind(0,99);
  144. }
  145. public function test_subsitute_using_alternate_values()
  146. {
  147. $a = new Expressions(null,'name=?','Tito');
  148. $this->assert_equals("name='Tito'",$a->to_s(true));
  149. $x = array('values' => array('Hocus'));
  150. $this->assert_equals("name='Hocus'",$a->to_s(true,$x));
  151. }
  152. public function test_null_value()
  153. {
  154. $a = new Expressions(null,'name=?',null);
  155. $this->assert_equals('name=NULL',$a->to_s(true));
  156. }
  157. public function test_hash_with_default_glue()
  158. {
  159. $a = new Expressions(null,array('id' => 1, 'name' => 'Tito'));
  160. $this->assert_equals('id=? AND name=?',$a->to_s());
  161. }
  162. public function test_hash_with_glue()
  163. {
  164. $a = new Expressions(null,array('id' => 1, 'name' => 'Tito'),', ');
  165. $this->assert_equals('id=?, name=?',$a->to_s());
  166. }
  167. public function test_hash_with_array()
  168. {
  169. $a = new Expressions(null,array('id' => 1, 'name' => array('Tito','Mexican')));
  170. $this->assert_equals('id=? AND name IN(?,?)',$a->to_s());
  171. }
  172. }
  173. ?>