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