diff --git a/tests/framework/BaseYiiTest.php b/tests/framework/BaseYiiTest.php index 9f61c1dd76..d045dff8bd 100644 --- a/tests/framework/BaseYiiTest.php +++ b/tests/framework/BaseYiiTest.php @@ -100,7 +100,7 @@ class BaseYiiTest extends TestCase BaseYii::setLogger(null); $defaultLogger = BaseYii::getLogger(); - $this->assertTrue($defaultLogger instanceof Logger); + $this->assertInstanceOf(Logger::className(), $defaultLogger); } /** diff --git a/tests/framework/ar/ActiveRecordTestTrait.php b/tests/framework/ar/ActiveRecordTestTrait.php index 898a5600ce..0eed00df3b 100644 --- a/tests/framework/ar/ActiveRecordTestTrait.php +++ b/tests/framework/ar/ActiveRecordTestTrait.php @@ -67,31 +67,32 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ // find one $result = $customerClass::find(); - $this->assertTrue($result instanceof ActiveQueryInterface); + $this->assertInstanceOf('\\yii\\db\\ActiveQueryInterface', $result); $customer = $result->one(); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); // find all $customers = $customerClass::find()->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers[0] instanceof $customerClass); - $this->assertTrue($customers[1] instanceof $customerClass); - $this->assertTrue($customers[2] instanceof $customerClass); + $this->assertCount(3, $customers); + $this->assertInstanceOf($customerClass, $customers[0]); + $this->assertInstanceOf($customerClass, $customers[1]); + $this->assertInstanceOf($customerClass, $customers[2]); // find by a single primary key $customer = $customerClass::findOne(2); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); $this->assertEquals('user2', $customer->name); $customer = $customerClass::findOne(5); $this->assertNull($customer); $customer = $customerClass::findOne(['id' => [5, 6, 1]]); + // can't use assertCount() here since it will count model attributes instead $this->assertEquals(1, count($customer)); $customer = $customerClass::find()->where(['id' => [5, 6, 1]])->one(); $this->assertNotNull($customer); // find by column values $customer = $customerClass::findOne(['id' => 2, 'name' => 'user2']); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); $this->assertEquals('user2', $customer->name); $customer = $customerClass::findOne(['id' => 2, 'name' => 'user1']); $this->assertNull($customer); @@ -102,11 +103,11 @@ trait ActiveRecordTestTrait // find by attributes $customer = $customerClass::find()->where(['name' => 'user2'])->one(); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); $this->assertEquals(2, $customer->id); // scope - $this->assertEquals(2, count($customerClass::find()->active()->all())); + $this->assertCount(2, $customerClass::find()->active()->all()); $this->assertEquals(2, $customerClass::find()->active()->count()); } @@ -128,7 +129,7 @@ trait ActiveRecordTestTrait // find all asArray $customers = $customerClass::find()->asArray()->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers[0]); $this->assertArrayHasKey('name', $customers[0]); $this->assertArrayHasKey('email', $customers[0]); @@ -200,19 +201,19 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ // indexBy $customers = $customerClass::find()->indexBy('name')->orderBy('id')->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers['user1'] instanceof $customerClass); - $this->assertTrue($customers['user2'] instanceof $customerClass); - $this->assertTrue($customers['user3'] instanceof $customerClass); + $this->assertCount(3, $customers); + $this->assertInstanceOf($customerClass, $customers['user1']); + $this->assertInstanceOf($customerClass, $customers['user2']); + $this->assertInstanceOf($customerClass, $customers['user3']); // indexBy callable $customers = $customerClass::find()->indexBy(function ($customer) { return $customer->id . '-' . $customer->name; })->orderBy('id')->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers['1-user1'] instanceof $customerClass); - $this->assertTrue($customers['2-user2'] instanceof $customerClass); - $this->assertTrue($customers['3-user3'] instanceof $customerClass); + $this->assertCount(3, $customers); + $this->assertInstanceOf($customerClass, $customers['1-user1']); + $this->assertInstanceOf($customerClass, $customers['2-user2']); + $this->assertInstanceOf($customerClass, $customers['3-user3']); } public function testFindIndexByAsArray() @@ -223,7 +224,7 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ // indexBy + asArray $customers = $customerClass::find()->asArray()->indexBy('name')->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers['user1']); $this->assertArrayHasKey('name', $customers['user1']); $this->assertArrayHasKey('email', $customers['user1']); @@ -244,7 +245,7 @@ trait ActiveRecordTestTrait $customers = $customerClass::find()->indexBy(function ($customer) { return $customer['id'] . '-' . $customer['name']; })->asArray()->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers['1-user1']); $this->assertArrayHasKey('name', $customers['1-user1']); $this->assertArrayHasKey('email', $customers['1-user1']); @@ -332,27 +333,27 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ // all() $customers = $customerClass::find()->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $customers = $customerClass::find()->orderBy('id')->limit(1)->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); $this->assertEquals('user1', $customers[0]->name); $customers = $customerClass::find()->orderBy('id')->limit(1)->offset(1)->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); $this->assertEquals('user2', $customers[0]->name); $customers = $customerClass::find()->orderBy('id')->limit(1)->offset(2)->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); $this->assertEquals('user3', $customers[0]->name); $customers = $customerClass::find()->orderBy('id')->limit(2)->offset(1)->all(); - $this->assertEquals(2, count($customers)); + $this->assertCount(2, $customers); $this->assertEquals('user2', $customers[0]->name); $this->assertEquals('user3', $customers[1]->name); $customers = $customerClass::find()->limit(2)->offset(3)->all(); - $this->assertEquals(0, count($customers)); + $this->assertCount(0, $customers); // one() $customer = $customerClass::find()->orderBy('id')->one(); @@ -379,13 +380,13 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ $this->assertEquals(2, $customerClass::find()->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->count()); - $this->assertEquals(2, count($customerClass::find()->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->all())); + $this->assertCount(2, $customerClass::find()->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->all()); $this->assertEquals(2, $customerClass::find()->where(['name' => ['user1', 'user2']])->count()); - $this->assertEquals(2, count($customerClass::find()->where(['name' => ['user1', 'user2']])->all())); + $this->assertCount(2, $customerClass::find()->where(['name' => ['user1', 'user2']])->all()); $this->assertEquals(1, $customerClass::find()->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->count()); - $this->assertEquals(1, count($customerClass::find()->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->all())); + $this->assertCount(1, $customerClass::find()->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->all()); } public function testFindNullValues() @@ -400,7 +401,7 @@ trait ActiveRecordTestTrait $this->afterSave(); $result = $customerClass::find()->where(['name' => null])->all(); - $this->assertEquals(1, count($result)); + $this->assertCount(1, $result); $this->assertEquals(2, reset($result)->primaryKey); } @@ -430,8 +431,8 @@ trait ActiveRecordTestTrait $this->assertFalse($customer->isRelationPopulated('orders')); $orders = $customer->orders; $this->assertTrue($customer->isRelationPopulated('orders')); - $this->assertEquals(2, count($orders)); - $this->assertEquals(1, count($customer->relatedRecords)); + $this->assertCount(2, $orders); + $this->assertCount(1, $customer->relatedRecords); // unset unset($customer['orders']); @@ -442,9 +443,9 @@ trait ActiveRecordTestTrait $this->assertFalse($customer->isRelationPopulated('orders')); $orders = $customer->getOrders()->where(['id' => 3])->all(); $this->assertFalse($customer->isRelationPopulated('orders')); - $this->assertEquals(0, count($customer->relatedRecords)); + $this->assertCount(0, $customer->relatedRecords); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertEquals(3, $orders[0]->id); } @@ -458,29 +459,29 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ $customers = $customerClass::find()->with('orders')->indexBy('id')->all(); ksort($customers); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertTrue($customers[1]->isRelationPopulated('orders')); $this->assertTrue($customers[2]->isRelationPopulated('orders')); $this->assertTrue($customers[3]->isRelationPopulated('orders')); - $this->assertEquals(1, count($customers[1]->orders)); - $this->assertEquals(2, count($customers[2]->orders)); - $this->assertEquals(0, count($customers[3]->orders)); + $this->assertCount(1, $customers[1]->orders); + $this->assertCount(2, $customers[2]->orders); + $this->assertCount(0, $customers[3]->orders); // unset unset($customers[1]->orders); $this->assertFalse($customers[1]->isRelationPopulated('orders')); $customer = $customerClass::find()->where(['id' => 1])->with('orders')->one(); $this->assertTrue($customer->isRelationPopulated('orders')); - $this->assertEquals(1, count($customer->orders)); - $this->assertEquals(1, count($customer->relatedRecords)); + $this->assertCount(1, $customer->orders); + $this->assertCount(1, $customer->relatedRecords); // multiple with() calls $orders = $orderClass::find()->with('customer', 'items')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('items')); $orders = $orderClass::find()->with('customer')->with('items')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('items')); } @@ -494,7 +495,7 @@ trait ActiveRecordTestTrait /* @var $order Order */ $order = $orderClass::findOne(1); $this->assertEquals(1, $order->id); - $this->assertEquals(2, count($order->items)); + $this->assertCount(2, $order->items); $this->assertEquals(1, $order->items[0]->id); $this->assertEquals(2, $order->items[1]->id); } @@ -518,11 +519,11 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ $orders = $orderClass::find()->with('items')->orderBy('id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $order = $orders[0]; $this->assertEquals(1, $order->id); $this->assertTrue($order->isRelationPopulated('items')); - $this->assertEquals(2, count($order->items)); + $this->assertCount(2, $order->items); $this->assertEquals(1, $order->items[0]->id); $this->assertEquals(2, $order->items[1]->id); } @@ -535,19 +536,19 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ $customers = $customerClass::find()->with('orders', 'orders.items')->indexBy('id')->all(); ksort($customers); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertTrue($customers[1]->isRelationPopulated('orders')); $this->assertTrue($customers[2]->isRelationPopulated('orders')); $this->assertTrue($customers[3]->isRelationPopulated('orders')); - $this->assertEquals(1, count($customers[1]->orders)); - $this->assertEquals(2, count($customers[2]->orders)); - $this->assertEquals(0, count($customers[3]->orders)); + $this->assertCount(1, $customers[1]->orders); + $this->assertCount(2, $customers[2]->orders); + $this->assertCount(0, $customers[3]->orders); $this->assertTrue($customers[1]->orders[0]->isRelationPopulated('items')); $this->assertTrue($customers[2]->orders[0]->isRelationPopulated('items')); $this->assertTrue($customers[2]->orders[1]->isRelationPopulated('items')); - $this->assertEquals(2, count($customers[1]->orders[0]->items)); - $this->assertEquals(3, count($customers[2]->orders[0]->items)); - $this->assertEquals(1, count($customers[2]->orders[1]->items)); + $this->assertCount(2, $customers[1]->orders[0]->items); + $this->assertCount(3, $customers[2]->orders[0]->items); + $this->assertCount(1, $customers[2]->orders[1]->items); $customers = $customerClass::find()->where(['id' => 1])->with('ordersWithItems')->one(); $this->assertTrue($customers->isRelationPopulated('ordersWithItems')); @@ -601,19 +602,19 @@ trait ActiveRecordTestTrait Item 3: 'Ice Age', 2 */ $orders = $orderClass::find()->with('itemsInOrder1')->orderBy('created_at')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $order = $orders[0]; $this->assertEquals(1, $order->id); $this->assertTrue($order->isRelationPopulated('itemsInOrder1')); - $this->assertEquals(2, count($order->itemsInOrder1)); + $this->assertCount(2, $order->itemsInOrder1); $this->assertEquals(1, $order->itemsInOrder1[0]->id); $this->assertEquals(2, $order->itemsInOrder1[1]->id); $order = $orders[1]; $this->assertEquals(2, $order->id); $this->assertTrue($order->isRelationPopulated('itemsInOrder1')); - $this->assertEquals(3, count($order->itemsInOrder1)); + $this->assertCount(3, $order->itemsInOrder1); $this->assertEquals(5, $order->itemsInOrder1[0]->id); $this->assertEquals(3, $order->itemsInOrder1[1]->id); $this->assertEquals(4, $order->itemsInOrder1[2]->id); @@ -621,7 +622,7 @@ trait ActiveRecordTestTrait $order = $orders[2]; $this->assertEquals(3, $order->id); $this->assertTrue($order->isRelationPopulated('itemsInOrder1')); - $this->assertEquals(1, count($order->itemsInOrder1)); + $this->assertCount(1, $order->itemsInOrder1); $this->assertEquals(2, $order->itemsInOrder1[0]->id); } @@ -632,19 +633,19 @@ trait ActiveRecordTestTrait $orderClass = $this->getOrderClass(); $orders = $orderClass::find()->with('itemsInOrder2')->orderBy('created_at')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $order = $orders[0]; $this->assertEquals(1, $order->id); $this->assertTrue($order->isRelationPopulated('itemsInOrder2')); - $this->assertEquals(2, count($order->itemsInOrder2)); + $this->assertCount(2, $order->itemsInOrder2); $this->assertEquals(1, $order->itemsInOrder2[0]->id); $this->assertEquals(2, $order->itemsInOrder2[1]->id); $order = $orders[1]; $this->assertEquals(2, $order->id); $this->assertTrue($order->isRelationPopulated('itemsInOrder2')); - $this->assertEquals(3, count($order->itemsInOrder2)); + $this->assertCount(3, $order->itemsInOrder2); $this->assertEquals(5, $order->itemsInOrder2[0]->id); $this->assertEquals(3, $order->itemsInOrder2[1]->id); $this->assertEquals(4, $order->itemsInOrder2[2]->id); @@ -652,7 +653,7 @@ trait ActiveRecordTestTrait $order = $orders[2]; $this->assertEquals(3, $order->id); $this->assertTrue($order->isRelationPopulated('itemsInOrder2')); - $this->assertEquals(1, count($order->itemsInOrder2)); + $this->assertCount(1, $order->itemsInOrder2); $this->assertEquals(2, $order->itemsInOrder2[0]->id); } @@ -668,7 +669,7 @@ trait ActiveRecordTestTrait $itemClass = $this->getItemClass(); /* @var $this TestCase|ActiveRecordTestTrait */ $customer = $customerClass::findOne(2); - $this->assertEquals(2, count($customer->orders)); + $this->assertCount(2, $customer->orders); // has many $order = new $orderClass; @@ -676,9 +677,9 @@ trait ActiveRecordTestTrait $this->assertTrue($order->isNewRecord); $customer->link('orders', $order); $this->afterSave(); - $this->assertEquals(3, count($customer->orders)); + $this->assertCount(3, $customer->orders); $this->assertFalse($order->isNewRecord); - $this->assertEquals(3, count($customer->getOrders()->all())); + $this->assertCount(3, $customer->getOrders()->all()); $this->assertEquals(2, $order->customer_id); // belongs to @@ -694,17 +695,17 @@ trait ActiveRecordTestTrait // via model $order = $orderClass::findOne(1); - $this->assertEquals(2, count($order->items)); - $this->assertEquals(2, count($order->orderItems)); + $this->assertCount(2, $order->items); + $this->assertCount(2, $order->orderItems); $orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]); $this->assertNull($orderItem); $item = $itemClass::findOne(3); $order->link('items', $item, ['quantity' => 10, 'subtotal' => 100]); $this->afterSave(); - $this->assertEquals(3, count($order->items)); - $this->assertEquals(3, count($order->orderItems)); + $this->assertCount(3, $order->items); + $this->assertCount(3, $order->orderItems); $orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]); - $this->assertTrue($orderItem instanceof $orderItemClass); + $this->assertInstanceOf($orderItemClass, $orderItem); $this->assertEquals(10, $orderItem->quantity); $this->assertEquals(100, $orderItem->subtotal); } @@ -725,10 +726,10 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ // has many without delete $customer = $customerClass::findOne(2); - $this->assertEquals(2, count($customer->ordersWithNullFK)); + $this->assertCount(2, $customer->ordersWithNullFK); $customer->unlink('ordersWithNullFK', $customer->ordersWithNullFK[1], false); - $this->assertEquals(1, count($customer->ordersWithNullFK)); + $this->assertCount(1, $customer->ordersWithNullFK); $orderWithNullFK = $orderWithNullFKClass::findOne(3); $this->assertEquals(3,$orderWithNullFK->id); @@ -736,30 +737,30 @@ trait ActiveRecordTestTrait // has many with delete $customer = $customerClass::findOne(2); - $this->assertEquals(2, count($customer->orders)); + $this->assertCount(2, $customer->orders); $customer->unlink('orders', $customer->orders[1], true); $this->afterSave(); - $this->assertEquals(1, count($customer->orders)); + $this->assertCount(1, $customer->orders); $this->assertNull($orderClass::findOne(3)); // via model with delete $order = $orderClass::findOne(2); - $this->assertEquals(3, count($order->items)); - $this->assertEquals(3, count($order->orderItems)); + $this->assertCount(3, $order->items); + $this->assertCount(3, $order->orderItems); $order->unlink('items', $order->items[2], true); $this->afterSave(); - $this->assertEquals(2, count($order->items)); - $this->assertEquals(2, count($order->orderItems)); + $this->assertCount(2, $order->items); + $this->assertCount(2, $order->orderItems); // via model without delete - $this->assertEquals(3, count($order->itemsWithNullFK)); + $this->assertCount(3, $order->itemsWithNullFK); $order->unlink('itemsWithNullFK', $order->itemsWithNullFK[2], false); $this->afterSave(); - $this->assertEquals(2, count($order->itemsWithNullFK)); - $this->assertEquals(2, count($order->orderItems)); + $this->assertCount(2, $order->itemsWithNullFK); + $this->assertCount(2, $order->orderItems); } public function testUnlinkAll() @@ -780,12 +781,12 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ // has many with delete $customer = $customerClass::findOne(2); - $this->assertEquals(2, count($customer->orders)); + $this->assertCount(2, $customer->orders); $this->assertEquals(3, $orderClass::find()->count()); $customer->unlinkAll('orders', true); $this->afterSave(); $this->assertEquals(1, $orderClass::find()->count()); - $this->assertEquals(0, count($customer->orders)); + $this->assertCount(0, $customer->orders); $this->assertNull($orderClass::findOne(2)); $this->assertNull($orderClass::findOne(3)); @@ -793,11 +794,11 @@ trait ActiveRecordTestTrait // has many without delete $customer = $customerClass::findOne(2); - $this->assertEquals(2, count($customer->ordersWithNullFK)); + $this->assertCount(2, $customer->ordersWithNullFK); $this->assertEquals(3, $orderWithNullFKClass::find()->count()); $customer->unlinkAll('ordersWithNullFK', false); $this->afterSave(); - $this->assertEquals(0, count($customer->ordersWithNullFK)); + $this->assertCount(0, $customer->ordersWithNullFK); $this->assertEquals(3, $orderWithNullFKClass::find()->count()); $this->assertEquals(2, $orderWithNullFKClass::find()->where(['AND', ['id' => [2, 3]], ['customer_id' => null]])->count()); @@ -805,22 +806,22 @@ trait ActiveRecordTestTrait // via model with delete /* @var $order Order */ $order = $orderClass::findOne(1); - $this->assertEquals(2, count($order->books)); + $this->assertCount(2, $order->books); $orderItemCount = $orderItemClass::find()->count(); $this->assertEquals(5, $itemClass::find()->count()); $order->unlinkAll('books', true); $this->afterSave(); $this->assertEquals(5, $itemClass::find()->count()); $this->assertEquals($orderItemCount - 2, $orderItemClass::find()->count()); - $this->assertEquals(0, count($order->books)); + $this->assertCount(0, $order->books); // via model without delete - $this->assertEquals(2, count($order->booksWithNullFK)); + $this->assertCount(2, $order->booksWithNullFK); $orderItemCount = $orderItemsWithNullFKClass::find()->count(); $this->assertEquals(5, $itemClass::find()->count()); $order->unlinkAll('booksWithNullFK',false); $this->afterSave(); - $this->assertEquals(0, count($order->booksWithNullFK)); + $this->assertCount(0, $order->booksWithNullFK); $this->assertEquals(2, $orderItemsWithNullFKClass::find()->where(['AND', ['item_id' => [1, 2]], ['order_id' => null]])->count()); $this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count()); $this->assertEquals(5, $itemClass::find()->count()); @@ -842,16 +843,16 @@ trait ActiveRecordTestTrait $this->afterSave(); $customer = $customerClass::findOne(1); - $this->assertEquals(3, count($customer->ordersWithNullFK)); - $this->assertEquals(1, count($customer->expensiveOrdersWithNullFK)); + $this->assertCount(3, $customer->ordersWithNullFK); + $this->assertCount(1, $customer->expensiveOrdersWithNullFK); $this->assertEquals(3, $orderClass::find()->count()); $customer->unlinkAll('expensiveOrdersWithNullFK'); - $this->assertEquals(3, count($customer->ordersWithNullFK)); - $this->assertEquals(0, count($customer->expensiveOrdersWithNullFK)); + $this->assertCount(3, $customer->ordersWithNullFK); + $this->assertCount(0, $customer->expensiveOrdersWithNullFK); $this->assertEquals(3, $orderClass::find()->count()); $customer = $customerClass::findOne(1); - $this->assertEquals(2, count($customer->ordersWithNullFK)); - $this->assertEquals(0, count($customer->expensiveOrdersWithNullFK)); + $this->assertCount(2, $customer->ordersWithNullFK); + $this->assertCount(0, $customer->expensiveOrdersWithNullFK); } public function testUnlinkAllAndConditionDelete() @@ -868,16 +869,16 @@ trait ActiveRecordTestTrait $this->afterSave(); $customer = $customerClass::findOne(1); - $this->assertEquals(3, count($customer->orders)); - $this->assertEquals(1, count($customer->expensiveOrders)); + $this->assertCount(3, $customer->orders); + $this->assertCount(1, $customer->expensiveOrders); $this->assertEquals(3, $orderClass::find()->count()); $customer->unlinkAll('expensiveOrders', true); - $this->assertEquals(3, count($customer->orders)); - $this->assertEquals(0, count($customer->expensiveOrders)); + $this->assertCount(3, $customer->orders); + $this->assertCount(0, $customer->expensiveOrders); $this->assertEquals(2, $orderClass::find()->count()); $customer = $customerClass::findOne(1); - $this->assertEquals(2, count($customer->orders)); - $this->assertEquals(0, count($customer->expensiveOrders)); + $this->assertCount(2, $customer->orders); + $this->assertCount(0, $customer->expensiveOrders); } public static $afterSaveNewRecord; @@ -934,7 +935,7 @@ trait ActiveRecordTestTrait // save /* @var $customer Customer */ $customer = $customerClass::findOne(2); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); $this->assertEquals('user2', $customer->name); $this->assertFalse($customer->isNewRecord); static::$afterSaveNewRecord = null; @@ -976,7 +977,7 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ /* @var $customer Customer */ $customer = $customerClass::findOne(2); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); $this->assertEquals('user2', $customer->name); $this->assertFalse($customer->isNewRecord); static::$afterSaveNewRecord = null; @@ -1042,7 +1043,7 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ // delete $customer = $customerClass::findOne(2); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); $this->assertEquals('user2', $customer->name); $customer->delete(); $this->afterSave(); @@ -1051,12 +1052,12 @@ trait ActiveRecordTestTrait // deleteAll $customers = $customerClass::find()->all(); - $this->assertEquals(2, count($customers)); + $this->assertCount(2, $customers); $ret = $customerClass::deleteAll(); $this->afterSave(); $this->assertEquals(2, $ret); $customers = $customerClass::find()->all(); - $this->assertEquals(0, count($customers)); + $this->assertCount(0, $customers); $ret = $customerClass::deleteAll(); $this->afterSave(); @@ -1088,10 +1089,10 @@ trait ActiveRecordTestTrait $this->assertEquals(0, $customer->status); $customers = $customerClass::find()->where(['status' => true])->all(); - $this->assertEquals(2, count($customers)); + $this->assertCount(2, $customers); $customers = $customerClass::find()->where(['status' => false])->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); } public function testAfterFind() @@ -1179,16 +1180,16 @@ trait ActiveRecordTestTrait /* @var $this TestCase|ActiveRecordTestTrait */ $customers = $customerClass::find()->where(['id' => [1]])->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); $customers = $customerClass::find()->where(['id' => []])->all(); - $this->assertEquals(0, count($customers)); + $this->assertCount(0, $customers); $customers = $customerClass::find()->where(['IN', 'id', [1]])->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); $customers = $customerClass::find()->where(['IN', 'id', []])->all(); - $this->assertEquals(0, count($customers)); + $this->assertCount(0, $customers); } public function testFindEagerIndexBy() @@ -1202,7 +1203,7 @@ trait ActiveRecordTestTrait $order = $orderClass::find()->with('itemsIndexed')->where(['id' => 1])->one(); $this->assertTrue($order->isRelationPopulated('itemsIndexed')); $items = $order->itemsIndexed; - $this->assertEquals(2, count($items)); + $this->assertCount(2, $items); $this->assertTrue(isset($items[1])); $this->assertTrue(isset($items[2])); @@ -1210,7 +1211,7 @@ trait ActiveRecordTestTrait $order = $orderClass::find()->with('itemsIndexed')->where(['id' => 2])->one(); $this->assertTrue($order->isRelationPopulated('itemsIndexed')); $items = $order->itemsIndexed; - $this->assertEquals(3, count($items)); + $this->assertCount(3, $items); $this->assertTrue(isset($items[3])); $this->assertTrue(isset($items[4])); $this->assertTrue(isset($items[5])); @@ -1239,7 +1240,7 @@ trait ActiveRecordTestTrait /* @var $customer ActiveRecord */ $customer = new $customerClass(); - $this->assertTrue($customer instanceof $customerClass); + $this->assertInstanceOf($customerClass, $customer); $this->assertTrue($customer->canGetProperty('id')); $this->assertTrue($customer->canSetProperty('id')); diff --git a/tests/framework/base/ComponentTest.php b/tests/framework/base/ComponentTest.php index e95d387a3f..da04122607 100644 --- a/tests/framework/base/ComponentTest.php +++ b/tests/framework/base/ComponentTest.php @@ -95,7 +95,7 @@ class ComponentTest extends TestCase public function testGetProperty() { - $this->assertTrue('default' === $this->component->Text); + $this->assertSame('default', $this->component->Text); $this->setExpectedException('yii\base\UnknownPropertyException'); $value2 = $this->component->Caption; } @@ -112,15 +112,15 @@ class ComponentTest extends TestCase public function testIsset() { $this->assertTrue(isset($this->component->Text)); - $this->assertFalse(empty($this->component->Text)); + $this->assertNotEmpty($this->component->Text); $this->component->Text = ''; $this->assertTrue(isset($this->component->Text)); - $this->assertTrue(empty($this->component->Text)); + $this->assertEmpty($this->component->Text); $this->component->Text = null; $this->assertFalse(isset($this->component->Text)); - $this->assertTrue(empty($this->component->Text)); + $this->assertEmpty($this->component->Text); $this->assertFalse(isset($this->component->p2)); $this->component->attachBehavior('a', new NewBehavior()); @@ -138,7 +138,7 @@ class ComponentTest extends TestCase { unset($this->component->Text); $this->assertFalse(isset($this->component->Text)); - $this->assertTrue(empty($this->component->Text)); + $this->assertEmpty($this->component->Text); $this->component->attachBehavior('a', new NewBehavior()); $this->component->setP2('test'); diff --git a/tests/framework/base/ObjectTest.php b/tests/framework/base/ObjectTest.php index 06968f0952..401d39d7df 100644 --- a/tests/framework/base/ObjectTest.php +++ b/tests/framework/base/ObjectTest.php @@ -60,7 +60,7 @@ class ObjectTest extends TestCase public function testGetProperty() { - $this->assertTrue('default' === $this->object->Text); + $this->assertSame('default', $this->object->Text); $this->setExpectedException('yii\base\UnknownPropertyException'); $value2 = $this->object->Caption; } @@ -83,15 +83,15 @@ class ObjectTest extends TestCase public function testIsset() { $this->assertTrue(isset($this->object->Text)); - $this->assertFalse(empty($this->object->Text)); + $this->assertNotEmpty($this->object->Text); $this->object->Text = ''; $this->assertTrue(isset($this->object->Text)); - $this->assertTrue(empty($this->object->Text)); + $this->assertEmpty($this->object->Text); $this->object->Text = null; $this->assertFalse(isset($this->object->Text)); - $this->assertTrue(empty($this->object->Text)); + $this->assertEmpty($this->object->Text); $this->assertFalse(isset($this->object->unknownProperty)); $this->assertTrue(empty($this->object->unknownProperty)); @@ -101,7 +101,7 @@ class ObjectTest extends TestCase { unset($this->object->Text); $this->assertFalse(isset($this->object->Text)); - $this->assertTrue(empty($this->object->Text)); + $this->assertEmpty($this->object->Text); } public function testUnsetReadOnlyProperty() @@ -128,7 +128,7 @@ class ObjectTest extends TestCase public function testObjectProperty() { - $this->assertTrue($this->object->object instanceof NewObject); + $this->assertInstanceOf(NewObject::className(), $this->object->object); $this->assertEquals('object text', $this->object->object->text); $this->object->object->text = 'new text'; $this->assertEquals('new text', $this->object->object->text); diff --git a/tests/framework/base/SecurityTest.php b/tests/framework/base/SecurityTest.php index b7150e2d14..e9026d2cfc 100644 --- a/tests/framework/base/SecurityTest.php +++ b/tests/framework/base/SecurityTest.php @@ -96,7 +96,7 @@ class SecurityTest extends TestCase $data = 'known data'; $key = 'secret'; $hashedData = $this->security->hashData($data, $key); - $this->assertFalse($data === $hashedData); + $this->assertNotSame($data, $hashedData); $this->assertEquals($data, $this->security->validateData($hashedData, $key)); $hashedData[strlen($hashedData) - 1] = 'A'; $this->assertFalse($this->security->validateData($hashedData, $key)); @@ -118,14 +118,14 @@ class SecurityTest extends TestCase $key = 'secret'; $encryptedData = $this->security->encryptByPassword($data, $key); - $this->assertFalse($data === $encryptedData); + $this->assertNotSame($data, $encryptedData); $decryptedData = $this->security->decryptByPassword($encryptedData, $key); $this->assertEquals($data, $decryptedData); $tampered = $encryptedData; $tampered[20] = ~$tampered[20]; $decryptedData = $this->security->decryptByPassword($tampered, $key); - $this->assertTrue(false === $decryptedData); + $this->assertFalse($decryptedData); } public function testEncryptByKey() @@ -134,7 +134,7 @@ class SecurityTest extends TestCase $key = $this->security->generateRandomKey(80); $encryptedData = $this->security->encryptByKey($data, $key); - $this->assertFalse($data === $encryptedData); + $this->assertNotSame($data, $encryptedData); $decryptedData = $this->security->decryptByKey($encryptedData, $key); $this->assertEquals($data, $decryptedData); @@ -145,10 +145,10 @@ class SecurityTest extends TestCase $tampered = $encryptedData; $tampered[20] = ~$tampered[20]; $decryptedData = $this->security->decryptByKey($tampered, $key); - $this->assertTrue(false === $decryptedData); + $this->assertFalse($decryptedData); $decryptedData = $this->security->decryptByKey($encryptedData, $key, $key . "\0"); - $this->assertTrue(false === $decryptedData); + $this->assertFalse($decryptedData); } /** @@ -951,7 +951,7 @@ TEXT; $this->assertInternalType('string', $key2); $this->assertEquals($length, strlen($key2)); if ($length >= 7) { // avoid random test failure, short strings are likely to collide - $this->assertTrue($key1 != $key2); + $this->assertNotEquals($key1, $key2); } } @@ -963,7 +963,7 @@ TEXT; $key2 = $this->security->generateRandomKey($length); $this->assertInternalType('string', $key2); $this->assertEquals($length, strlen($key2)); - $this->assertTrue($key1 != $key2); + $this->assertNotEquals($key1, $key2); // force /dev/urandom reading loop to deal with chunked data // the above test may have read everything in one run. diff --git a/tests/framework/behaviors/AttributeTypecastBehaviorTest.php b/tests/framework/behaviors/AttributeTypecastBehaviorTest.php index 8b9764daa8..b73d50037c 100644 --- a/tests/framework/behaviors/AttributeTypecastBehaviorTest.php +++ b/tests/framework/behaviors/AttributeTypecastBehaviorTest.php @@ -68,7 +68,7 @@ class AttributeTypecastBehaviorTest extends TestCase $this->assertSame('123', $model->name); $this->assertSame(58, $model->amount); $this->assertSame(100.8, $model->price); - $this->assertSame(true, $model->isActive); + $this->assertTrue($model->isActive); $this->assertSame('callback: foo', $model->callback); } @@ -100,7 +100,7 @@ class AttributeTypecastBehaviorTest extends TestCase $this->assertSame('', $model->name); $this->assertSame(0, $model->amount); $this->assertSame(0.0, $model->price); - $this->assertSame(false, $model->isActive); + $this->assertFalse($model->isActive); $this->assertSame('callback: ', $model->callback); } diff --git a/tests/framework/console/controllers/AssetControllerTest.php b/tests/framework/console/controllers/AssetControllerTest.php index d3a38e09ce..7e5f2eb4c2 100644 --- a/tests/framework/console/controllers/AssetControllerTest.php +++ b/tests/framework/console/controllers/AssetControllerTest.php @@ -248,7 +248,7 @@ EOL; { $configFileName = $this->testFilePath . DIRECTORY_SEPARATOR . 'config.php'; $this->runAssetControllerAction('template', [$configFileName]); - $this->assertTrue(file_exists($configFileName), 'Unable to create config file template!'); + $this->assertFileExists($configFileName, 'Unable to create config file template!'); $config = require($configFileName); $this->assertTrue(is_array($config), 'Invalid config created!'); } @@ -294,7 +294,7 @@ EOL; $this->runAssetControllerAction('compress', [$configFile, $bundleFile]); // Then : - $this->assertTrue(file_exists($bundleFile), 'Unable to create output bundle file!'); + $this->assertFileExists($bundleFile, 'Unable to create output bundle file!'); $compressedBundleConfig = require($bundleFile); $this->assertTrue(is_array($compressedBundleConfig), 'Output bundle file has incorrect format!'); $this->assertCount(2, $compressedBundleConfig, 'Output bundle config contains wrong bundle count!'); @@ -306,9 +306,9 @@ EOL; $this->assertNotEmpty($compressedAssetBundleConfig['depends'], 'Compressed bundle dependency is invalid!'); $compressedCssFileName = $this->testAssetsBasePath . DIRECTORY_SEPARATOR . 'all.css'; - $this->assertTrue(file_exists($compressedCssFileName), 'Unable to compress CSS files!'); + $this->assertFileExists($compressedCssFileName, 'Unable to compress CSS files!'); $compressedJsFileName = $this->testAssetsBasePath . DIRECTORY_SEPARATOR . 'all.js'; - $this->assertTrue(file_exists($compressedJsFileName), 'Unable to compress JS files!'); + $this->assertFileExists($compressedJsFileName, 'Unable to compress JS files!'); $compressedCssFileContent = file_get_contents($compressedCssFileName); foreach ($cssFiles as $name => $content) { @@ -374,7 +374,7 @@ EOL; $this->runAssetControllerAction('compress', [$configFile, $bundleFile]); // Then : - $this->assertTrue(file_exists($bundleFile), 'Unable to create output bundle file!'); + $this->assertFileExists($bundleFile, 'Unable to create output bundle file!'); $compressedBundleConfig = require($bundleFile); $this->assertTrue(is_array($compressedBundleConfig), 'Output bundle file has incorrect format!'); $this->assertArrayHasKey($externalAssetBundleClassName, $compressedBundleConfig, 'External bundle is lost!'); diff --git a/tests/framework/console/controllers/BaseMessageControllerTest.php b/tests/framework/console/controllers/BaseMessageControllerTest.php index 3b4a2a1ba6..b22af5b883 100644 --- a/tests/framework/console/controllers/BaseMessageControllerTest.php +++ b/tests/framework/console/controllers/BaseMessageControllerTest.php @@ -140,7 +140,8 @@ abstract class BaseMessageControllerTest extends TestCase { $configFileName = $this->configFileName; $out = $this->runMessageControllerAction('config', [$configFileName]); - $this->assertTrue(file_exists($configFileName), "Unable to create config file from template. Command output:\n\n" . $out); + $this->assertFileExists($configFileName, + "Unable to create config file from template. Command output:\n\n" . $out); } public function testConfigFileNotExist() @@ -177,7 +178,8 @@ abstract class BaseMessageControllerTest extends TestCase $out = $this->runMessageControllerAction('extract', [$this->configFileName]); $out .= $this->runMessageControllerAction('extract', [$this->configFileName]); - $this->assertTrue(strpos($out, 'Nothing to save') !== false, "Controller should respond with \"Nothing to save\" if there's nothing to update. Command output:\n\n" . $out); + $this->assertNotFalse(strpos($out, 'Nothing to save'), + "Controller should respond with \"Nothing to save\" if there's nothing to update. Command output:\n\n" . $out); } /** @@ -280,8 +282,12 @@ abstract class BaseMessageControllerTest extends TestCase $out = $this->runMessageControllerAction('extract', [$this->configFileName]); $messages = $this->loadMessages($category); - $this->assertTrue($zeroMessageContent === $messages[$zeroMessage], "Message content \"0\" is lost. Command output:\n\n" . $out); - $this->assertTrue($falseMessageContent === $messages[$falseMessage], "Message content \"false\" is lost. Command output:\n\n" . $out); + $this->assertSame($zeroMessageContent, + $messages[$zeroMessage], + "Message content \"0\" is lost. Command output:\n\n" . $out); + $this->assertSame($falseMessageContent, + $messages[$falseMessage], + "Message content \"false\" is lost. Command output:\n\n" . $out); } /** diff --git a/tests/framework/data/ActiveDataProviderTest.php b/tests/framework/data/ActiveDataProviderTest.php index ae74ee54cb..024ef3937d 100644 --- a/tests/framework/data/ActiveDataProviderTest.php +++ b/tests/framework/data/ActiveDataProviderTest.php @@ -38,10 +38,10 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase 'query' => Order::find()->orderBy('id'), ]); $orders = $provider->getModels(); - $this->assertEquals(3, count($orders)); - $this->assertTrue($orders[0] instanceof Order); - $this->assertTrue($orders[1] instanceof Order); - $this->assertTrue($orders[2] instanceof Order); + $this->assertCount(3, $orders); + $this->assertInstanceOf(Order::className(), $orders[0]); + $this->assertInstanceOf(Order::className(), $orders[1]); + $this->assertInstanceOf(Order::className(), $orders[2]); $this->assertEquals([1, 2, 3], $provider->getKeys()); $provider = new ActiveDataProvider([ @@ -51,7 +51,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase ] ]); $orders = $provider->getModels(); - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); } public function testActiveRelation() @@ -62,9 +62,9 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase 'query' => $customer->getOrders(), ]); $orders = $provider->getModels(); - $this->assertEquals(2, count($orders)); - $this->assertTrue($orders[0] instanceof Order); - $this->assertTrue($orders[1] instanceof Order); + $this->assertCount(2, $orders); + $this->assertInstanceOf(Order::className(), $orders[0]); + $this->assertInstanceOf(Order::className(), $orders[1]); $this->assertEquals([2, 3], $provider->getKeys()); $provider = new ActiveDataProvider([ @@ -74,7 +74,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase ] ]); $orders = $provider->getModels(); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); } public function testActiveRelationVia() @@ -85,10 +85,10 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase 'query' => $order->getItems(), ]); $items = $provider->getModels(); - $this->assertEquals(3, count($items)); - $this->assertTrue($items[0] instanceof Item); - $this->assertTrue($items[1] instanceof Item); - $this->assertTrue($items[2] instanceof Item); + $this->assertCount(3, $items); + $this->assertInstanceOf(Item::className(), $items[0]); + $this->assertInstanceOf(item::className(), $items[1]); + $this->assertInstanceOf(Item::className(), $items[2]); $this->assertEquals([3, 4, 5], $provider->getKeys()); $provider = new ActiveDataProvider([ @@ -98,7 +98,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase ] ]); $items = $provider->getModels(); - $this->assertEquals(2, count($items)); + $this->assertCount(2, $items); } public function testActiveRelationViaTable() @@ -109,9 +109,9 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase 'query' => $order->getBooks(), ]); $items = $provider->getModels(); - $this->assertEquals(2, count($items)); - $this->assertTrue($items[0] instanceof Item); - $this->assertTrue($items[1] instanceof Item); + $this->assertCount(2, $items); + $this->assertInstanceOf(Item::className(), $items[0]); + $this->assertInstanceOf(Item::className(), $items[1]); $provider = new ActiveDataProvider([ 'query' => $order->getBooks(), @@ -120,7 +120,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase ] ]); $items = $provider->getModels(); - $this->assertEquals(1, count($items)); + $this->assertCount(1, $items); } public function testQuery() @@ -131,7 +131,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase 'query' => $query->from('order')->orderBy('id'), ]); $orders = $provider->getModels(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertTrue(is_array($orders[0])); $this->assertEquals([0, 1, 2], $provider->getKeys()); @@ -144,7 +144,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase ] ]); $orders = $provider->getModels(); - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); } public function testRefresh() @@ -154,12 +154,12 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase 'db' => $this->getConnection(), 'query' => $query->from('order')->orderBy('id'), ]); - $this->assertEquals(3, count($provider->getModels())); + $this->assertCount(3, $provider->getModels()); $provider->getPagination()->pageSize = 2; - $this->assertEquals(3, count($provider->getModels())); + $this->assertCount(3, $provider->getModels()); $provider->refresh(); - $this->assertEquals(2, count($provider->getModels())); + $this->assertCount(2, $provider->getModels()); } public function testPaginationBeforeModels() @@ -175,9 +175,9 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase $this->assertEquals(1, $pagination->getPageCount()); $provider->getPagination()->pageSize = 2; - $this->assertEquals(3, count($provider->getModels())); + $this->assertCount(3, $provider->getModels()); $provider->refresh(); - $this->assertEquals(2, count($provider->getModels())); + $this->assertCount(2, $provider->getModels()); } public function testDoesNotPerformQueryWhenHasNoModels() diff --git a/tests/framework/data/SortTest.php b/tests/framework/data/SortTest.php index 965abd56d3..d0fd5056ce 100644 --- a/tests/framework/data/SortTest.php +++ b/tests/framework/data/SortTest.php @@ -42,14 +42,14 @@ class SortTest extends TestCase ]); $orders = $sort->getOrders(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(SORT_ASC, $orders['age']); $this->assertEquals(SORT_DESC, $orders['first_name']); $this->assertEquals(SORT_DESC, $orders['last_name']); $sort->enableMultiSort = false; $orders = $sort->getOrders(true); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertEquals(SORT_ASC, $orders['age']); } @@ -73,13 +73,13 @@ class SortTest extends TestCase ]); $orders = $sort->getAttributeOrders(); - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertEquals(SORT_ASC, $orders['age']); $this->assertEquals(SORT_DESC, $orders['name']); $sort->enableMultiSort = false; $orders = $sort->getAttributeOrders(true); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertEquals(SORT_ASC, $orders['age']); } diff --git a/tests/framework/db/ActiveRecordTest.php b/tests/framework/db/ActiveRecordTest.php index 89b0b3056a..7350bca1ce 100644 --- a/tests/framework/db/ActiveRecordTest.php +++ b/tests/framework/db/ActiveRecordTest.php @@ -33,35 +33,57 @@ abstract class ActiveRecordTest extends DatabaseTestCase ActiveRecord::$db = $this->getConnection(); } + /** + * @inheritdoc + */ public function getCustomerClass() { return Customer::className(); } + /** + * @inheritdoc + */ public function getItemClass() { return Item::className(); } + /** + * @inheritdoc + */ public function getOrderClass() { return Order::className(); } + /** + * @inheritdoc + */ public function getOrderItemClass() { return OrderItem::className(); } + /** + * @return string + */ public function getCategoryClass() { return Category::className(); } + /** + * @inheritdoc + */ public function getOrderWithNullFKClass() { return OrderWithNullFK::className(); } + + /** + * @inheritdoc + */ public function getOrderItemWithNullFKmClass() { return OrderItemWithNullFK::className(); @@ -119,16 +141,16 @@ abstract class ActiveRecordTest extends DatabaseTestCase { // find one $customer = Customer::findBySql('SELECT * FROM {{customer}} ORDER BY [[id]] DESC')->one(); - $this->assertTrue($customer instanceof Customer); + $this->assertInstanceOf(Customer::className(), $customer); $this->assertEquals('user3', $customer->name); // find all $customers = Customer::findBySql('SELECT * FROM {{customer}}')->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); // find with parameter binding $customer = Customer::findBySql('SELECT * FROM {{customer}} WHERE [[id]]=:id', [':id' => 2])->one(); - $this->assertTrue($customer instanceof Customer); + $this->assertInstanceOf(Customer::className(), $customer); $this->assertEquals('user2', $customer->name); } @@ -150,13 +172,13 @@ abstract class ActiveRecordTest extends DatabaseTestCase /* @var $order Order */ $order = Order::findOne(1); $this->assertEquals(1, $order->id); - $this->assertEquals(2, count($order->books)); + $this->assertCount(2, $order->books); $this->assertEquals(1, $order->items[0]->id); $this->assertEquals(2, $order->items[1]->id); $order = Order::findOne(2); $this->assertEquals(2, $order->id); - $this->assertEquals(0, count($order->books)); + $this->assertCount(0, $order->books); $order = Order::find()->where(['id' => 1])->asArray()->one(); $this->assertTrue(is_array($order)); @@ -165,32 +187,32 @@ abstract class ActiveRecordTest extends DatabaseTestCase public function testFindEagerViaTable() { $orders = Order::find()->with('books')->orderBy('id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $order = $orders[0]; $this->assertEquals(1, $order->id); - $this->assertEquals(2, count($order->books)); + $this->assertCount(2, $order->books); $this->assertEquals(1, $order->books[0]->id); $this->assertEquals(2, $order->books[1]->id); $order = $orders[1]; $this->assertEquals(2, $order->id); - $this->assertEquals(0, count($order->books)); + $this->assertCount(0, $order->books); $order = $orders[2]; $this->assertEquals(3, $order->id); - $this->assertEquals(1, count($order->books)); + $this->assertCount(1, $order->books); $this->assertEquals(2, $order->books[0]->id); // https://github.com/yiisoft/yii2/issues/1402 $orders = Order::find()->with('books')->orderBy('id')->asArray()->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertTrue(is_array($orders[0]['orderItems'][0])); $order = $orders[0]; $this->assertTrue(is_array($order)); $this->assertEquals(1, $order['id']); - $this->assertEquals(2, count($order['books'])); + $this->assertCount(2, $order['books']); $this->assertEquals(1, $order['books'][0]['id']); $this->assertEquals(2, $order['books'][1]['id']); } @@ -204,7 +226,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $items = $customer->orderItems; - $this->assertEquals(2, count($items)); + $this->assertCount(2, $items); $this->assertInstanceOf(Item::className(), $items[0]); $this->assertInstanceOf(Item::className(), $items[1]); $this->assertEquals(1, $items[0]->id); @@ -223,7 +245,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $category = Category::findOne(1); $this->assertNotNull($category); $orders = $category->orders; - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertInstanceOf(Order::className(), $orders[0]); $this->assertInstanceOf(Order::className(), $orders[1]); $ids = [$orders[0]->id, $orders[1]->id]; @@ -233,7 +255,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $category = Category::findOne(2); $this->assertNotNull($category); $orders = $category->orders; - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertInstanceOf(Order::className(), $orders[0]); $this->assertEquals(2, $orders[0]->id); @@ -301,8 +323,8 @@ abstract class ActiveRecordTest extends DatabaseTestCase $this->assertTrue($record->refresh()); // https://github.com/yiisoft/yii2/commit/34945b0b69011bc7cab684c7f7095d837892a0d4#commitcomment-4458225 - $this->assertTrue($record->var1 === $record->var2); - $this->assertTrue($record->var2 === $record->var3); + $this->assertSame($record->var1, $record->var2); + $this->assertSame($record->var2, $record->var3); } public function testIsPrimaryKey() @@ -326,7 +348,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase { // left join and eager loading $orders = Order::find()->joinWith('customer')->orderBy('customer.id DESC, order.id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertEquals(1, $orders[2]->id); @@ -340,7 +362,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $query->where('{{customer}}.[[id]]=2'); }, ])->orderBy('order.id')->all(); - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertTrue($orders[0]->isRelationPopulated('customer')); @@ -352,7 +374,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $query->where(['customer.id' => 2]); }, ])->where(['order.id' => [1, 2]])->orderBy('order.id')->all(); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertTrue($orders[0]->isRelationPopulated('customer')); @@ -362,7 +384,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $query->where('{{customer}}.[[id]]=2'); }, ], false)->orderBy('order.id')->all(); - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertFalse($orders[0]->isRelationPopulated('customer')); @@ -374,19 +396,19 @@ abstract class ActiveRecordTest extends DatabaseTestCase $query->where(['customer.id' => 2]); }, ], false)->where(['order.id' => [1, 2]])->orderBy('order.id')->all(); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertFalse($orders[0]->isRelationPopulated('customer')); // join with via-relation $orders = Order::find()->innerJoinWith('books')->orderBy('order.id')->all(); - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertEquals(1, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertTrue($orders[0]->isRelationPopulated('books')); $this->assertTrue($orders[1]->isRelationPopulated('books')); - $this->assertEquals(2, count($orders[0]->books)); - $this->assertEquals(1, count($orders[1]->books)); + $this->assertCount(2, $orders[0]->books); + $this->assertCount(1, $orders[1]->books); // join with sub-relation $orders = Order::find()->innerJoinWith([ @@ -397,10 +419,10 @@ abstract class ActiveRecordTest extends DatabaseTestCase $q->where('{{category}}.[[id]] = 2'); }, ])->orderBy('order.id')->all(); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertEquals(2, $orders[0]->id); - $this->assertEquals(3, count($orders[0]->items)); + $this->assertCount(3, $orders[0]->items); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertEquals(2, $orders[0]->items[0]->category->id); @@ -410,7 +432,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $q->from('customer c'); } ])->orderBy('c.id DESC, order.id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertEquals(1, $orders[2]->id); @@ -420,7 +442,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase // join with table alias $orders = Order::find()->joinWith('customer as c')->orderBy('c.id DESC, order.id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertEquals(1, $orders[2]->id); @@ -437,53 +459,53 @@ abstract class ActiveRecordTest extends DatabaseTestCase $q->where('{{c}}.[[id]] = 2'); }, ])->orderBy('order.id')->all(); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertEquals(2, $orders[0]->id); - $this->assertEquals(3, count($orders[0]->items)); + $this->assertCount(3, $orders[0]->items); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertEquals(2, $orders[0]->items[0]->category->id); // join with ON condition $orders = Order::find()->joinWith('books2')->orderBy('order.id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(1, $orders[0]->id); $this->assertEquals(2, $orders[1]->id); $this->assertEquals(3, $orders[2]->id); $this->assertTrue($orders[0]->isRelationPopulated('books2')); $this->assertTrue($orders[1]->isRelationPopulated('books2')); $this->assertTrue($orders[2]->isRelationPopulated('books2')); - $this->assertEquals(2, count($orders[0]->books2)); - $this->assertEquals(0, count($orders[1]->books2)); - $this->assertEquals(1, count($orders[2]->books2)); + $this->assertCount(2, $orders[0]->books2); + $this->assertCount(0, $orders[1]->books2); + $this->assertCount(1, $orders[2]->books2); // lazy loading with ON condition $order = Order::findOne(1); - $this->assertEquals(2, count($order->books2)); + $this->assertCount(2, $order->books2); $order = Order::findOne(2); - $this->assertEquals(0, count($order->books2)); + $this->assertCount(0, $order->books2); $order = Order::findOne(3); - $this->assertEquals(1, count($order->books2)); + $this->assertCount(1, $order->books2); // eager loading with ON condition $orders = Order::find()->with('books2')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(1, $orders[0]->id); $this->assertEquals(2, $orders[1]->id); $this->assertEquals(3, $orders[2]->id); $this->assertTrue($orders[0]->isRelationPopulated('books2')); $this->assertTrue($orders[1]->isRelationPopulated('books2')); $this->assertTrue($orders[2]->isRelationPopulated('books2')); - $this->assertEquals(2, count($orders[0]->books2)); - $this->assertEquals(0, count($orders[1]->books2)); - $this->assertEquals(1, count($orders[2]->books2)); + $this->assertCount(2, $orders[0]->books2); + $this->assertCount(0, $orders[1]->books2); + $this->assertCount(1, $orders[2]->books2); // join with count and query $query = Order::find()->joinWith('customer'); $count = $query->count(); $this->assertEquals(3, $count); $orders = $query->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); // https://github.com/yiisoft/yii2/issues/2880 $query = Order::findOne(1); @@ -509,10 +531,10 @@ abstract class ActiveRecordTest extends DatabaseTestCase ]); }, ])->orderBy('order.id')->all(); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertEquals(2, $orders[0]->id); - $this->assertEquals(3, count($orders[0]->items)); + $this->assertCount(3, $orders[0]->items); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertEquals(2, $orders[0]->items[0]->category->id); } @@ -521,13 +543,13 @@ abstract class ActiveRecordTest extends DatabaseTestCase { // hasOne inner join $customers = Customer::find()->active()->innerJoinWith('profile')->orderBy('customer.id')->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); $this->assertEquals(1, $customers[0]->id); $this->assertTrue($customers[0]->isRelationPopulated('profile')); // hasOne outer join $customers = Customer::find()->active()->joinWith('profile')->orderBy('customer.id')->all(); - $this->assertEquals(2, count($customers)); + $this->assertCount(2, $customers); $this->assertEquals(1, $customers[0]->id); $this->assertEquals(2, $customers[1]->id); $this->assertTrue($customers[0]->isRelationPopulated('profile')); @@ -541,7 +563,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase $q->orderBy('order.id'); } ])->orderBy('customer.id DESC, order.id')->all(); - $this->assertEquals(2, count($customers)); + $this->assertCount(2, $customers); $this->assertEquals(2, $customers[0]->id); $this->assertEquals(1, $customers[1]->id); $this->assertTrue($customers[0]->isRelationPopulated('orders')); @@ -589,7 +611,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase } elseif ($aliasMethod === 'applyAlias') { $orders = $query->orderBy($query->applyAlias('customer', 'id') . ' DESC,' . $query->applyAlias('order', 'id'))->all(); } - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertEquals(1, $orders[2]->id); @@ -606,7 +628,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase } elseif ($aliasMethod === 'applyAlias') { $orders = $query->where([$query->applyAlias('customer', 'id') => 2])->orderBy($query->applyAlias('order', 'id'))->all(); } - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertTrue($orders[0]->isRelationPopulated('customer')); @@ -621,7 +643,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase } elseif ($aliasMethod === 'applyAlias') { $orders = $query->where([$query->applyAlias('customer', 'id') => 2])->orderBy($query->applyAlias('order', 'id'))->all(); } - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertEquals(2, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertFalse($orders[0]->isRelationPopulated('customer')); @@ -636,13 +658,13 @@ abstract class ActiveRecordTest extends DatabaseTestCase } elseif ($aliasMethod === 'applyAlias') { $orders = $query->where([$query->applyAlias('book', 'name') => 'Yii 1.1 Application Development Cookbook'])->orderBy($query->applyAlias('order', 'id'))->all(); } - $this->assertEquals(2, count($orders)); + $this->assertCount(2, $orders); $this->assertEquals(1, $orders[0]->id); $this->assertEquals(3, $orders[1]->id); $this->assertTrue($orders[0]->isRelationPopulated('books')); $this->assertTrue($orders[1]->isRelationPopulated('books')); - $this->assertEquals(2, count($orders[0]->books)); - $this->assertEquals(1, count($orders[1]->books)); + $this->assertCount(2, $orders[0]->books); + $this->assertCount(1, $orders[1]->books); // joining sub relations @@ -675,10 +697,10 @@ abstract class ActiveRecordTest extends DatabaseTestCase } elseif ($aliasMethod === 'applyAlias') { $orders = $query->orderBy($query->applyAlias('item', 'id'))->all(); } - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertEquals(2, $orders[0]->id); - $this->assertEquals(3, count($orders[0]->items)); + $this->assertCount(3, $orders[0]->items); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertEquals(2, $orders[0]->items[0]->category->id); @@ -686,32 +708,32 @@ abstract class ActiveRecordTest extends DatabaseTestCase if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { $relationName = 'books' . ucfirst($aliasMethod); $orders = Order::find()->joinWith(["$relationName b"])->orderBy('order.id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(1, $orders[0]->id); $this->assertEquals(2, $orders[1]->id); $this->assertEquals(3, $orders[2]->id); $this->assertTrue($orders[0]->isRelationPopulated($relationName)); $this->assertTrue($orders[1]->isRelationPopulated($relationName)); $this->assertTrue($orders[2]->isRelationPopulated($relationName)); - $this->assertEquals(2, count($orders[0]->$relationName)); - $this->assertEquals(0, count($orders[1]->$relationName)); - $this->assertEquals(1, count($orders[2]->$relationName)); + $this->assertCount(2, $orders[0]->$relationName); + $this->assertCount(0, $orders[1]->$relationName); + $this->assertCount(1, $orders[2]->$relationName); } // join with ON condition and alias in relation definition if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { $relationName = 'books' . ucfirst($aliasMethod) . 'A'; $orders = Order::find()->joinWith(["$relationName"])->orderBy('order.id')->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); $this->assertEquals(1, $orders[0]->id); $this->assertEquals(2, $orders[1]->id); $this->assertEquals(3, $orders[2]->id); $this->assertTrue($orders[0]->isRelationPopulated($relationName)); $this->assertTrue($orders[1]->isRelationPopulated($relationName)); $this->assertTrue($orders[2]->isRelationPopulated($relationName)); - $this->assertEquals(2, count($orders[0]->$relationName)); - $this->assertEquals(0, count($orders[1]->$relationName)); - $this->assertEquals(1, count($orders[2]->$relationName)); + $this->assertCount(2, $orders[0]->$relationName); + $this->assertCount(0, $orders[1]->$relationName); + $this->assertCount(1, $orders[2]->$relationName); } // join with count and query @@ -726,7 +748,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase } $this->assertEquals(3, $count); $orders = $query->all(); - $this->assertEquals(3, count($orders)); + $this->assertCount(3, $orders); // relational query /** @var $order Order */ @@ -757,10 +779,10 @@ abstract class ActiveRecordTest extends DatabaseTestCase } }, ])->orderBy('order.id')->all(); - $this->assertEquals(1, count($orders)); + $this->assertCount(1, $orders); $this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertEquals(2, $orders[0]->id); - $this->assertEquals(3, count($orders[0]->items)); + $this->assertCount(3, $orders[0]->items); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertEquals(2, $orders[0]->items[0]->category->id); @@ -776,7 +798,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase ->joinWith('movieItems', false) ->where(['movies.name' => 'Toy Story']); $orders = $query->all(); - $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); + $this->assertCount(1, $orders, $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(2, $orders[0]->id); $this->assertFalse($orders[0]->isRelationPopulated('bookItems')); $this->assertFalse($orders[0]->isRelationPopulated('movieItems')); @@ -786,12 +808,12 @@ abstract class ActiveRecordTest extends DatabaseTestCase ->joinWith('movieItems', true) ->where(['movies.name' => 'Toy Story']); $orders = $query->all(); - $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); + $this->assertCount(1, $orders, $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(2, $orders[0]->id); $this->assertTrue($orders[0]->isRelationPopulated('bookItems')); $this->assertTrue($orders[0]->isRelationPopulated('movieItems')); - $this->assertEquals(0, count($orders[0]->bookItems)); - $this->assertEquals(3, count($orders[0]->movieItems)); + $this->assertCount(0, $orders[0]->bookItems); + $this->assertCount(3, $orders[0]->movieItems); // join with the same table but different aliases // alias is defined in the call to joinWith() @@ -801,7 +823,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase ->joinWith(['itemsIndexed movies' => function($q) { $q->onCondition('movies.category_id = 2'); }], false) ->where(['movies.name' => 'Toy Story']); $orders = $query->all(); - $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); + $this->assertCount(1, $orders, $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(2, $orders[0]->id); $this->assertFalse($orders[0]->isRelationPopulated('itemsIndexed')); // with eager loading, only for one relation as it would be overwritten otherwise. @@ -810,20 +832,20 @@ abstract class ActiveRecordTest extends DatabaseTestCase ->joinWith(['itemsIndexed movies' => function($q) { $q->onCondition('movies.category_id = 2'); }], true) ->where(['movies.name' => 'Toy Story']); $orders = $query->all(); - $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); + $this->assertCount(1, $orders, $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(2, $orders[0]->id); $this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed')); - $this->assertEquals(3, count($orders[0]->itemsIndexed)); + $this->assertCount(3, $orders[0]->itemsIndexed); // with eager loading, and the other relation $query = Order::find() ->joinWith(['itemsIndexed books' => function($q) { $q->onCondition('books.category_id = 1'); }], true) ->joinWith(['itemsIndexed movies' => function($q) { $q->onCondition('movies.category_id = 2'); }], false) ->where(['movies.name' => 'Toy Story']); $orders = $query->all(); - $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); + $this->assertCount(1, $orders, $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(2, $orders[0]->id); $this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed')); - $this->assertEquals(0, count($orders[0]->itemsIndexed)); + $this->assertCount(0, $orders[0]->itemsIndexed); } /** @@ -919,55 +941,55 @@ abstract class ActiveRecordTest extends DatabaseTestCase { // eager loading: find one and all $customer = Customer::find()->with('orders2')->where(['id' => 1])->one(); - $this->assertTrue($customer->orders2[0]->customer2 === $customer); + $this->assertSame($customer->orders2[0]->customer2, $customer); $customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->all(); - $this->assertTrue($customers[0]->orders2[0]->customer2 === $customers[0]); - $this->assertTrue(empty($customers[1]->orders2)); + $this->assertSame($customers[0]->orders2[0]->customer2, $customers[0]); + $this->assertEmpty($customers[1]->orders2); // lazy loading $customer = Customer::findOne(2); $orders = $customer->orders2; - $this->assertTrue(count($orders) === 2); - $this->assertTrue($customer->orders2[0]->customer2 === $customer); - $this->assertTrue($customer->orders2[1]->customer2 === $customer); + $this->assertCount(2, $orders); + $this->assertSame($customer->orders2[0]->customer2, $customer); + $this->assertSame($customer->orders2[1]->customer2, $customer); // ad-hoc lazy loading $customer = Customer::findOne(2); $orders = $customer->getOrders2()->all(); - $this->assertTrue(count($orders) === 2); + $this->assertCount(2, $orders); $this->assertTrue($orders[0]->isRelationPopulated('customer2'), 'inverse relation did not populate the relation'); $this->assertTrue($orders[1]->isRelationPopulated('customer2'), 'inverse relation did not populate the relation'); - $this->assertTrue($orders[0]->customer2 === $customer); - $this->assertTrue($orders[1]->customer2 === $customer); + $this->assertSame($orders[0]->customer2, $customer); + $this->assertSame($orders[1]->customer2, $customer); // the other way around $customer = Customer::find()->with('orders2')->where(['id' => 1])->asArray()->one(); - $this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customer['id']); + $this->assertSame($customer['orders2'][0]['customer2']['id'], $customer['id']); $customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->asArray()->all(); - $this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customers[0]['id']); - $this->assertTrue(empty($customers[1]['orders2'])); + $this->assertSame($customer['orders2'][0]['customer2']['id'], $customers[0]['id']); + $this->assertEmpty($customers[1]['orders2']); $orders = Order::find()->with('customer2')->where(['id' => 1])->all(); - $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]); + $this->assertSame($orders[0]->customer2->orders2, [$orders[0]]); $order = Order::find()->with('customer2')->where(['id' => 1])->one(); - $this->assertTrue($order->customer2->orders2 === [$order]); + $this->assertSame($order->customer2->orders2, [$order]); $orders = Order::find()->with('customer2')->where(['id' => 1])->asArray()->all(); - $this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']); + $this->assertSame($orders[0]['customer2']['orders2'][0]['id'], $orders[0]['id']); $order = Order::find()->with('customer2')->where(['id' => 1])->asArray()->one(); - $this->assertTrue($order['customer2']['orders2'][0]['id'] === $orders[0]['id']); + $this->assertSame($order['customer2']['orders2'][0]['id'], $orders[0]['id']); $orders = Order::find()->with('customer2')->where(['id' => [1, 3]])->all(); - $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]); - $this->assertTrue($orders[1]->customer2->orders2 === [$orders[1]]); + $this->assertSame($orders[0]->customer2->orders2, [$orders[0]]); + $this->assertSame($orders[1]->customer2->orders2, [$orders[1]]); $orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->all(); - $this->assertTrue($orders[0]->customer2->orders2 === $orders); - $this->assertTrue($orders[1]->customer2->orders2 === $orders); + $this->assertSame($orders[0]->customer2->orders2, $orders); + $this->assertSame($orders[1]->customer2->orders2, $orders); $orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->asArray()->all(); - $this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']); - $this->assertTrue($orders[0]['customer2']['orders2'][1]['id'] === $orders[1]['id']); - $this->assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']); - $this->assertTrue($orders[1]['customer2']['orders2'][1]['id'] === $orders[1]['id']); + $this->assertSame($orders[0]['customer2']['orders2'][0]['id'], $orders[0]['id']); + $this->assertSame($orders[0]['customer2']['orders2'][1]['id'], $orders[1]['id']); + $this->assertSame($orders[1]['customer2']['orders2'][0]['id'], $orders[0]['id']); + $this->assertSame($orders[1]['customer2']['orders2'][1]['id'], $orders[1]['id']); } public function testInverseOfDynamic() @@ -1040,21 +1062,21 @@ abstract class ActiveRecordTest extends DatabaseTestCase // via table with delete /* @var $order Order */ $order = $orderClass::findOne(1); - $this->assertEquals(2, count($order->booksViaTable)); + $this->assertCount(2, $order->booksViaTable); $orderItemCount = $orderItemClass::find()->count(); $this->assertEquals(5, $itemClass::find()->count()); $order->unlinkAll('booksViaTable', true); $this->afterSave(); $this->assertEquals(5, $itemClass::find()->count()); $this->assertEquals($orderItemCount - 2, $orderItemClass::find()->count()); - $this->assertEquals(0, count($order->booksViaTable)); + $this->assertCount(0, $order->booksViaTable); // via table without delete - $this->assertEquals(2, count($order->booksWithNullFKViaTable)); + $this->assertCount(2, $order->booksWithNullFKViaTable); $orderItemCount = $orderItemsWithNullFKClass::find()->count(); $this->assertEquals(5, $itemClass::find()->count()); - $order->unlinkAll('booksWithNullFKViaTable', false); - $this->assertEquals(0, count($order->booksWithNullFKViaTable)); + $order->unlinkAll('booksWithNullFKViaTable', false); + $this->assertCount(0, $order->booksWithNullFKViaTable); $this->assertEquals(2,$orderItemsWithNullFKClass::find()->where(['AND', ['item_id' => [1, 2]], ['order_id' => null]])->count()); $this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count()); $this->assertEquals(5, $itemClass::find()->count()); @@ -1093,22 +1115,22 @@ abstract class ActiveRecordTest extends DatabaseTestCase { // https://github.com/yiisoft/yii2/issues/4938 $category = Category::findOne(2); - $this->assertTrue($category instanceof Category); + $this->assertInstanceOf(Category::className(), $category); $this->assertEquals(3, $category->getItems()->count()); $this->assertEquals(1, $category->getLimitedItems()->count()); $this->assertEquals(1, $category->getLimitedItems()->distinct(true)->count()); // https://github.com/yiisoft/yii2/issues/3197 $orders = Order::find()->with('orderItems')->orderBy('id')->all(); - $this->assertEquals(3, count($orders)); - $this->assertEquals(2, count($orders[0]->orderItems)); - $this->assertEquals(3, count($orders[1]->orderItems)); - $this->assertEquals(1, count($orders[2]->orderItems)); + $this->assertCount(3, $orders); + $this->assertCount(2, $orders[0]->orderItems); + $this->assertCount(3, $orders[1]->orderItems); + $this->assertCount(1, $orders[2]->orderItems); $orders = Order::find()->with(['orderItems' => function ($q) { $q->indexBy('item_id'); }])->orderBy('id')->all(); - $this->assertEquals(3, count($orders)); - $this->assertEquals(2, count($orders[0]->orderItems)); - $this->assertEquals(3, count($orders[1]->orderItems)); - $this->assertEquals(1, count($orders[2]->orderItems)); + $this->assertCount(3, $orders); + $this->assertCount(2, $orders[0]->orderItems); + $this->assertCount(3, $orders[1]->orderItems); + $this->assertCount(1, $orders[2]->orderItems); // https://github.com/yiisoft/yii2/issues/8149 $model = new Customer(); @@ -1312,12 +1334,12 @@ abstract class ActiveRecordTest extends DatabaseTestCase $row = Customer::find() ->emulateExecution() ->one(); - $this->assertSame(null, $row); + $this->assertNull($row); $exists = Customer::find() ->emulateExecution() ->exists(); - $this->assertSame(false, $exists); + $this->assertFalse($exists); $count = Customer::find() ->emulateExecution() @@ -1337,18 +1359,18 @@ abstract class ActiveRecordTest extends DatabaseTestCase $max = Customer::find() ->emulateExecution() ->max('id'); - $this->assertSame(null, $max); + $this->assertNull($max); $min = Customer::find() ->emulateExecution() ->min('id'); - $this->assertSame(null, $min); + $this->assertNull($min); $scalar = Customer::find() ->select(['id']) ->emulateExecution() ->scalar(); - $this->assertSame(null, $scalar); + $this->assertNull($scalar); $column = Customer::find() ->select(['id']) diff --git a/tests/framework/db/BatchQueryResultTest.php b/tests/framework/db/BatchQueryResultTest.php index 9a4117377a..5fab5ce3c7 100644 --- a/tests/framework/db/BatchQueryResultTest.php +++ b/tests/framework/db/BatchQueryResultTest.php @@ -24,9 +24,9 @@ abstract class BatchQueryResultTest extends DatabaseTestCase $query = new Query(); $query->from('customer')->orderBy('id'); $result = $query->batch(2, $db); - $this->assertTrue($result instanceof BatchQueryResult); + $this->assertInstanceOf(BatchQueryResult::className(), $result); $this->assertEquals(2, $result->batchSize); - $this->assertTrue($result->query === $query); + $this->assertSame($result->query, $query); // normal query $query = new Query(); @@ -36,7 +36,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase foreach ($batch as $rows) { $allRows = array_merge($allRows, $rows); } - $this->assertEquals(3, count($allRows)); + $this->assertCount(3, $allRows); $this->assertEquals('user1', $allRows[0]['name']); $this->assertEquals('user2', $allRows[1]['name']); $this->assertEquals('user3', $allRows[2]['name']); @@ -45,7 +45,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase foreach ($batch as $rows) { $allRows = array_merge($allRows, $rows); } - $this->assertEquals(3, count($allRows)); + $this->assertCount(3, $allRows); // reset $batch->reset(); @@ -57,7 +57,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase foreach ($batch as $rows) { $allRows = array_merge($allRows, $rows); } - $this->assertEquals(0, count($allRows)); + $this->assertCount(0, $allRows); // query with index $query = new Query(); @@ -66,7 +66,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase foreach ($query->batch(2, $db) as $rows) { $allRows = array_merge($allRows, $rows); } - $this->assertEquals(3, count($allRows)); + $this->assertCount(3, $allRows); $this->assertEquals('address1', $allRows['user1']['address']); $this->assertEquals('address2', $allRows['user2']['address']); $this->assertEquals('address3', $allRows['user3']['address']); @@ -78,7 +78,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase foreach ($query->each(100, $db) as $rows) { $allRows[] = $rows; } - $this->assertEquals(3, count($allRows)); + $this->assertCount(3, $allRows); $this->assertEquals('user1', $allRows[0]['name']); $this->assertEquals('user2', $allRows[1]['name']); $this->assertEquals('user3', $allRows[2]['name']); @@ -90,7 +90,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase foreach ($query->each(100, $db) as $key => $row) { $allRows[$key] = $row; } - $this->assertEquals(3, count($allRows)); + $this->assertCount(3, $allRows); $this->assertEquals('address1', $allRows['user1']['address']); $this->assertEquals('address2', $allRows['user2']['address']); $this->assertEquals('address3', $allRows['user3']['address']); @@ -105,7 +105,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase foreach ($query->batch(2, $db) as $models) { $customers = array_merge($customers, $models); } - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertEquals('user1', $customers[0]->name); $this->assertEquals('user2', $customers[1]->name); $this->assertEquals('user3', $customers[2]->name); @@ -119,9 +119,9 @@ abstract class BatchQueryResultTest extends DatabaseTestCase $this->assertTrue($model->isRelationPopulated('orders')); } } - $this->assertEquals(3, count($customers)); - $this->assertEquals(1, count($customers[0]->orders)); - $this->assertEquals(2, count($customers[1]->orders)); - $this->assertEquals(0, count($customers[2]->orders)); + $this->assertCount(3, $customers); + $this->assertCount(1, $customers[0]->orders); + $this->assertCount(2, $customers[1]->orders); + $this->assertCount(0, $customers[2]->orders); } } diff --git a/tests/framework/db/CommandTest.php b/tests/framework/db/CommandTest.php index fb107bd4a3..80804bd6fa 100644 --- a/tests/framework/db/CommandTest.php +++ b/tests/framework/db/CommandTest.php @@ -82,11 +82,11 @@ abstract class CommandTest extends DatabaseTestCase // query $sql = 'SELECT * FROM {{customer}}'; $reader = $db->createCommand($sql)->query(); - $this->assertTrue($reader instanceof DataReader); + $this->assertInstanceOf(DataReader::className(), $reader); // queryAll $rows = $db->createCommand('SELECT * FROM {{customer}}')->queryAll(); - $this->assertEquals(3, count($rows)); + $this->assertCount(3, $rows); $row = $rows[2]; $this->assertEquals(3, $row['id']); $this->assertEquals('user3', $row['name']); @@ -583,10 +583,10 @@ SQL; $db = $this->getConnection(); $rows = $db->createCommand('SELECT * FROM {{animal}}')->queryAll(); - $this->assertEquals(2, count($rows)); + $this->assertCount(2, $rows); $db->createCommand()->truncateTable('animal')->execute(); $rows = $db->createCommand('SELECT * FROM {{animal}}')->queryAll(); - $this->assertEquals(0, count($rows)); + $this->assertCount(0, $rows); } public function testRenameTable() diff --git a/tests/framework/db/ConnectionTest.php b/tests/framework/db/ConnectionTest.php index 5bb6444d6b..c31d6e3e4e 100644 --- a/tests/framework/db/ConnectionTest.php +++ b/tests/framework/db/ConnectionTest.php @@ -27,7 +27,7 @@ abstract class ConnectionTest extends DatabaseTestCase $connection->open(); $this->assertTrue($connection->isActive); - $this->assertTrue($connection->pdo instanceof \PDO); + $this->assertInstanceOf('\\PDO', $connection->pdo); $connection->close(); $this->assertFalse($connection->isActive); diff --git a/tests/framework/db/QueryBuilderTest.php b/tests/framework/db/QueryBuilderTest.php index 96308dcc6e..b34081c0fa 100644 --- a/tests/framework/db/QueryBuilderTest.php +++ b/tests/framework/db/QueryBuilderTest.php @@ -1230,25 +1230,25 @@ abstract class QueryBuilderTest extends DatabaseTestCase $qb = $this->getQueryBuilder(); $qb->db->createCommand()->addPrimaryKey($pkeyName, $tableName, ['id'])->execute(); $tableSchema = $qb->db->getSchema()->getTableSchema($tableName); - $this->assertEquals(1, count($tableSchema->primaryKey)); + $this->assertCount(1, $tableSchema->primaryKey); // DROP $qb->db->createCommand()->dropPrimaryKey($pkeyName, $tableName)->execute(); $qb = $this->getQueryBuilder(); // resets the schema $tableSchema = $qb->db->getSchema()->getTableSchema($tableName); - $this->assertEquals(0, count($tableSchema->primaryKey)); + $this->assertCount(0, $tableSchema->primaryKey); // ADD (2 columns) $qb = $this->getQueryBuilder(); $qb->db->createCommand()->addPrimaryKey($pkeyName, $tableName, 'id, field1')->execute(); $tableSchema = $qb->db->getSchema()->getTableSchema($tableName); - $this->assertEquals(2, count($tableSchema->primaryKey)); + $this->assertCount(2, $tableSchema->primaryKey); // DROP (2 columns) $qb->db->createCommand()->dropPrimaryKey($pkeyName, $tableName)->execute(); $qb = $this->getQueryBuilder(); // resets the schema $tableSchema = $qb->db->getSchema()->getTableSchema($tableName); - $this->assertEquals(0, count($tableSchema->primaryKey)); + $this->assertCount(0, $tableSchema->primaryKey); } public function existsParamsProvider() diff --git a/tests/framework/db/QueryTest.php b/tests/framework/db/QueryTest.php index 1adf680c6b..f230c86927 100644 --- a/tests/framework/db/QueryTest.php +++ b/tests/framework/db/QueryTest.php @@ -267,7 +267,7 @@ abstract class QueryTest extends DatabaseTestCase ); $result = $query->all($connection); $this->assertNotEmpty($result); - $this->assertSame(4, count($result)); + $this->assertCount(4, $result); } public function testOne() @@ -407,13 +407,13 @@ abstract class QueryTest extends DatabaseTestCase ->from('customer') ->emulateExecution() ->one($db); - $this->assertSame(false, $row); + $this->assertFalse($row); $exists = (new Query()) ->from('customer') ->emulateExecution() ->exists($db); - $this->assertSame(false, $exists); + $this->assertFalse($exists); $count = (new Query()) ->from('customer') @@ -437,20 +437,20 @@ abstract class QueryTest extends DatabaseTestCase ->from('customer') ->emulateExecution() ->max('id', $db); - $this->assertSame(null, $max); + $this->assertNull($max); $min = (new Query()) ->from('customer') ->emulateExecution() ->min('id', $db); - $this->assertSame(null, $min); + $this->assertNull($min); $scalar = (new Query()) ->select(['id']) ->from('customer') ->emulateExecution() ->scalar($db); - $this->assertSame(null, $scalar); + $this->assertNull($scalar); $column = (new Query()) ->select(['id']) diff --git a/tests/framework/db/SchemaTest.php b/tests/framework/db/SchemaTest.php index 4ee6cef0bd..f80d7aefdf 100644 --- a/tests/framework/db/SchemaTest.php +++ b/tests/framework/db/SchemaTest.php @@ -101,7 +101,7 @@ abstract class SchemaTest extends DatabaseTestCase $schema->refreshTableSchema('type'); $refreshedTable = $schema->getTableSchema('type', false); - $this->assertFalse($noCacheTable === $refreshedTable); + $this->assertNotSame($noCacheTable, $refreshedTable); } public function testCompositeFk() diff --git a/tests/framework/db/oci/ActiveRecordTest.php b/tests/framework/db/oci/ActiveRecordTest.php index 6cf047536d..34e34478e2 100644 --- a/tests/framework/db/oci/ActiveRecordTest.php +++ b/tests/framework/db/oci/ActiveRecordTest.php @@ -88,7 +88,7 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest // find all asArray $customers = $customerClass::find()->asArray()->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers[0]); $this->assertArrayHasKey('name', $customers[0]); $this->assertArrayHasKey('email', $customers[0]); diff --git a/tests/framework/db/oci/SchemaTest.php b/tests/framework/db/oci/SchemaTest.php index 267ec247e8..8198230e6a 100644 --- a/tests/framework/db/oci/SchemaTest.php +++ b/tests/framework/db/oci/SchemaTest.php @@ -91,6 +91,6 @@ class SchemaTest extends \yiiunit\framework\db\SchemaTest public function testAutoincrementDisabled() { $table = $this->getConnection(false)->schema->getTableSchema('order', true); - $this->assertSame(false, $table->columns['id']->autoIncrement); + $this->assertFalse($table->columns['id']->autoIncrement); } } diff --git a/tests/framework/db/pgsql/ActiveRecordTest.php b/tests/framework/db/pgsql/ActiveRecordTest.php index 22ecfeb12b..261e0effef 100644 --- a/tests/framework/db/pgsql/ActiveRecordTest.php +++ b/tests/framework/db/pgsql/ActiveRecordTest.php @@ -29,19 +29,19 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest $customer->save(false); $customer->refresh(); - $this->assertSame(false, $customer->bool_status); + $this->assertFalse($customer->bool_status); $customer->bool_status = true; $customer->save(false); $customer->refresh(); - $this->assertSame(true, $customer->bool_status); + $this->assertTrue($customer->bool_status); $customers = $customerClass::find()->where(['bool_status' => true])->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $customers = $customerClass::find()->where(['bool_status' => false])->all(); - $this->assertEquals(1, count($customers)); + $this->assertCount(1, $customers); } public function testFindAsArray() @@ -63,7 +63,7 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest // find all asArray $customers = $customerClass::find()->asArray()->all(); - $this->assertEquals(3, count($customers)); + $this->assertCount(3, $customers); $this->assertArrayHasKey('id', $customers[0]); $this->assertArrayHasKey('name', $customers[0]); $this->assertArrayHasKey('email', $customers[0]); @@ -106,8 +106,8 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest $this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db)); $this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db)); - $this->assertSame(true, BoolAR::find()->where(['bool_col' => true])->one($db)->bool_col); - $this->assertSame(false, BoolAR::find()->where(['bool_col' => false])->one($db)->bool_col); + $this->assertTrue(BoolAR::find()->where(['bool_col' => true])->one($db)->bool_col); + $this->assertFalse(BoolAR::find()->where(['bool_col' => false])->one($db)->bool_col); } /** @@ -141,9 +141,9 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest $user->email = 'test@example.com'; $user->save(false); - $this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => false])->all($db))); - $this->assertEquals(0, count(UserAR::find()->where(['is_deleted' => true])->all($db))); - $this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => [true, false]])->all($db))); + $this->assertCount(1, UserAR::find()->where(['is_deleted' => false])->all($db)); + $this->assertCount(0, UserAR::find()->where(['is_deleted' => true])->all($db)); + $this->assertCount(1, UserAR::find()->where(['is_deleted' => [true, false]])->all($db)); } public function testBooleanDefaultValues() @@ -154,8 +154,8 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest $this->assertNull($model->default_false); $model->loadDefaultValues(); $this->assertNull($model->bool_col); - $this->assertSame(true, $model->default_true); - $this->assertSame(false, $model->default_false); + $this->assertTrue($model->default_true); + $this->assertFalse($model->default_false); $this->assertTrue($model->save(false)); } diff --git a/tests/framework/db/pgsql/SchemaTest.php b/tests/framework/db/pgsql/SchemaTest.php index b7b4ff6c7f..f4451dd4e8 100644 --- a/tests/framework/db/pgsql/SchemaTest.php +++ b/tests/framework/db/pgsql/SchemaTest.php @@ -124,15 +124,15 @@ class SchemaTest extends \yiiunit\framework\db\SchemaTest $schema = $this->getConnection()->schema; $table = $schema->getTableSchema('bool_values'); - $this->assertSame(true, $table->getColumn('default_true')->defaultValue); - $this->assertSame(false, $table->getColumn('default_false')->defaultValue); + $this->assertTrue($table->getColumn('default_true')->defaultValue); + $this->assertFalse($table->getColumn('default_false')->defaultValue); } public function testFindSchemaNames() { $schema = $this->getConnection()->schema; - $this->assertEquals(3, count($schema->getSchemaNames())); + $this->assertCount(3, $schema->getSchemaNames()); } public function bigintValueProvider() diff --git a/tests/framework/db/sqlite/ConnectionTest.php b/tests/framework/db/sqlite/ConnectionTest.php index d91ef1b8d0..fbbc97a778 100644 --- a/tests/framework/db/sqlite/ConnectionTest.php +++ b/tests/framework/db/sqlite/ConnectionTest.php @@ -51,7 +51,7 @@ class ConnectionTest extends \yiiunit\framework\db\ConnectionTest $db = $this->prepareMasterSlave($masterCount, $slaveCount); - $this->assertTrue($db->getSlave() instanceof Connection); + $this->assertInstanceOf(Connection::className(), $db->getSlave()); $this->assertTrue($db->getSlave()->isActive); $this->assertFalse($db->isActive); @@ -63,7 +63,7 @@ class ConnectionTest extends \yiiunit\framework\db\ConnectionTest $db->createCommand("UPDATE profile SET description='test' WHERE id=1")->execute(); $this->assertTrue($db->isActive); if ($masterCount > 0) { - $this->assertTrue($db->getMaster() instanceof Connection); + $this->assertInstanceOf(Connection::className(), $db->getMaster()); $this->assertTrue($db->getMaster()->isActive); } else { $this->assertNull($db->getMaster()); @@ -79,7 +79,7 @@ class ConnectionTest extends \yiiunit\framework\db\ConnectionTest $this->assertFalse($db->isActive); $customer = Customer::findOne(1); - $this->assertTrue($customer instanceof Customer); + $this->assertInstanceOf(Customer::className(), $customer); $this->assertEquals('user1', $customer->name); $this->assertFalse($db->isActive); @@ -87,7 +87,7 @@ class ConnectionTest extends \yiiunit\framework\db\ConnectionTest $customer->save(); $this->assertTrue($db->isActive); $customer = Customer::findOne(1); - $this->assertTrue($customer instanceof Customer); + $this->assertInstanceOf(Customer::className(), $customer); $this->assertEquals('user1', $customer->name); $result = $db->useMaster(function () { return Customer::findOne(1)->name; @@ -114,8 +114,8 @@ class ConnectionTest extends \yiiunit\framework\db\ConnectionTest $hit_masters[$db->getMaster()->dsn] = true; } - $this->assertEquals($mastersCount, count($hit_masters), 'all masters hit'); - $this->assertEquals($slavesCount, count($hit_slaves), 'all slaves hit'); + $this->assertCount($mastersCount, $hit_masters, 'all masters hit'); + $this->assertCount($slavesCount, $hit_slaves, 'all slaves hit'); } public function testMastersSequential() @@ -136,9 +136,9 @@ class ConnectionTest extends \yiiunit\framework\db\ConnectionTest $hit_masters[$db->getMaster()->dsn] = true; } - $this->assertEquals(1, count($hit_masters), 'same master hit'); + $this->assertCount(1, $hit_masters, 'same master hit'); // slaves are always random - $this->assertEquals($slavesCount, count($hit_slaves), 'all slaves hit'); + $this->assertCount($slavesCount, $hit_slaves, 'all slaves hit'); } public function testRestoreMasterAfterException() diff --git a/tests/framework/db/sqlite/QueryTest.php b/tests/framework/db/sqlite/QueryTest.php index 17baad43b5..37cb55388c 100644 --- a/tests/framework/db/sqlite/QueryTest.php +++ b/tests/framework/db/sqlite/QueryTest.php @@ -24,6 +24,6 @@ class QueryTest extends \yiiunit\framework\db\QueryTest ); $result = $query->all($connection); $this->assertNotEmpty($result); - $this->assertSame(7, count($result)); + $this->assertCount(7, $result); } } diff --git a/tests/framework/di/ContainerTest.php b/tests/framework/di/ContainerTest.php index 920103c965..211d336c7a 100644 --- a/tests/framework/di/ContainerTest.php +++ b/tests/framework/di/ContainerTest.php @@ -40,11 +40,11 @@ class ContainerTest extends TestCase $container = new Container; $container->set($QuxInterface, $Qux); $foo = $container->get($Foo); - $this->assertTrue($foo instanceof $Foo); - $this->assertTrue($foo->bar instanceof $Bar); - $this->assertTrue($foo->bar->qux instanceof $Qux); + $this->assertInstanceOf($Foo, $foo); + $this->assertInstanceOf($Bar, $foo->bar); + $this->assertInstanceOf($Qux, $foo->bar->qux); $foo2 = $container->get($Foo); - $this->assertFalse($foo === $foo2); + $this->assertNotSame($foo, $foo2); // full wiring $container = new Container; @@ -53,9 +53,9 @@ class ContainerTest extends TestCase $container->set($Qux); $container->set($Foo); $foo = $container->get($Foo); - $this->assertTrue($foo instanceof $Foo); - $this->assertTrue($foo->bar instanceof $Bar); - $this->assertTrue($foo->bar->qux instanceof $Qux); + $this->assertInstanceOf($Foo, $foo); + $this->assertInstanceOf($Bar, $foo->bar); + $this->assertInstanceOf($Qux, $foo->bar->qux); // wiring by closure $container = new Container; @@ -65,9 +65,9 @@ class ContainerTest extends TestCase return new Foo($bar); }); $foo = $container->get('foo'); - $this->assertTrue($foo instanceof $Foo); - $this->assertTrue($foo->bar instanceof $Bar); - $this->assertTrue($foo->bar->qux instanceof $Qux); + $this->assertInstanceOf($Foo, $foo); + $this->assertInstanceOf($Bar, $foo->bar); + $this->assertInstanceOf($Qux, $foo->bar->qux); // wiring by closure which uses container $container = new Container; @@ -76,9 +76,9 @@ class ContainerTest extends TestCase return $c->get(Foo::className()); }); $foo = $container->get('foo'); - $this->assertTrue($foo instanceof $Foo); - $this->assertTrue($foo->bar instanceof $Bar); - $this->assertTrue($foo->bar->qux instanceof $Qux); + $this->assertInstanceOf($Foo, $foo); + $this->assertInstanceOf($Bar, $foo->bar); + $this->assertInstanceOf($Qux, $foo->bar->qux); // predefined constructor parameters $container = new Container; @@ -86,16 +86,16 @@ class ContainerTest extends TestCase $container->set('bar', $Bar, [Instance::of('qux')]); $container->set('qux', $Qux); $foo = $container->get('foo'); - $this->assertTrue($foo instanceof $Foo); - $this->assertTrue($foo->bar instanceof $Bar); - $this->assertTrue($foo->bar->qux instanceof $Qux); + $this->assertInstanceOf($Foo, $foo); + $this->assertInstanceOf($Bar, $foo->bar); + $this->assertInstanceOf($Qux, $foo->bar->qux); // wiring by closure $container = new Container; $container->set('qux', new Qux); $qux1 = $container->get('qux'); $qux2 = $container->get('qux'); - $this->assertTrue($qux1 === $qux2); + $this->assertSame($qux1, $qux2); // config $container = new Container; diff --git a/tests/framework/di/InstanceTest.php b/tests/framework/di/InstanceTest.php index 4a660a92df..3f91f48b11 100644 --- a/tests/framework/di/InstanceTest.php +++ b/tests/framework/di/InstanceTest.php @@ -27,10 +27,10 @@ class InstanceTest extends TestCase $className = Component::className(); $instance = Instance::of($className); - $this->assertTrue($instance instanceof Instance); - $this->assertTrue($instance->get($container) instanceof Component); - $this->assertTrue(Instance::ensure($instance, $className, $container) instanceof Component); - $this->assertTrue($instance->get($container) !== Instance::ensure($instance, $className, $container)); + $this->assertInstanceOf('\\yii\\di\\Instance', $instance); + $this->assertInstanceOf(Component::className(), $instance->get($container)); + $this->assertInstanceOf(Component::className(), Instance::ensure($instance, $className, $container)); + $this->assertNotSame($instance->get($container), Instance::ensure($instance, $className, $container)); } public function testEnsure() @@ -41,9 +41,9 @@ class InstanceTest extends TestCase 'dsn' => 'test', ]); - $this->assertTrue(Instance::ensure('db', 'yii\db\Connection', $container) instanceof Connection); - $this->assertTrue(Instance::ensure(new Connection, 'yii\db\Connection', $container) instanceof Connection); - $this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], 'yii\db\Connection', $container) instanceof Connection); + $this->assertInstanceOf(Connection::className(), Instance::ensure('db', 'yii\db\Connection', $container)); + $this->assertInstanceOf(Connection::className(), Instance::ensure(new Connection, 'yii\db\Connection', $container)); + $this->assertInstanceOf('\\yii\\db\\Connection', Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], 'yii\db\Connection', $container)); } /** @@ -74,9 +74,9 @@ class InstanceTest extends TestCase 'dsn' => 'test', ]); - $this->assertTrue(Instance::ensure('db', null, $container) instanceof Connection); - $this->assertTrue(Instance::ensure(new Connection, null, $container) instanceof Connection); - $this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], null, $container) instanceof Connection); + $this->assertInstanceOf(Connection::className(), Instance::ensure('db', null, $container)); + $this->assertInstanceOf(Connection::className(), Instance::ensure(new Connection, null, $container)); + $this->assertInstanceOf('\\yii\\db\\Connection', Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'], null, $container)); } public function testEnsure_MinimalSettings() @@ -86,10 +86,9 @@ class InstanceTest extends TestCase 'dsn' => 'test', ]); - $this->assertTrue(Instance::ensure('db') instanceof Connection); - $this->assertTrue(Instance::ensure(new Connection) instanceof Connection); - $this->assertTrue(Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test']) instanceof Connection); - + $this->assertInstanceOf(Connection::className(), Instance::ensure('db')); + $this->assertInstanceOf(Connection::className(), Instance::ensure(new Connection)); + $this->assertInstanceOf(Connection::className(), Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'])); Yii::$container = new Container; } @@ -132,7 +131,7 @@ class InstanceTest extends TestCase $container = Instance::of('db'); - $this->assertTrue($container->get() instanceof Connection); + $this->assertInstanceOf(Connection::className(), $container->get()); $this->destroyApplication(); } diff --git a/tests/framework/di/ServiceLocatorTest.php b/tests/framework/di/ServiceLocatorTest.php index 5e3dd5f1be..0e8411b066 100644 --- a/tests/framework/di/ServiceLocatorTest.php +++ b/tests/framework/di/ServiceLocatorTest.php @@ -44,7 +44,7 @@ class ServiceLocatorTest extends TestCase ]); }); $object = $container->get($className); - $this->assertTrue($object instanceof $className); + $this->assertInstanceOf($className, $object); $this->assertEquals(100, $object->prop1); $this->assertEquals(200, $object->prop2); @@ -53,7 +53,7 @@ class ServiceLocatorTest extends TestCase $className = TestClass::className(); $container->set($className, [__NAMESPACE__ . "\\Creator", 'create']); $object = $container->get($className); - $this->assertTrue($object instanceof $className); + $this->assertInstanceOf($className, $object); $this->assertEquals(1, $object->prop1); $this->assertNull($object->prop2); } @@ -64,7 +64,7 @@ class ServiceLocatorTest extends TestCase $className = TestClass::className(); $container = new ServiceLocator; $container->set($className, $object); - $this->assertTrue($container->get($className) === $object); + $this->assertSame($container->get($className), $object); } public function testShared() @@ -80,11 +80,11 @@ class ServiceLocatorTest extends TestCase $object = $container->get($className); $this->assertEquals(10, $object->prop1); $this->assertEquals(20, $object->prop2); - $this->assertTrue($object instanceof $className); + $this->assertInstanceOf($className, $object); // check shared $object2 = $container->get($className); - $this->assertTrue($object2 instanceof $className); - $this->assertTrue($object === $object2); + $this->assertInstanceOf($className, $object2); + $this->assertSame($object, $object2); } /** @@ -104,11 +104,11 @@ class ServiceLocatorTest extends TestCase $app = new ServiceLocator($config); $this->assertTrue(isset($app->captcha->name)); - $this->assertFalse(empty($app->captcha->name)); + $this->assertNotEmpty($app->captcha->name); $this->assertEquals('foo bar', $app->captcha->name); $this->assertTrue(isset($app->captcha->name)); - $this->assertFalse(empty($app->captcha->name)); + $this->assertNotEmpty($app->captcha->name); } } diff --git a/tests/framework/filters/HttpCacheTest.php b/tests/framework/filters/HttpCacheTest.php index 791e209681..deafe97868 100644 --- a/tests/framework/filters/HttpCacheTest.php +++ b/tests/framework/filters/HttpCacheTest.php @@ -37,7 +37,7 @@ class HttpCacheTest extends \yiiunit\TestCase $httpCache->beforeAction(null); $response = Yii::$app->getResponse(); $this->assertFalse($response->getHeaders()->offsetExists('Pragma')); - $this->assertFalse($response->getHeaders()->get('Pragma') === ''); + $this->assertNotSame($response->getHeaders()->get('Pragma'), ''); } /** diff --git a/tests/framework/helpers/FileHelperTest.php b/tests/framework/helpers/FileHelperTest.php index 90b3b6e87e..082d473ac8 100644 --- a/tests/framework/helpers/FileHelperTest.php +++ b/tests/framework/helpers/FileHelperTest.php @@ -163,7 +163,7 @@ class FileHelperTest extends TestCase foreach ($files as $name => $content) { $fileName = $dstDirName . DIRECTORY_SEPARATOR . $name; $this->assertFileExists($fileName); - $this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!'); + $this->assertStringEqualsFile($fileName, $content, 'Incorrect file content!'); } } @@ -200,7 +200,7 @@ class FileHelperTest extends TestCase } else { $fileName = $dstDirName . DIRECTORY_SEPARATOR . $name; $this->assertFileExists($fileName); - $this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!'); + $this->assertStringEqualsFile($fileName, $content, 'Incorrect file content!'); } } }; @@ -241,7 +241,7 @@ class FileHelperTest extends TestCase $this->assertFileNotExists($fileName); } else { $this->assertFileExists($fileName); - $this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!'); + $this->assertStringEqualsFile($fileName, $content, 'Incorrect file content!'); } } } @@ -780,7 +780,7 @@ class FileHelperTest extends TestCase foreach ($dataFiles as $name => $content) { $fileName = $dstDirName . DIRECTORY_SEPARATOR . $name; $this->assertFileExists($fileName); - $this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!'); + $this->assertStringEqualsFile($fileName, $content, 'Incorrect file content!'); } } } diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index 55e74ec00b..3377d13b35 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -900,7 +900,7 @@ EOD; $options = []; Html::removeCssStyle($options, ['color', 'background']); - $this->assertTrue(!array_key_exists('style', $options)); + $this->assertNotTrue(array_key_exists('style', $options)); $options = [ 'style' => [ 'color' => 'red', diff --git a/tests/framework/log/FileTargetTest.php b/tests/framework/log/FileTargetTest.php index ed7ba16e56..9e0e90e403 100644 --- a/tests/framework/log/FileTargetTest.php +++ b/tests/framework/log/FileTargetTest.php @@ -62,11 +62,11 @@ class FileTargetTest extends TestCase clearstatcache(); - $this->assertTrue(file_exists($logFile)); - $this->assertFalse(file_exists($logFile . '.1')); - $this->assertFalse(file_exists($logFile . '.2')); - $this->assertFalse(file_exists($logFile . '.3')); - $this->assertFalse(file_exists($logFile . '.4')); + $this->assertFileExists($logFile); + $this->assertFileNotExists($logFile . '.1'); + $this->assertFileNotExists($logFile . '.2'); + $this->assertFileNotExists($logFile . '.3'); + $this->assertFileNotExists($logFile . '.4'); // exceed max size for ($i = 0; $i < 1024; $i++) { @@ -81,11 +81,11 @@ class FileTargetTest extends TestCase clearstatcache(); - $this->assertTrue(file_exists($logFile)); - $this->assertTrue(file_exists($logFile . '.1')); - $this->assertFalse(file_exists($logFile . '.2')); - $this->assertFalse(file_exists($logFile . '.3')); - $this->assertFalse(file_exists($logFile . '.4')); + $this->assertFileExists($logFile); + $this->assertFileExists($logFile . '.1'); + $this->assertFileNotExists($logFile . '.2'); + $this->assertFileNotExists($logFile . '.3'); + $this->assertFileNotExists($logFile . '.4'); // second rotate @@ -96,10 +96,10 @@ class FileTargetTest extends TestCase clearstatcache(); - $this->assertTrue(file_exists($logFile)); - $this->assertTrue(file_exists($logFile . '.1')); - $this->assertFalse(file_exists($logFile . '.2')); - $this->assertFalse(file_exists($logFile . '.3')); - $this->assertFalse(file_exists($logFile . '.4')); + $this->assertFileExists($logFile); + $this->assertFileExists($logFile . '.1'); + $this->assertFileNotExists($logFile . '.2'); + $this->assertFileNotExists($logFile . '.3'); + $this->assertFileNotExists($logFile . '.4'); } } \ No newline at end of file diff --git a/tests/framework/log/LoggerTest.php b/tests/framework/log/LoggerTest.php index ed572c7197..b9441e7191 100644 --- a/tests/framework/log/LoggerTest.php +++ b/tests/framework/log/LoggerTest.php @@ -36,7 +36,7 @@ class LoggerTest extends TestCase { $memory = memory_get_usage(); $this->logger->log('test1', Logger::LEVEL_INFO); - $this->assertEquals(1, count($this->logger->messages)); + $this->assertCount(1, $this->logger->messages); $this->assertEquals('test1', $this->logger->messages[0][0]); $this->assertEquals(Logger::LEVEL_INFO, $this->logger->messages[0][1]); $this->assertEquals('application', $this->logger->messages[0][2]); @@ -44,7 +44,7 @@ class LoggerTest extends TestCase $this->assertGreaterThanOrEqual($memory, $this->logger->messages[0][5]); $this->logger->log('test2', Logger::LEVEL_ERROR, 'category'); - $this->assertEquals(2, count($this->logger->messages)); + $this->assertCount(2, $this->logger->messages); $this->assertEquals('test2', $this->logger->messages[1][0]); $this->assertEquals(Logger::LEVEL_ERROR, $this->logger->messages[1][1]); $this->assertEquals('category', $this->logger->messages[1][2]); @@ -60,7 +60,7 @@ class LoggerTest extends TestCase $memory = memory_get_usage(); $this->logger->traceLevel = 3; $this->logger->log('test3', Logger::LEVEL_INFO); - $this->assertEquals(1, count($this->logger->messages)); + $this->assertCount(1, $this->logger->messages); $this->assertEquals('test3', $this->logger->messages[0][0]); $this->assertEquals(Logger::LEVEL_INFO, $this->logger->messages[0][1]); $this->assertEquals('application', $this->logger->messages[0][2]); @@ -71,7 +71,7 @@ class LoggerTest extends TestCase 'class' => get_class($this->logger), 'type' => '->' ], $this->logger->messages[0][4][0]); - $this->assertEquals(3, count($this->logger->messages[0][4])); + $this->assertCount(3, $this->logger->messages[0][4]); $this->assertGreaterThanOrEqual($memory, $this->logger->messages[0][5]); } diff --git a/tests/framework/mail/BaseMailerTest.php b/tests/framework/mail/BaseMailerTest.php index b6d166f713..2e457bb65d 100644 --- a/tests/framework/mail/BaseMailerTest.php +++ b/tests/framework/mail/BaseMailerTest.php @@ -285,7 +285,7 @@ TEXT $this->assertTrue($mailer->send($message)); $file = Yii::getAlias($mailer->fileTransportPath) . '/message.txt'; $this->assertTrue(is_file($file)); - $this->assertEquals($message->toString(), file_get_contents($file)); + $this->assertStringEqualsFile($file, $message->toString()); } public function testBeforeSendEvent() diff --git a/tests/framework/rbac/ManagerTestCase.php b/tests/framework/rbac/ManagerTestCase.php index 2d773994fa..6bd5df7f88 100644 --- a/tests/framework/rbac/ManagerTestCase.php +++ b/tests/framework/rbac/ManagerTestCase.php @@ -25,7 +25,7 @@ abstract class ManagerTestCase extends TestCase public function testCreateRole() { $role = $this->auth->createRole('admin'); - $this->assertTrue($role instanceof Role); + $this->assertInstanceOf(Role::className(), $role); $this->assertEquals(Item::TYPE_ROLE, $role->type); $this->assertEquals('admin', $role->name); } @@ -33,7 +33,7 @@ abstract class ManagerTestCase extends TestCase public function testCreatePermission() { $permission = $this->auth->createPermission('edit post'); - $this->assertTrue($permission instanceof Permission); + $this->assertInstanceOf(Permission::className(), $permission); $this->assertEquals(Item::TYPE_PERMISSION, $permission->type); $this->assertEquals('edit post', $permission->name); } @@ -266,7 +266,7 @@ abstract class ManagerTestCase extends TestCase $expectedPermissions = ['createPost', 'updatePost', 'readPost', 'updateAnyPost']; $this->assertEquals(count($expectedPermissions), count($permissions)); foreach ($expectedPermissions as $permissionName) { - $this->assertTrue($permissions[$permissionName] instanceof Permission); + $this->assertInstanceOf(Permission::className(), $permissions[$permissionName]); } } @@ -277,7 +277,7 @@ abstract class ManagerTestCase extends TestCase $expectedPermissions = ['deletePost', 'createPost', 'updatePost', 'readPost']; $this->assertEquals(count($expectedPermissions), count($permissions)); foreach ($expectedPermissions as $permissionName) { - $this->assertTrue($permissions[$permissionName] instanceof Permission); + $this->assertInstanceOf(Permission::className(), $permissions[$permissionName]); } } @@ -289,15 +289,15 @@ abstract class ManagerTestCase extends TestCase $this->auth->assign($reader, 123); $roles = $this->auth->getRolesByUser('reader A'); - $this->assertTrue(reset($roles) instanceof Role); + $this->assertInstanceOf(Role::className(), reset($roles)); $this->assertEquals($roles['reader']->name, 'reader'); $roles = $this->auth->getRolesByUser(0); - $this->assertTrue(reset($roles) instanceof Role); + $this->assertInstanceOf(Role::className(), reset($roles)); $this->assertEquals($roles['reader']->name, 'reader'); $roles = $this->auth->getRolesByUser(123); - $this->assertTrue(reset($roles) instanceof Role); + $this->assertInstanceOf(Role::className(), reset($roles)); $this->assertEquals($roles['reader']->name, 'reader'); $this->assertContains('myDefaultRole', array_keys($roles)); @@ -310,12 +310,12 @@ abstract class ManagerTestCase extends TestCase $roles = $this->auth->getChildRoles('withoutChildren'); $this->assertCount(1, $roles); $this->assertInstanceOf(Role::className(), reset($roles)); - $this->assertTrue(reset($roles)->name === 'withoutChildren'); + $this->assertSame(reset($roles)->name, 'withoutChildren'); $roles = $this->auth->getChildRoles('reader'); $this->assertCount(1, $roles); $this->assertInstanceOf(Role::className(), reset($roles)); - $this->assertTrue(reset($roles)->name === 'reader'); + $this->assertSame(reset($roles)->name, 'reader'); $roles = $this->auth->getChildRoles('author'); $this->assertCount(2, $roles); @@ -362,9 +362,9 @@ abstract class ManagerTestCase extends TestCase $this->auth = $this->createManager(); - $this->assertEquals(0, count($this->auth->getAssignments(0))); - $this->assertEquals(1, count($this->auth->getAssignments(42))); - $this->assertEquals(2, count($this->auth->getAssignments(1337))); + $this->assertCount(0, $this->auth->getAssignments(0)); + $this->assertCount(1, $this->auth->getAssignments(42)); + $this->assertCount(2, $this->auth->getAssignments(1337)); } public function testGetAssignmentsByRole() @@ -508,6 +508,6 @@ abstract class ManagerTestCase extends TestCase /** @var ActionRule $rule */ $rule = $this->auth->getRule('action_rule'); - $this->assertTrue($rule instanceof ActionRule); + $this->assertInstanceOf(ActionRule::className(), $rule); } } diff --git a/tests/framework/requirements/YiiRequirementCheckerTest.php b/tests/framework/requirements/YiiRequirementCheckerTest.php index b99b44d0d0..7e74e7be24 100644 --- a/tests/framework/requirements/YiiRequirementCheckerTest.php +++ b/tests/framework/requirements/YiiRequirementCheckerTest.php @@ -42,7 +42,7 @@ class YiiRequirementCheckerTest extends TestCase $checkResult = $requirementsChecker->check($requirements)->getResult(); $summary = $checkResult['summary']; - $this->assertEquals(count($requirements), $summary['total'], 'Wrong summary total!'); + $this->assertCount($summary['total'], $requirements, 'Wrong summary total!'); $this->assertEquals(1, $summary['errors'], 'Wrong summary errors!'); $this->assertEquals(1, $summary['warnings'], 'Wrong summary warnings!'); @@ -121,7 +121,7 @@ class YiiRequirementCheckerTest extends TestCase $mergedRequirements = array_merge($requirements1, $requirements2); - $this->assertEquals(count($mergedRequirements), $checkResult['summary']['total'], 'Wrong total checks count!'); + $this->assertCount($checkResult['summary']['total'], $mergedRequirements, 'Wrong total checks count!'); foreach ($mergedRequirements as $key => $mergedRequirement) { $this->assertEquals($mergedRequirement['name'], $checkResult['requirements'][$key]['name'], 'Wrong requirements list!'); } diff --git a/tests/framework/test/ActiveFixtureTest.php b/tests/framework/test/ActiveFixtureTest.php index 9618cd9977..1947a985ea 100644 --- a/tests/framework/test/ActiveFixtureTest.php +++ b/tests/framework/test/ActiveFixtureTest.php @@ -64,7 +64,7 @@ abstract class ActiveFixtureTest extends DatabaseTestCase $test->setUp(); $fixture = $test->getFixture('customers'); $this->assertEquals(CustomerFixture::className(), get_class($fixture)); - $this->assertEquals(2, count($fixture)); + $this->assertCount(2, $fixture); $this->assertEquals(1, $fixture['customer1']['id']); $this->assertEquals('customer1@example.com', $fixture['customer1']['email']); $this->assertEquals(2, $fixture['customer2']['id']); diff --git a/tests/framework/validators/EachValidatorTest.php b/tests/framework/validators/EachValidatorTest.php index 8a12c5da53..743052794b 100644 --- a/tests/framework/validators/EachValidatorTest.php +++ b/tests/framework/validators/EachValidatorTest.php @@ -141,7 +141,7 @@ class EachValidatorTest extends TestCase $validator = new EachValidator(['rule' => ['compare', 'compareAttribute' => 'attr_two']]); $validator->validateAttribute($model, 'attr_one'); $this->assertNotEmpty($model->getErrors('attr_one')); - $this->assertEquals(3, count($model->attr_one)); + $this->assertCount(3, $model->attr_one); $model = FakedValidationModel::createWithAttributes([ 'attr_one' => [ diff --git a/tests/framework/validators/FileValidatorTest.php b/tests/framework/validators/FileValidatorTest.php index f6a406bf9d..8775bd5f39 100644 --- a/tests/framework/validators/FileValidatorTest.php +++ b/tests/framework/validators/FileValidatorTest.php @@ -137,7 +137,7 @@ class FileValidatorTest extends TestCase ]); $val->validateAttribute($m, 'attr_files'); $this->assertTrue($m->hasErrors()); - $this->assertTrue(stripos(current($m->getErrors('attr_files')), 'you can upload at most') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_files')), 'you can upload at most')); $val->maxFiles = 0; $m->clearErrors(); @@ -168,8 +168,8 @@ class FileValidatorTest extends TestCase ); $m->setScenario('validateMultipleFiles'); $this->assertFalse($m->validate()); - $this->assertTrue(stripos(current($m->getErrors('attr_images')), - 'Only files with these extensions are allowed') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_images')), + 'Only files with these extensions are allowed')); $m = FakedValidationModel::createWithAttributes( [ @@ -306,19 +306,19 @@ class FileValidatorTest extends TestCase $val = new FileValidator(['maxSize' => 128]); $val->validateAttribute($m, 'attr_files'); $this->assertTrue($m->hasErrors('attr_files')); - $this->assertTrue(stripos(current($m->getErrors('attr_files')), 'too big') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_files')), 'too big')); // to Small $m = $this->createModelForAttributeTest(); $val = new FileValidator(['minSize' => 2048]); $val->validateAttribute($m, 'attr_files'); $this->assertTrue($m->hasErrors('attr_files')); - $this->assertTrue(stripos(current($m->getErrors('attr_files')), 'too small') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_files')), 'too small')); // UPLOAD_ERR_INI_SIZE/UPLOAD_ERR_FORM_SIZE $m = $this->createModelForAttributeTest(); $val = new FileValidator(); $val->validateAttribute($m, 'attr_err_ini'); $this->assertTrue($m->hasErrors('attr_err_ini')); - $this->assertTrue(stripos(current($m->getErrors('attr_err_ini')), 'too big') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_err_ini')), 'too big')); // UPLOAD_ERR_PARTIAL $m = $this->createModelForAttributeTest(); $val = new FileValidator(); @@ -343,7 +343,7 @@ class FileValidatorTest extends TestCase $this->assertFalse($m->hasErrors('attr_jpg')); $val->validateAttribute($m, 'attr_exe'); $this->assertTrue($m->hasErrors('attr_exe')); - $this->assertTrue(stripos(current($m->getErrors('attr_exe')), 'Only files with these extensions ') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_exe')), 'Only files with these extensions ')); } public function testIssue11012() diff --git a/tests/framework/validators/NumberValidatorTest.php b/tests/framework/validators/NumberValidatorTest.php index ebd4f02162..7959d1a48e 100644 --- a/tests/framework/validators/NumberValidatorTest.php +++ b/tests/framework/validators/NumberValidatorTest.php @@ -237,7 +237,7 @@ class NumberValidatorTest extends TestCase $model->attr_number = 0; $val->validateAttribute($model, 'attr_number'); $this->assertTrue($model->hasErrors('attr_number')); - $this->assertEquals(1, count($model->getErrors('attr_number'))); + $this->assertCount(1, $model->getErrors('attr_number')); $msgs = $model->getErrors('attr_number'); $this->assertSame('attr_number is to small.', $msgs[0]); } diff --git a/tests/framework/validators/RangeValidatorTest.php b/tests/framework/validators/RangeValidatorTest.php index e105513826..15b9eff585 100644 --- a/tests/framework/validators/RangeValidatorTest.php +++ b/tests/framework/validators/RangeValidatorTest.php @@ -103,7 +103,7 @@ class RangeValidatorTest extends TestCase $val->validateAttribute($m, 'attr_r2'); $this->assertTrue($m->hasErrors('attr_r2')); $err = $m->getErrors('attr_r2'); - $this->assertTrue(stripos($err[0], 'attr_r2') !== false); + $this->assertNotFalse(stripos($err[0], 'attr_r2')); } public function testValidateSubsetArrayable() diff --git a/tests/framework/validators/RequiredValidatorTest.php b/tests/framework/validators/RequiredValidatorTest.php index 8e5cccc970..0c206e4ccf 100644 --- a/tests/framework/validators/RequiredValidatorTest.php +++ b/tests/framework/validators/RequiredValidatorTest.php @@ -47,12 +47,12 @@ class RequiredValidatorTest extends TestCase $m = FakedValidationModel::createWithAttributes(['attr_val' => null]); $val->validateAttribute($m, 'attr_val'); $this->assertTrue($m->hasErrors('attr_val')); - $this->assertTrue(stripos(current($m->getErrors('attr_val')), 'blank') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_val')), 'blank')); $val = new RequiredValidator(['requiredValue' => 55]); $m = FakedValidationModel::createWithAttributes(['attr_val' => 56]); $val->validateAttribute($m, 'attr_val'); $this->assertTrue($m->hasErrors('attr_val')); - $this->assertTrue(stripos(current($m->getErrors('attr_val')), 'must be') !== false); + $this->assertNotFalse(stripos(current($m->getErrors('attr_val')), 'must be')); $val = new RequiredValidator(['requiredValue' => 55]); $m = FakedValidationModel::createWithAttributes(['attr_val' => 55]); $val->validateAttribute($m, 'attr_val'); diff --git a/tests/framework/validators/UrlValidatorTest.php b/tests/framework/validators/UrlValidatorTest.php index b9591670b9..063d172f7a 100644 --- a/tests/framework/validators/UrlValidatorTest.php +++ b/tests/framework/validators/UrlValidatorTest.php @@ -111,7 +111,7 @@ class UrlValidatorTest extends TestCase $obj->attr_url = 'google.de'; $val->validateAttribute($obj, 'attr_url'); $this->assertFalse($obj->hasErrors('attr_url')); - $this->assertTrue(stripos($obj->attr_url, 'http') !== false); + $this->assertNotFalse(stripos($obj->attr_url, 'http')); $obj = new FakedValidationModel; $obj->attr_url = 'gttp;/invalid string'; $val->validateAttribute($obj, 'attr_url'); diff --git a/tests/framework/web/AssetBundleTest.php b/tests/framework/web/AssetBundleTest.php index afd487576c..430da08bbe 100644 --- a/tests/framework/web/AssetBundleTest.php +++ b/tests/framework/web/AssetBundleTest.php @@ -165,9 +165,9 @@ class AssetBundleTest extends \yiiunit\TestCase $this->assertEmpty($view->assetBundles); TestSimpleAsset::register($view); - $this->assertEquals(1, count($view->assetBundles)); + $this->assertCount(1, $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestSimpleAsset', $view->assetBundles); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset'] instanceof AssetBundle); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset']); $expected = <<4 @@ -181,13 +181,13 @@ EOF; $this->assertEmpty($view->assetBundles); TestAssetBundle::register($view); - $this->assertEquals(3, count($view->assetBundles)); + $this->assertCount(3, $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']); $expected = <<23 @@ -226,14 +226,14 @@ EOF; TestJqueryAsset::register($view); } TestAssetBundle::register($view); - $this->assertEquals(3, count($view->assetBundles)); + $this->assertCount(3, $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']); $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions); $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions['position']); @@ -315,9 +315,9 @@ EOF; $this->assertEmpty($view->assetBundles); TestSimpleAsset::register($view); - $this->assertEquals(1, count($view->assetBundles)); + $this->assertCount(1, $view->assetBundles); $this->assertArrayHasKey('yiiunit\\framework\\web\\TestSimpleAsset', $view->assetBundles); - $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset'] instanceof AssetBundle); + $this->assertInstanceOf(AssetBundle::className(), $view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset']); // register TestJqueryAsset which also has the jquery.js TestJqueryAsset::register($view); diff --git a/tests/framework/web/AssetConverterTest.php b/tests/framework/web/AssetConverterTest.php index 15f699fae7..97fe56e91e 100644 --- a/tests/framework/web/AssetConverterTest.php +++ b/tests/framework/web/AssetConverterTest.php @@ -53,8 +53,8 @@ EOF $converter->commands['php'] = ['txt', 'php {from} > {to}']; $this->assertEquals('test.txt', $converter->convert('test.php', $tmpPath)); - $this->assertTrue(file_exists($tmpPath . '/test.txt'), 'Failed asserting that asset output file exists.'); - $this->assertEquals("Hello World!\nHello Yii!", file_get_contents($tmpPath . '/test.txt')); + $this->assertFileExists($tmpPath . '/test.txt', 'Failed asserting that asset output file exists.'); + $this->assertStringEqualsFile($tmpPath . '/test.txt', "Hello World!\nHello Yii!"); } /** @@ -78,7 +78,7 @@ EOF usleep(1); $converter->convert('test.php', $tmpPath); - $this->assertEquals($initialConvertTime, file_get_contents($tmpPath . '/test.txt')); + $this->assertStringEqualsFile($tmpPath . '/test.txt', $initialConvertTime); $converter->forceConvert = true; $converter->convert('test.php', $tmpPath); diff --git a/tests/framework/web/MultipartFormDataParserTest.php b/tests/framework/web/MultipartFormDataParserTest.php index 31bf7f158c..3f9e867b4d 100644 --- a/tests/framework/web/MultipartFormDataParserTest.php +++ b/tests/framework/web/MultipartFormDataParserTest.php @@ -33,18 +33,18 @@ class MultipartFormDataParserTest extends TestCase ]; $this->assertEquals($expectedBodyParams, $bodyParams); - $this->assertFalse(empty($_FILES['someFile'])); + $this->assertNotEmpty($_FILES['someFile']); $this->assertEquals(UPLOAD_ERR_OK, $_FILES['someFile']['error']); $this->assertEquals('some-file.txt', $_FILES['someFile']['name']); $this->assertEquals('text/plain', $_FILES['someFile']['type']); - $this->assertEquals('some file content', file_get_contents($_FILES['someFile']['tmp_name'])); + $this->assertStringEqualsFile($_FILES['someFile']['tmp_name'], 'some file content'); - $this->assertFalse(empty($_FILES['Item'])); - $this->assertFalse(empty($_FILES['Item']['name']['file'])); + $this->assertNotEmpty($_FILES['Item']); + $this->assertNotEmpty($_FILES['Item']['name']['file']); $this->assertEquals(UPLOAD_ERR_OK, $_FILES['Item']['error']['file']); $this->assertEquals('item-file.txt', $_FILES['Item']['name']['file']); $this->assertEquals('text/plain', $_FILES['Item']['type']['file']); - $this->assertEquals('item file content', file_get_contents($_FILES['Item']['tmp_name']['file'])); + $this->assertStringEqualsFile($_FILES['Item']['tmp_name']['file'], 'item file content'); } /** diff --git a/tests/framework/web/RequestTest.php b/tests/framework/web/RequestTest.php index d671f50be0..c225c03066 100644 --- a/tests/framework/web/RequestTest.php +++ b/tests/framework/web/RequestTest.php @@ -245,8 +245,8 @@ class RequestTest extends TestCase $request = new Request(); unset($_SERVER['SERVER_NAME'], $_SERVER['HTTP_HOST']); - $this->assertSame(null, $request->getHostInfo()); - $this->assertSame(null, $request->getHostName()); + $this->assertNull($request->getHostInfo()); + $this->assertNull($request->getHostName()); $request->setHostInfo('http://servername.com:80'); $this->assertSame('http://servername.com:80', $request->getHostInfo()); diff --git a/tests/framework/web/UploadedFileTest.php b/tests/framework/web/UploadedFileTest.php index 2e164f4c41..64d4def3a6 100644 --- a/tests/framework/web/UploadedFileTest.php +++ b/tests/framework/web/UploadedFileTest.php @@ -50,8 +50,8 @@ class UploadedFileTest extends TestCase $productImage = UploadedFile::getInstance(new ModelStub(), 'prod_image'); $vendorImage = VendorImage::getInstance(new ModelStub(), 'vendor_image'); - $this->assertTrue($productImage instanceof UploadedFile); - $this->assertTrue($vendorImage instanceof VendorImage); + $this->assertInstanceOf(UploadedFile::className(), $productImage); + $this->assertInstanceOf(VendorImage::className(), $vendorImage); } public function testGetInstances() @@ -60,11 +60,11 @@ class UploadedFileTest extends TestCase $vendorImages = VendorImage::getInstances(new ModelStub(), 'vendor_images'); foreach ($productImages as $productImage) { - $this->assertTrue($productImage instanceof UploadedFile); + $this->assertInstanceOf(UploadedFile::className(), $productImage); } foreach ($vendorImages as $vendorImage) { - $this->assertTrue($vendorImage instanceof VendorImage); + $this->assertInstanceOf(VendorImage::className(), $vendorImage); } } } \ No newline at end of file diff --git a/tests/framework/web/UserTest.php b/tests/framework/web/UserTest.php index e82693bb7e..f9dcc35660 100644 --- a/tests/framework/web/UserTest.php +++ b/tests/framework/web/UserTest.php @@ -126,17 +126,17 @@ class UserTest extends TestCase $cookie->value = 'junk'; $cookiesMock->add($cookie); Yii::$app->user->getIdentity(); - $this->assertTrue(strlen($cookiesMock->getValue(Yii::$app->user->identityCookie['name'])) == 0); + $this->assertEquals(strlen($cookiesMock->getValue(Yii::$app->user->identityCookie['name'])), 0); Yii::$app->user->login(UserIdentity::findIdentity('user1'),3600); $this->assertFalse(Yii::$app->user->isGuest); $this->assertSame(Yii::$app->user->id, 'user1'); - $this->assertFalse(strlen($cookiesMock->getValue(Yii::$app->user->identityCookie['name'])) == 0); + $this->assertNotEquals(strlen($cookiesMock->getValue(Yii::$app->user->identityCookie['name'])), 0); Yii::$app->user->login(UserIdentity::findIdentity('user2'),0); $this->assertFalse(Yii::$app->user->isGuest); $this->assertSame(Yii::$app->user->id, 'user2'); - $this->assertTrue(strlen($cookiesMock->getValue(Yii::$app->user->identityCookie['name'])) == 0); + $this->assertEquals(strlen($cookiesMock->getValue(Yii::$app->user->identityCookie['name'])), 0); } /** diff --git a/tests/framework/widgets/ActiveFieldTest.php b/tests/framework/widgets/ActiveFieldTest.php index 35256a1e2b..3b0fa34462 100644 --- a/tests/framework/widgets/ActiveFieldTest.php +++ b/tests/framework/widgets/ActiveFieldTest.php @@ -342,7 +342,7 @@ EOD; // expected empty $actualValue = $this->activeField->getClientOptions(); - $this->assertTrue(empty($actualValue) === true); + $this->assertEmpty($actualValue); } public function testGetClientOptionsWithActiveAttributeInScenario() @@ -354,7 +354,7 @@ EOD; // expected empty $actualValue = $this->activeField->getClientOptions(); - $this->assertTrue(empty($actualValue) === true); + $this->assertEmpty($actualValue); } @@ -368,11 +368,11 @@ EOD; $expectedJsExpression = "function (attribute, value, messages, deferred, \$form) {return true;}"; $this->assertEquals($expectedJsExpression, $actualValue['validate']); - $this->assertTrue(!isset($actualValue['validateOnChange'])); - $this->assertTrue(!isset($actualValue['validateOnBlur'])); - $this->assertTrue(!isset($actualValue['validateOnType'])); - $this->assertTrue(!isset($actualValue['validationDelay'])); - $this->assertTrue(!isset($actualValue['enableAjaxValidation'])); + $this->assertNotTrue(isset($actualValue['validateOnChange'])); + $this->assertNotTrue(isset($actualValue['validateOnBlur'])); + $this->assertNotTrue(isset($actualValue['validateOnType'])); + $this->assertNotTrue(isset($actualValue['validationDelay'])); + $this->assertNotTrue(isset($actualValue['enableAjaxValidation'])); $this->activeField->validateOnChange = $expectedValidateOnChange = false; $this->activeField->validateOnBlur = $expectedValidateOnBlur = false; @@ -382,11 +382,11 @@ EOD; $actualValue = $this->activeField->getClientOptions(); - $this->assertTrue($expectedValidateOnChange === $actualValue['validateOnChange']); - $this->assertTrue($expectedValidateOnBlur === $actualValue['validateOnBlur']); - $this->assertTrue($expectedValidateOnType === $actualValue['validateOnType']); - $this->assertTrue($expectedValidationDelay === $actualValue['validationDelay']); - $this->assertTrue($expectedEnableAjaxValidation === $actualValue['enableAjaxValidation']); + $this->assertSame($expectedValidateOnChange, $actualValue['validateOnChange']); + $this->assertSame($expectedValidateOnBlur, $actualValue['validateOnBlur']); + $this->assertSame($expectedValidateOnType, $actualValue['validateOnType']); + $this->assertSame($expectedValidationDelay, $actualValue['validationDelay']); + $this->assertSame($expectedEnableAjaxValidation, $actualValue['enableAjaxValidation']); } public function testGetClientOptionsValidatorWhenClientSet() diff --git a/tests/framework/widgets/LinkSorterTest.php b/tests/framework/widgets/LinkSorterTest.php index 50bac464ff..9377c5fce6 100644 --- a/tests/framework/widgets/LinkSorterTest.php +++ b/tests/framework/widgets/LinkSorterTest.php @@ -47,8 +47,10 @@ class LinkSorterTest extends DatabaseTestCase ]); $actualHtml = ob_get_clean(); - $this->assertTrue(strpos($actualHtml, 'Customer') !== false); - $this->assertTrue(strpos($actualHtml, 'Invoice Total') !== false); + $this->assertNotFalse(strpos($actualHtml, + 'Customer')); + $this->assertNotFalse(strpos($actualHtml, + 'Invoice Total')); } public function testLabelsExplicit() @@ -70,8 +72,10 @@ class LinkSorterTest extends DatabaseTestCase ]); $actualHtml = ob_get_clean(); - $this->assertFalse(strpos($actualHtml, 'Customer') !== false); - $this->assertTrue(strpos($actualHtml, 'Invoice Total') !== false); + $this->assertFalse(strpos($actualHtml, + 'Customer')); + $this->assertNotFalse(strpos($actualHtml, + 'Invoice Total')); } }