Skip to content

Commit ba155b4

Browse files
author
igor
committed
update tests, change visibility from protected to private for queryProcessor property
1 parent ac3534f commit ba155b4

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

ArrayQuery.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ class ArrayQuery extends Component
3232
/**
3333
* @var QueryProcessor
3434
*/
35-
protected $queryProcessor;
35+
private $_queryProcessor;
3636

3737
/**
3838
* @inheritdoc
3939
*/
4040
public function init()
4141
{
42-
$this->queryProcessor = Yii::createObject($this->queryProcessorClass);
42+
$this->_queryProcessor = Yii::createObject($this->queryProcessorClass);
4343

4444
parent::init();
4545
}
@@ -112,7 +112,7 @@ public function from(array $data)
112112
*/
113113
protected function fetchData()
114114
{
115-
return $this->queryProcessor->process($this);
115+
return $this->_queryProcessor->process($this);
116116
}
117117

118118
/**

tests/ArrayQueryTest.php

+43-53
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
class ArrayQueryTest extends TestCase
1212
{
1313
/**
14-
* @return array
14+
* @param array $config
15+
* @return ArrayQuery array query instance.
1516
*/
16-
protected function getTestData()
17+
protected function createArrayQuery($config = [])
1718
{
18-
return [
19+
return (new ArrayQuery($config))->from([
1920
[
2021
'id' => 1,
2122
'username' => 'admin',
@@ -31,144 +32,132 @@ protected function getTestData()
3132
'username' => 'guest',
3233
'email' => '[email protected]'
3334
],
34-
];
35+
]);
3536
}
3637

3738
// Tests :
3839

3940
public function testWhereCondition()
4041
{
41-
$query = new ArrayQuery();
42-
$query->from($this->getTestData());
42+
$query = $this->createArrayQuery();
4343
$query->where(['username' => 'admin']);
44-
4544
$rows = $query->all();
45+
4646
$this->assertEquals('admin', $rows[0]['username']);
4747
}
4848

4949
public function testLikeCondition()
5050
{
51-
$query = new ArrayQuery();
52-
$query->from($this->getTestData());
51+
$query = $this->createArrayQuery();
5352
$query->where(['like', 'email', 'test']);
54-
5553
$rows = $query->all();
54+
5655
$this->assertEquals('test', $rows[0]['username']);
5756
}
5857

5958
public function testNotLikeCondition()
6059
{
61-
$query = new ArrayQuery();
62-
$query->from($this->getTestData());
60+
$query = $this->createArrayQuery();
6361
$query->where(['not like', 'username', 'admin']);
64-
6562
$rows = $query->all();
63+
6664
$this->assertEquals('guest', $rows[1]['username']);
6765
$this->assertCount(2, $rows);
6866
}
6967

7068
public function testApplyLimit()
7169
{
72-
$query = new ArrayQuery();
73-
$query->from($this->getTestData());
70+
$query = $this->createArrayQuery();
7471
$query->where(['like', 'email', 'example.com']);
7572
$query->limit(2);
76-
7773
$rows = $query->all();
78-
$this->assertEquals(2, count($rows));
74+
75+
$this->assertCount(2, $rows);
7976
$this->assertEquals('admin', $rows[0]['username']);
8077
$this->assertEquals('test', $rows[1]['username']);
8178
}
8279

8380
public function testFetchFirstRow()
8481
{
85-
$query = new ArrayQuery();
86-
$query->from($this->getTestData());
87-
82+
$query = $this->createArrayQuery();
8883
$row = $query->one();
84+
8985
$this->assertEquals('admin', $row['username']);
9086
}
9187

9288
public function testOrCondition()
9389
{
94-
$query = new ArrayQuery();
95-
$query->from($this->getTestData());
90+
$query = $this->createArrayQuery();
9691
$query->where(['or', ['username' => 'admin'], ['id' => 3]]);
97-
9892
$rows = $query->all();
99-
$this->assertEquals(2, count($rows));
93+
94+
$this->assertCount(2, $rows);
10095
$this->assertEquals('admin', $rows[0]['username']);
10196
$this->assertEquals(3, $rows[1]['id']);
10297
}
10398

10499
public function testBetweenCondition()
105100
{
106-
$query = new ArrayQuery();
107-
$query->from($this->getTestData());
101+
$query = $this->createArrayQuery();
108102
$query->where(['between', 'id', 1, 2]);
109-
110103
$rows = $query->all();
111-
$this->assertEquals(2, count($rows));
104+
105+
$this->assertCount(2, $rows);
112106
$this->assertEquals('admin', $rows[0]['username']);
113107
$this->assertEquals('test', $rows[1]['username']);
114108
}
115109

116110
public function testNotCondition()
117111
{
118-
$query = new ArrayQuery();
119-
$query->from($this->getTestData());
112+
$query = $this->createArrayQuery();
120113
$query->where(['not', ['username' => 'admin']]);
121-
122114
$rows = $query->all();
123-
$this->assertEquals(2, count($rows));
115+
116+
$this->assertCount(2, $rows);
124117
$this->assertEquals('test', $rows[0]['username']);
125118
$this->assertEquals('guest', $rows[1]['username']);
126119
}
127120

128121
public function testInCondition()
129122
{
130-
$query = new ArrayQuery();
131-
$query->from($this->getTestData());
123+
$query = $this->createArrayQuery();
132124
$query->where(['in', 'id', [1, 3]]);
133-
134125
$rows = $query->all();
135-
$this->assertEquals(2, count($rows));
126+
127+
$this->assertCount(2, $rows);
136128
$this->assertEquals('admin', $rows[0]['username']);
137129
$this->assertEquals('guest', $rows[1]['username']);
138130
}
139131

140132
public function testExistsCondition()
141133
{
142-
$query = new ArrayQuery();
143-
$query->from($this->getTestData());
134+
$query = $this->createArrayQuery();
144135
$query->where(['username' => 'admin']);
145136

146137
$this->assertTrue($query->exists());
147138
}
148139

149140
public function testOrderByASC()
150141
{
151-
$query = new ArrayQuery();
152-
$query->from($this->getTestData());
142+
$query = $this->createArrayQuery();
153143
$query->orderBy('email');
154-
155144
$rows = $query->all();
145+
156146
$this->assertEquals('admin', $rows[0]['username']);
157147
}
158148

159149
public function testOrderByDESC()
160150
{
161-
$query = new ArrayQuery();
162-
$query->from($this->getTestData());
151+
$query = $this->createArrayQuery();
163152
$query->orderBy(['email' => SORT_DESC]);
164-
165153
$rows = $query->all();
154+
166155
$this->assertEquals('test', $rows[0]['username']);
167156
}
168157

169158
public function testFilterWhereCondition()
170159
{
171-
$query = (new ArrayQuery())->from($this->getTestData());
160+
$query = $this->createArrayQuery();
172161
$query->filterWhere(['username' => 'admin']);
173162
$rows = $query->all();
174163

@@ -178,7 +167,7 @@ public function testFilterWhereCondition()
178167

179168
public function testFilterAndCondition()
180169
{
181-
$query = (new ArrayQuery())->from($this->getTestData());
170+
$query = $this->createArrayQuery();
182171
$query->filterWhere(['username' => 'guest']);
183172
$query->andFilterWhere(['email' => '[email protected]']);
184173
$rows = $query->all();
@@ -189,7 +178,7 @@ public function testFilterAndCondition()
189178

190179
public function testFilterOrCondition()
191180
{
192-
$query = (new ArrayQuery())->from($this->getTestData());
181+
$query = $this->createArrayQuery();
193182
$query->filterWhere(['username' => 'guest']);
194183
$query->orFilterWhere(['username' => 'admin']);
195184
$rows = $query->all();
@@ -201,7 +190,7 @@ public function testFilterOrCondition()
201190

202191
public function testFilterNotCondition()
203192
{
204-
$query = (new ArrayQuery())->from($this->getTestData());
193+
$query = $this->createArrayQuery();
205194
$query->filterWhere(['not', ['username' => 'guest']]);
206195
$rows = $query->all();
207196

@@ -212,7 +201,7 @@ public function testFilterNotCondition()
212201

213202
public function testFilterBetweenCondition()
214203
{
215-
$query = (new ArrayQuery())->from($this->getTestData());
204+
$query = $this->createArrayQuery();
216205
$query->filterWhere(['between', 'id', 2, 3]);
217206
$rows = $query->all();
218207

@@ -223,7 +212,7 @@ public function testFilterBetweenCondition()
223212

224213
public function testFilterInCondition()
225214
{
226-
$query = (new ArrayQuery())->from($this->getTestData());
215+
$query = $this->createArrayQuery();
227216
$query->filterWhere(['in', 'id', [1, 2, 3]]);
228217
$rows = $query->all();
229218

@@ -235,23 +224,24 @@ public function testFilterInCondition()
235224

236225
public function testFilterLikeCondition()
237226
{
238-
$query = (new ArrayQuery())->from($this->getTestData());
227+
$query = $this->createArrayQuery();
239228
$query->filterWhere(['like', 'username', 'gu']);
240229
$query->orFilterWhere(['like', 'username', 'ad']);
241230
$rows = $query->all();
242231

243-
244232
$this->assertEquals('guest', $rows[0]['username']);
245233
$this->assertEquals('admin', $rows[1]['username']);
246234
$this->assertCount(2, $rows);
247235
}
248236

249237
public function testSetCustomPrimaryKey()
250238
{
251-
$query = (new ArrayQuery(['primaryKeyName' => 'username']))->from($this->getTestData());
239+
$query = $this->createArrayQuery(['primaryKeyName' => 'username']);
252240
$query->where(['not', ['username' => 'admin']]);
253241
$rows = $query->all();
254242

255243
$this->assertCount(2, $rows);
244+
$this->assertEquals('username', $query->primaryKeyName);
245+
256246
}
257247
}

0 commit comments

Comments
 (0)