Skip to content

Commit aadcc4e

Browse files
committed
Update tests. Fix overwrite where condition.
1 parent 7c6656d commit aadcc4e

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

src/ActiveQuery.php

Lines changed: 2 additions & 2 deletions
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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public function testFindExists(): void
4747

4848
$customerQuery = new ActiveQuery(Customer::class);
4949

50-
$this->assertTrue($customerQuery->where(['[[id]]' => 2])->exists());
51-
$this->assertTrue($customerQuery->where(['[[id]]' => 2])->select('[[name]]')->exists());
50+
$this->assertTrue((clone $customerQuery)->where(['[[id]]' => 2])->exists());
51+
$this->assertTrue((clone $customerQuery)->where(['[[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((clone $customerQuery)->where(['[[id]]' => 42])->exists());
54+
$this->assertFalse((clone $customerQuery)->where(['[[id]]' => 42])->select('[[name]]')->exists());
5555
}
5656

5757
public function testFindColumn(): void
@@ -191,12 +191,12 @@ public function testFindOneByColumnName(): void
191191
$customer = new ActiveQuery(Customer::class);
192192
$customerQuery = new CustomerQuery(Customer::class);
193193

194-
$arClass = $customer->findOne(['id' => 1]);
194+
$arClass = (clone $customer)->findOne(['id' => 1]);
195195
$this->assertEquals(1, $arClass->getId());
196196

197197
$customerQuery->joinWithProfile = true;
198198

199-
$arClass = $customer->findOne(['customer.id' => 1]);
199+
$arClass = (clone $customer)->findOne(['customer.id' => 1]);
200200
$this->assertEquals(1, $arClass->getId());
201201

202202
$customerQuery->joinWithProfile = false;
@@ -223,33 +223,33 @@ public function testFind(): void
223223

224224
/** find by a single primary key */
225225
$customerQuery = new ActiveQuery(Customer::class);
226-
$customer = $customerQuery->findOne(2);
226+
$customer = (clone $customerQuery)->findOne(2);
227227
$this->assertInstanceOf(Customer::class, $customer);
228228
$this->assertEquals('user2', $customer->getName());
229229

230-
$customer = $customerQuery->findOne(5);
230+
$customer = (clone $customerQuery)->findOne(5);
231231
$this->assertNull($customer);
232232

233233
$customerQuery = new ActiveQuery(Customer::class);
234-
$customer = $customerQuery->findOne(['id' => [5, 6, 1]]);
234+
$customer = (clone $customerQuery)->findOne(['id' => [5, 6, 1]]);
235235
$this->assertInstanceOf(Customer::class, $customer);
236236

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

240240
/** find by column values */
241241
$customerQuery = new ActiveQuery(Customer::class);
242-
$customer = $customerQuery->findOne(['id' => 2, 'name' => 'user2']);
242+
$customer = (clone $customerQuery)->findOne(['id' => 2, 'name' => 'user2']);
243243
$this->assertInstanceOf(Customer::class, $customer);
244244
$this->assertEquals('user2', $customer->getName());
245245

246-
$customer = $customerQuery->findOne(['id' => 2, 'name' => 'user1']);
246+
$customer = (clone $customerQuery)->findOne(['id' => 2, 'name' => 'user1']);
247247
$this->assertNull($customer);
248248

249-
$customer = $customerQuery->findOne(['id' => 5]);
249+
$customer = (clone $customerQuery)->findOne(['id' => 5]);
250250
$this->assertNull($customer);
251251

252-
$customer = $customerQuery->findOne(['name' => 'user5']);
252+
$customer = (clone $customerQuery)->findOne(['name' => 'user5']);
253253
$this->assertNull($customer);
254254

255255
/** find by column */
@@ -379,10 +379,10 @@ public function testFindCount(): void
379379

380380
$customerQuery = new ActiveQuery(Customer::class);
381381
$this->assertEquals(3, $customerQuery->count());
382-
$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());
382+
$this->assertEquals(1, (clone $customerQuery)->where(['id' => 1])->count());
383+
$this->assertEquals(2, (clone $customerQuery)->where(['id' => [1, 2]])->count());
384+
$this->assertEquals(2, (clone $customerQuery)->where(['id' => [1, 2]])->offset(1)->count());
385+
$this->assertEquals(2, (clone $customerQuery)->where(['id' => [1, 2]])->offset(2)->count());
386386

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

451451
$this->assertEquals(
452452
2,
453-
$customerQuery->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->count()
453+
(clone $customerQuery)->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->count()
454454
);
455455

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

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

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

471471
$this->assertEquals(
472472
1,
473-
$customerQuery->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->count()
473+
(clone $customerQuery)->where(['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+
(clone $customerQuery)->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->all()
479479
);
480480
}
481481

@@ -485,11 +485,11 @@ public function testFindNullValues(): void
485485

486486
$customerQuery = new ActiveQuery(Customer::class);
487487

488-
$customer = $customerQuery->findOne(2);
488+
$customer = (clone $customerQuery)->findOne(2);
489489
$customer->setName(null);
490490
$customer->save();
491491

492-
$result = $customerQuery->where(['name' => null])->all();
492+
$result = (clone $customerQuery)->where(['name' => null])->all();
493493
$this->assertCount(1, $result);
494494
$this->assertEquals(2, reset($result)->getPrimaryKey());
495495
}
@@ -649,16 +649,16 @@ public function testFindEmptyInCondition(): void
649649
$this->checkFixture($this->db(), 'customer');
650650

651651
$customerQuery = new ActiveQuery(Customer::class);
652-
$customers = $customerQuery->where(['id' => [1]])->all();
652+
$customers = (clone $customerQuery)->where(['id' => [1]])->all();
653653
$this->assertCount(1, $customers);
654654

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

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

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

@@ -667,15 +667,15 @@ public function testFindEagerIndexBy(): void
667667
$this->checkFixture($this->db(), 'order');
668668

669669
$orderQuery = new ActiveQuery(Order::class);
670-
$order = $orderQuery->with('itemsIndexed')->where(['id' => 1])->one();
670+
$order = (clone $orderQuery)->with('itemsIndexed')->where(['id' => 1])->one();
671671
$this->assertTrue($order->isRelationPopulated('itemsIndexed'));
672672

673673
$items = $order->getItemsIndexed();
674674
$this->assertCount(2, $items);
675675
$this->assertTrue(isset($items[1]));
676676
$this->assertTrue(isset($items[2]));
677677

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

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

tests/ActiveQueryTest.php

Lines changed: 0 additions & 1 deletion
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

0 commit comments

Comments
 (0)