11
11
class ArrayQueryTest extends TestCase
12
12
{
13
13
/**
14
- * @return array
14
+ * @param array $config
15
+ * @return ArrayQuery array query instance.
15
16
*/
16
- protected function getTestData ( )
17
+ protected function createArrayQuery ( $ config = [] )
17
18
{
18
- return [
19
+ return ( new ArrayQuery ( $ config ))-> from ( [
19
20
[
20
21
'id ' => 1 ,
21
22
'username ' => 'admin ' ,
@@ -31,144 +32,132 @@ protected function getTestData()
31
32
'username ' => 'guest ' ,
32
33
33
34
],
34
- ];
35
+ ]) ;
35
36
}
36
37
37
38
// Tests :
38
39
39
40
public function testWhereCondition ()
40
41
{
41
- $ query = new ArrayQuery ();
42
- $ query ->from ($ this ->getTestData ());
42
+ $ query = $ this ->createArrayQuery ();
43
43
$ query ->where (['username ' => 'admin ' ]);
44
-
45
44
$ rows = $ query ->all ();
45
+
46
46
$ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
47
47
}
48
48
49
49
public function testLikeCondition ()
50
50
{
51
- $ query = new ArrayQuery ();
52
- $ query ->from ($ this ->getTestData ());
51
+ $ query = $ this ->createArrayQuery ();
53
52
$ query ->where (['like ' , 'email ' , 'test ' ]);
54
-
55
53
$ rows = $ query ->all ();
54
+
56
55
$ this ->assertEquals ('test ' , $ rows [0 ]['username ' ]);
57
56
}
58
57
59
58
public function testNotLikeCondition ()
60
59
{
61
- $ query = new ArrayQuery ();
62
- $ query ->from ($ this ->getTestData ());
60
+ $ query = $ this ->createArrayQuery ();
63
61
$ query ->where (['not like ' , 'username ' , 'admin ' ]);
64
-
65
62
$ rows = $ query ->all ();
63
+
66
64
$ this ->assertEquals ('guest ' , $ rows [1 ]['username ' ]);
67
65
$ this ->assertCount (2 , $ rows );
68
66
}
69
67
70
68
public function testApplyLimit ()
71
69
{
72
- $ query = new ArrayQuery ();
73
- $ query ->from ($ this ->getTestData ());
70
+ $ query = $ this ->createArrayQuery ();
74
71
$ query ->where (['like ' , 'email ' , 'example.com ' ]);
75
72
$ query ->limit (2 );
76
-
77
73
$ rows = $ query ->all ();
78
- $ this ->assertEquals (2 , count ($ rows ));
74
+
75
+ $ this ->assertCount (2 , $ rows );
79
76
$ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
80
77
$ this ->assertEquals ('test ' , $ rows [1 ]['username ' ]);
81
78
}
82
79
83
80
public function testFetchFirstRow ()
84
81
{
85
- $ query = new ArrayQuery ();
86
- $ query ->from ($ this ->getTestData ());
87
-
82
+ $ query = $ this ->createArrayQuery ();
88
83
$ row = $ query ->one ();
84
+
89
85
$ this ->assertEquals ('admin ' , $ row ['username ' ]);
90
86
}
91
87
92
88
public function testOrCondition ()
93
89
{
94
- $ query = new ArrayQuery ();
95
- $ query ->from ($ this ->getTestData ());
90
+ $ query = $ this ->createArrayQuery ();
96
91
$ query ->where (['or ' , ['username ' => 'admin ' ], ['id ' => 3 ]]);
97
-
98
92
$ rows = $ query ->all ();
99
- $ this ->assertEquals (2 , count ($ rows ));
93
+
94
+ $ this ->assertCount (2 , $ rows );
100
95
$ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
101
96
$ this ->assertEquals (3 , $ rows [1 ]['id ' ]);
102
97
}
103
98
104
99
public function testBetweenCondition ()
105
100
{
106
- $ query = new ArrayQuery ();
107
- $ query ->from ($ this ->getTestData ());
101
+ $ query = $ this ->createArrayQuery ();
108
102
$ query ->where (['between ' , 'id ' , 1 , 2 ]);
109
-
110
103
$ rows = $ query ->all ();
111
- $ this ->assertEquals (2 , count ($ rows ));
104
+
105
+ $ this ->assertCount (2 , $ rows );
112
106
$ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
113
107
$ this ->assertEquals ('test ' , $ rows [1 ]['username ' ]);
114
108
}
115
109
116
110
public function testNotCondition ()
117
111
{
118
- $ query = new ArrayQuery ();
119
- $ query ->from ($ this ->getTestData ());
112
+ $ query = $ this ->createArrayQuery ();
120
113
$ query ->where (['not ' , ['username ' => 'admin ' ]]);
121
-
122
114
$ rows = $ query ->all ();
123
- $ this ->assertEquals (2 , count ($ rows ));
115
+
116
+ $ this ->assertCount (2 , $ rows );
124
117
$ this ->assertEquals ('test ' , $ rows [0 ]['username ' ]);
125
118
$ this ->assertEquals ('guest ' , $ rows [1 ]['username ' ]);
126
119
}
127
120
128
121
public function testInCondition ()
129
122
{
130
- $ query = new ArrayQuery ();
131
- $ query ->from ($ this ->getTestData ());
123
+ $ query = $ this ->createArrayQuery ();
132
124
$ query ->where (['in ' , 'id ' , [1 , 3 ]]);
133
-
134
125
$ rows = $ query ->all ();
135
- $ this ->assertEquals (2 , count ($ rows ));
126
+
127
+ $ this ->assertCount (2 , $ rows );
136
128
$ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
137
129
$ this ->assertEquals ('guest ' , $ rows [1 ]['username ' ]);
138
130
}
139
131
140
132
public function testExistsCondition ()
141
133
{
142
- $ query = new ArrayQuery ();
143
- $ query ->from ($ this ->getTestData ());
134
+ $ query = $ this ->createArrayQuery ();
144
135
$ query ->where (['username ' => 'admin ' ]);
145
136
146
137
$ this ->assertTrue ($ query ->exists ());
147
138
}
148
139
149
140
public function testOrderByASC ()
150
141
{
151
- $ query = new ArrayQuery ();
152
- $ query ->from ($ this ->getTestData ());
142
+ $ query = $ this ->createArrayQuery ();
153
143
$ query ->orderBy ('email ' );
154
-
155
144
$ rows = $ query ->all ();
145
+
156
146
$ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
157
147
}
158
148
159
149
public function testOrderByDESC ()
160
150
{
161
- $ query = new ArrayQuery ();
162
- $ query ->from ($ this ->getTestData ());
151
+ $ query = $ this ->createArrayQuery ();
163
152
$ query ->orderBy (['email ' => SORT_DESC ]);
164
-
165
153
$ rows = $ query ->all ();
154
+
166
155
$ this ->assertEquals ('test ' , $ rows [0 ]['username ' ]);
167
156
}
168
157
169
158
public function testFilterWhereCondition ()
170
159
{
171
- $ query = ( new ArrayQuery ())-> from ( $ this ->getTestData () );
160
+ $ query = $ this ->createArrayQuery ( );
172
161
$ query ->filterWhere (['username ' => 'admin ' ]);
173
162
$ rows = $ query ->all ();
174
163
@@ -178,7 +167,7 @@ public function testFilterWhereCondition()
178
167
179
168
public function testFilterAndCondition ()
180
169
{
181
- $ query = ( new ArrayQuery ())-> from ( $ this ->getTestData () );
170
+ $ query = $ this ->createArrayQuery ( );
182
171
$ query ->filterWhere (['username ' => 'guest ' ]);
183
172
$ query->
andFilterWhere ([
'email ' =>
'[email protected] ' ]);
184
173
$ rows = $ query ->all ();
@@ -189,7 +178,7 @@ public function testFilterAndCondition()
189
178
190
179
public function testFilterOrCondition ()
191
180
{
192
- $ query = ( new ArrayQuery ())-> from ( $ this ->getTestData () );
181
+ $ query = $ this ->createArrayQuery ( );
193
182
$ query ->filterWhere (['username ' => 'guest ' ]);
194
183
$ query ->orFilterWhere (['username ' => 'admin ' ]);
195
184
$ rows = $ query ->all ();
@@ -201,7 +190,7 @@ public function testFilterOrCondition()
201
190
202
191
public function testFilterNotCondition ()
203
192
{
204
- $ query = ( new ArrayQuery ())-> from ( $ this ->getTestData () );
193
+ $ query = $ this ->createArrayQuery ( );
205
194
$ query ->filterWhere (['not ' , ['username ' => 'guest ' ]]);
206
195
$ rows = $ query ->all ();
207
196
@@ -212,7 +201,7 @@ public function testFilterNotCondition()
212
201
213
202
public function testFilterBetweenCondition ()
214
203
{
215
- $ query = ( new ArrayQuery ())-> from ( $ this ->getTestData () );
204
+ $ query = $ this ->createArrayQuery ( );
216
205
$ query ->filterWhere (['between ' , 'id ' , 2 , 3 ]);
217
206
$ rows = $ query ->all ();
218
207
@@ -223,7 +212,7 @@ public function testFilterBetweenCondition()
223
212
224
213
public function testFilterInCondition ()
225
214
{
226
- $ query = ( new ArrayQuery ())-> from ( $ this ->getTestData () );
215
+ $ query = $ this ->createArrayQuery ( );
227
216
$ query ->filterWhere (['in ' , 'id ' , [1 , 2 , 3 ]]);
228
217
$ rows = $ query ->all ();
229
218
@@ -235,23 +224,24 @@ public function testFilterInCondition()
235
224
236
225
public function testFilterLikeCondition ()
237
226
{
238
- $ query = ( new ArrayQuery ())-> from ( $ this ->getTestData () );
227
+ $ query = $ this ->createArrayQuery ( );
239
228
$ query ->filterWhere (['like ' , 'username ' , 'gu ' ]);
240
229
$ query ->orFilterWhere (['like ' , 'username ' , 'ad ' ]);
241
230
$ rows = $ query ->all ();
242
231
243
-
244
232
$ this ->assertEquals ('guest ' , $ rows [0 ]['username ' ]);
245
233
$ this ->assertEquals ('admin ' , $ rows [1 ]['username ' ]);
246
234
$ this ->assertCount (2 , $ rows );
247
235
}
248
236
249
237
public function testSetCustomPrimaryKey ()
250
238
{
251
- $ query = ( new ArrayQuery ( ['primaryKeyName ' => 'username ' ]))-> from ( $ this -> getTestData () );
239
+ $ query = $ this -> createArrayQuery ( ['primaryKeyName ' => 'username ' ]);
252
240
$ query ->where (['not ' , ['username ' => 'admin ' ]]);
253
241
$ rows = $ query ->all ();
254
242
255
243
$ this ->assertCount (2 , $ rows );
244
+ $ this ->assertEquals ('username ' , $ query ->primaryKeyName );
245
+
256
246
}
257
247
}
0 commit comments