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