Skip to content

Commit afd2e28

Browse files
authored
Update tests. Fix overwrite where condition. (#407)
1 parent 0f3fd98 commit afd2e28

7 files changed

+55
-57
lines changed

src/ActiveQuery.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
218218
}
219219

220220
$query = $this->createInstance();
221-
$this->where($where);
221+
$this->setWhere($where);
222222
}
223223

224224
if (!empty($this->on)) {
@@ -919,7 +919,7 @@ protected function findByCondition(mixed $condition): static
919919
$condition = $arInstance->filterCondition($condition, $aliases);
920920
}
921921

922-
return $this->where($condition);
922+
return $this->setWhere($condition);
923923
}
924924

925925
public function findBySql(string $sql, array $params = []): static

tests/ActiveQueryFindTest.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function testFindExists(): void
4848
$customerQuery = new ActiveQuery(Customer::class);
4949

5050
$this->assertTrue($customerQuery->where(['[[id]]' => 2])->exists());
51-
$this->assertTrue($customerQuery->where(['[[id]]' => 2])->select('[[name]]')->exists());
51+
$this->assertTrue($customerQuery->setWhere(['[[id]]' => 2])->select('[[name]]')->exists());
5252

53-
$this->assertFalse($customerQuery->where(['[[id]]' => 42])->exists());
54-
$this->assertFalse($customerQuery->where(['[[id]]' => 42])->select('[[name]]')->exists());
53+
$this->assertFalse($customerQuery->setWhere(['[[id]]' => 42])->exists());
54+
$this->assertFalse($customerQuery->setWhere(['[[id]]' => 42])->select('[[name]]')->exists());
5555
}
5656

5757
public function testFindColumn(): void
@@ -104,7 +104,7 @@ public function testFindLazyViaTable(): void
104104
$this->assertCount(0, $orders->getBooks());
105105
$this->assertEquals(2, $orders->get('id'));
106106

107-
$orders = $orderQuery->where(['id' => 1])->asArray()->one();
107+
$orders = $orderQuery->setWhere(['id' => 1])->asArray()->one();
108108
$this->assertIsArray($orders);
109109
}
110110

@@ -234,7 +234,7 @@ public function testFind(): void
234234
$customer = $customerQuery->findOne(['id' => [5, 6, 1]]);
235235
$this->assertInstanceOf(Customer::class, $customer);
236236

237-
$customer = $customerQuery->where(['id' => [5, 6, 1]])->one();
237+
$customer = $customerQuery->setWhere(['id' => [5, 6, 1]])->one();
238238
$this->assertNotNull($customer);
239239

240240
/** find by column values */
@@ -380,9 +380,9 @@ public function testFindCount(): void
380380
$customerQuery = new ActiveQuery(Customer::class);
381381
$this->assertEquals(3, $customerQuery->count());
382382
$this->assertEquals(1, $customerQuery->where(['id' => 1])->count());
383-
$this->assertEquals(2, $customerQuery->where(['id' => [1, 2]])->count());
384-
$this->assertEquals(2, $customerQuery->where(['id' => [1, 2]])->offset(1)->count());
385-
$this->assertEquals(2, $customerQuery->where(['id' => [1, 2]])->offset(2)->count());
383+
$this->assertEquals(2, $customerQuery->setWhere(['id' => [1, 2]])->count());
384+
$this->assertEquals(2, $customerQuery->setWhere(['id' => [1, 2]])->offset(1)->count());
385+
$this->assertEquals(2, $customerQuery->setWhere(['id' => [1, 2]])->offset(2)->count());
386386

387387
$customerQuery = new ActiveQuery(Customer::class);
388388
$this->assertEquals(3, $customerQuery->limit(1)->count());
@@ -455,27 +455,27 @@ public function testFindComplexCondition(): void
455455

456456
$this->assertCount(
457457
2,
458-
$customerQuery->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->all()
458+
$customerQuery->setWhere(['OR', ['name' => 'user1'], ['name' => 'user2']])->all()
459459
);
460460

461461
$this->assertEquals(
462462
2,
463-
$customerQuery->where(['name' => ['user1', 'user2']])->count()
463+
$customerQuery->setWhere(['name' => ['user1', 'user2']])->count()
464464
);
465465

466466
$this->assertCount(
467467
2,
468-
$customerQuery->where(['name' => ['user1', 'user2']])->all()
468+
$customerQuery->setWhere(['name' => ['user1', 'user2']])->all()
469469
);
470470

471471
$this->assertEquals(
472472
1,
473-
$customerQuery->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->count()
473+
$customerQuery->setWhere(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->count()
474474
);
475475

476476
$this->assertCount(
477477
1,
478-
$customerQuery->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->all()
478+
$customerQuery->setWhere(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->all()
479479
);
480480
}
481481

@@ -489,7 +489,7 @@ public function testFindNullValues(): void
489489
$customer->setName(null);
490490
$customer->save();
491491

492-
$result = $customerQuery->where(['name' => null])->all();
492+
$result = $customerQuery->setWhere(['name' => null])->all();
493493
$this->assertCount(1, $result);
494494
$this->assertEquals(2, reset($result)->getPrimaryKey());
495495
}
@@ -652,13 +652,13 @@ public function testFindEmptyInCondition(): void
652652
$customers = $customerQuery->where(['id' => [1]])->all();
653653
$this->assertCount(1, $customers);
654654

655-
$customers = $customerQuery->where(['id' => []])->all();
655+
$customers = $customerQuery->setWhere(['id' => []])->all();
656656
$this->assertCount(0, $customers);
657657

658-
$customers = $customerQuery->where(['IN', 'id', [1]])->all();
658+
$customers = $customerQuery->setWhere(['IN', 'id', [1]])->all();
659659
$this->assertCount(1, $customers);
660660

661-
$customers = $customerQuery->where(['IN', 'id', []])->all();
661+
$customers = $customerQuery->setWhere(['IN', 'id', []])->all();
662662
$this->assertCount(0, $customers);
663663
}
664664

@@ -675,7 +675,7 @@ public function testFindEagerIndexBy(): void
675675
$this->assertTrue(isset($items[1]));
676676
$this->assertTrue(isset($items[2]));
677677

678-
$order = $orderQuery->with('itemsIndexed')->where(['id' => 2])->one();
678+
$order = $orderQuery->with('itemsIndexed')->setWhere(['id' => 2])->one();
679679
$this->assertTrue($order->isRelationPopulated('itemsIndexed'));
680680

681681
$items = $order->getItemsIndexed();

tests/ActiveQueryTest.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ public function testDeeplyNestedTableRelation(): void
425425
$customerQuery = new ActiveQuery(Customer::class);
426426

427427
$customers = $customerQuery->findOne(1);
428-
$this->assertNotNull($customerQuery);
429428

430429
$items = $customers->getOrderItems();
431430

@@ -460,7 +459,7 @@ public function testDeeplyNestedTableRelation2(): void
460459
sort($ids);
461460
$this->assertEquals([1, 3], $ids);
462461

463-
$categories = $categoryQuery->where(['id' => 2])->one();
462+
$categories = $categoryQuery->setWhere(['id' => 2])->one();
464463
$this->assertNotNull($categories);
465464

466465
$orders = $categories->getOrders();
@@ -1393,8 +1392,7 @@ public function testInverseOf(): void
13931392
$customer = $customerQuery->with('orders2')->where(['id' => 1])->one();
13941393
$this->assertSame($customer->getOrders2()[0]->getCustomer2(), $customer);
13951394

1396-
//$customerQuery = new ActiveQuery(Customer::class);
1397-
$customers = $customerQuery->with('orders2')->where(['id' => [1, 3]])->all();
1395+
$customers = $customerQuery->with('orders2')->setWhere(['id' => [1, 3]])->all();
13981396
$this->assertEmpty($customers[1]->getOrders2());
13991397
$this->assertSame($customers[0]->getOrders2()[0]->getCustomer2(), $customers[0]);
14001398

@@ -1730,7 +1728,7 @@ public function testUnlinkAllOnCondition(): void
17301728
$category->unlinkAll('limitedItems', true);
17311729

17321730
/** Make sure that only one item was unlinked */
1733-
$itemsCount = $itemQuery->where(['category_id' => 2])->count();
1731+
$itemsCount = $itemQuery->setWhere(['category_id' => 2])->count();
17341732
$this->assertEquals(2, $itemsCount);
17351733

17361734
/** Call $categoryQuery again to ensure no items were found */
@@ -1765,7 +1763,7 @@ public function testUnlinkAllOnConditionViaTable(): void
17651763
$this->assertCount(0, $orderQuery->one()->getLimitedItems());
17661764

17671765
/** Make sure that only links were removed, the items were not removed */
1768-
$this->assertEquals(3, $itemQuery->where(['category_id' => 2])->count());
1766+
$this->assertEquals(3, $itemQuery->setWhere(['category_id' => 2])->count());
17691767
}
17701768

17711769
/**
@@ -2350,12 +2348,12 @@ public function testExists(): void
23502348
$customer = new ActiveQuery(Customer::class);
23512349

23522350
$this->assertTrue($customer->where(['id' => 2])->exists());
2353-
$this->assertFalse($customer->where(['id' => 5])->exists());
2354-
$this->assertTrue($customer->where(['name' => 'user1'])->exists());
2355-
$this->assertFalse($customer->where(['name' => 'user5'])->exists());
2356-
$this->assertTrue($customer->where(['id' => [2, 3]])->exists());
2357-
$this->assertTrue($customer->where(['id' => [2, 3]])->offset(1)->exists());
2358-
$this->assertFalse($customer->where(['id' => [2, 3]])->offset(2)->exists());
2351+
$this->assertFalse($customer->setWhere(['id' => 5])->exists());
2352+
$this->assertTrue($customer->setWhere(['name' => 'user1'])->exists());
2353+
$this->assertFalse($customer->setWhere(['name' => 'user5'])->exists());
2354+
$this->assertTrue($customer->setWhere(['id' => [2, 3]])->exists());
2355+
$this->assertTrue($customer->setWhere(['id' => [2, 3]])->offset(1)->exists());
2356+
$this->assertFalse($customer->setWhere(['id' => [2, 3]])->offset(2)->exists());
23592357
}
23602358

23612359
public function testUnlink(): void

tests/ActiveRecordTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function testPopulateRecordCallWhenQueryingOnParentClass(): void
239239
$animals = $animal->where(['type' => Dog::class])->one();
240240
$this->assertEquals('bark', $animals->getDoes());
241241

242-
$animals = $animal->where(['type' => Cat::class])->one();
242+
$animals = $animal->setWhere(['type' => Cat::class])->one();
243243
$this->assertEquals('meow', $animals->getDoes());
244244
}
245245

tests/Driver/Pgsql/ActiveRecordTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,18 @@ public function testBooleanValues(): void
149149
$boolARQuery = new ActiveQuery(BoolAR::class);
150150

151151
$this->assertTrue($boolARQuery->where(['bool_col' => true])->one()->bool_col);
152-
$this->assertFalse($boolARQuery->where(['bool_col' => false])->one()->bool_col);
152+
$this->assertFalse($boolARQuery->setWhere(['bool_col' => false])->one()->bool_col);
153153

154-
$this->assertEquals(1, $boolARQuery->where('bool_col = TRUE')->count('*'));
155-
$this->assertEquals(1, $boolARQuery->where('bool_col = FALSE')->count('*'));
156-
$this->assertEquals(2, $boolARQuery->where('bool_col IN (TRUE, FALSE)')->count('*'));
154+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = TRUE')->count('*'));
155+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = FALSE')->count('*'));
156+
$this->assertEquals(2, $boolARQuery->setWhere('bool_col IN (TRUE, FALSE)')->count('*'));
157157

158-
$this->assertEquals(1, $boolARQuery->where(['bool_col' => true])->count('*'));
159-
$this->assertEquals(1, $boolARQuery->where(['bool_col' => false])->count('*'));
160-
$this->assertEquals(2, $boolARQuery->where(['bool_col' => [true, false]])->count('*'));
158+
$this->assertEquals(1, $boolARQuery->setWhere(['bool_col' => true])->count('*'));
159+
$this->assertEquals(1, $boolARQuery->setWhere(['bool_col' => false])->count('*'));
160+
$this->assertEquals(2, $boolARQuery->setWhere(['bool_col' => [true, false]])->count('*'));
161161

162-
$this->assertEquals(1, $boolARQuery->where('bool_col = :bool_col', ['bool_col' => true])->count('*'));
163-
$this->assertEquals(1, $boolARQuery->where('bool_col = :bool_col', ['bool_col' => false])->count('*'));
162+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = :bool_col', ['bool_col' => true])->count('*'));
163+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = :bool_col', ['bool_col' => false])->count('*'));
164164
}
165165

166166
/**
@@ -201,8 +201,8 @@ public function testBooleanValues2(): void
201201

202202
$userQuery = new ActiveQuery(UserAR::class);
203203
$this->assertCount(1, $userQuery->where(['is_deleted' => false])->all());
204-
$this->assertCount(0, $userQuery->where(['is_deleted' => true])->all());
205-
$this->assertCount(1, $userQuery->where(['is_deleted' => [true, false]])->all());
204+
$this->assertCount(0, $userQuery->setWhere(['is_deleted' => true])->all());
205+
$this->assertCount(1, $userQuery->setWhere(['is_deleted' => [true, false]])->all());
206206
}
207207

208208
public function testBooleanDefaultValues(): void

tests/Driver/Pgsql/MagicActiveRecordTest.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testBooleanProperty(): void
8787
$customers = $customerQuery->where(['bool_status' => true])->all();
8888
$this->assertCount(3, $customers);
8989

90-
$customers = $customerQuery->where(['bool_status' => false])->all();
90+
$customers = $customerQuery->setWhere(['bool_status' => false])->all();
9191
$this->assertCount(1, $customers);
9292
}
9393

@@ -100,18 +100,18 @@ public function testBooleanValues(): void
100100
$boolARQuery = new ActiveQuery(BoolAR::class);
101101

102102
$this->assertTrue($boolARQuery->where(['bool_col' => true])->one()->bool_col);
103-
$this->assertFalse($boolARQuery->where(['bool_col' => false])->one()->bool_col);
103+
$this->assertFalse($boolARQuery->setWhere(['bool_col' => false])->one()->bool_col);
104104

105-
$this->assertEquals(1, $boolARQuery->where('bool_col = TRUE')->count('*'));
106-
$this->assertEquals(1, $boolARQuery->where('bool_col = FALSE')->count('*'));
107-
$this->assertEquals(2, $boolARQuery->where('bool_col IN (TRUE, FALSE)')->count('*'));
105+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = TRUE')->count('*'));
106+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = FALSE')->count('*'));
107+
$this->assertEquals(2, $boolARQuery->setWhere('bool_col IN (TRUE, FALSE)')->count('*'));
108108

109-
$this->assertEquals(1, $boolARQuery->where(['bool_col' => true])->count('*'));
110-
$this->assertEquals(1, $boolARQuery->where(['bool_col' => false])->count('*'));
111-
$this->assertEquals(2, $boolARQuery->where(['bool_col' => [true, false]])->count('*'));
109+
$this->assertEquals(1, $boolARQuery->setWhere(['bool_col' => true])->count('*'));
110+
$this->assertEquals(1, $boolARQuery->setWhere(['bool_col' => false])->count('*'));
111+
$this->assertEquals(2, $boolARQuery->setWhere(['bool_col' => [true, false]])->count('*'));
112112

113-
$this->assertEquals(1, $boolARQuery->where('bool_col = :bool_col', ['bool_col' => true])->count('*'));
114-
$this->assertEquals(1, $boolARQuery->where('bool_col = :bool_col', ['bool_col' => false])->count('*'));
113+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = :bool_col', ['bool_col' => true])->count('*'));
114+
$this->assertEquals(1, $boolARQuery->setWhere('bool_col = :bool_col', ['bool_col' => false])->count('*'));
115115
}
116116

117117
/**
@@ -152,8 +152,8 @@ public function testBooleanValues2(): void
152152

153153
$userQuery = new ActiveQuery(UserAR::class);
154154
$this->assertCount(1, $userQuery->where(['is_deleted' => false])->all());
155-
$this->assertCount(0, $userQuery->where(['is_deleted' => true])->all());
156-
$this->assertCount(1, $userQuery->where(['is_deleted' => [true, false]])->all());
155+
$this->assertCount(0, $userQuery->setWhere(['is_deleted' => true])->all());
156+
$this->assertCount(1, $userQuery->setWhere(['is_deleted' => [true, false]])->all());
157157
}
158158

159159
public function testBooleanDefaultValues(): void

tests/MagicActiveRecordTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function testPopulateRecordCallWhenQueryingOnParentClass(): void
232232
$animals = $animal->where(['type' => Dog::class])->one();
233233
$this->assertEquals('bark', $animals->getDoes());
234234

235-
$animals = $animal->where(['type' => Cat::class])->one();
235+
$animals = $animal->setWhere(['type' => Cat::class])->one();
236236
$this->assertEquals('meow', $animals->getDoes());
237237
}
238238

0 commit comments

Comments
 (0)