public function testBatchRemoveForSinglePrimaryKey()
 {
     $ids = [];
     for ($i = 0; $i < 3; $i++) {
         $user = new User();
         $user->username = "******";
         $user->password = "******";
         $user->save(false);
         $ids[] = $user->id;
     }
     Yii::$app->request->setBodyParams($ids);
     $this->specify('Remove a AR with single primary key', function () {
         $action = new BatchRemoveAction("batch-remove", null, ['modelClass' => 'tests\\codeception\\unit\\models\\User']);
         $n = $action->run();
         expect("Number of deleted records should be 3: ", $n)->equals(3);
     });
 }
示例#2
0
 public function testBatchSaveReturnModels()
 {
     /** @var User[] $inputModels */
     $inputModels = [];
     for ($i = 0; $i < 10; $i++) {
         $m = $inputModels[] = new User();
         $m->username = "******" . rand(1000, 9999);
         $m->password = $m->username;
     }
     /** @var array $savedReturn */
     $savedReturn = [];
     $return = DbHelper::batchSave($inputModels, [], DbHelper::SAVE_MODE_AUTO, $savedReturn);
     $this->assertObjectNotHasAttribute('updateCount', $return);
     $this->assertObjectHasAttribute('insertCount', $return);
     $this->assertEquals(10, $return->insertCount);
     $this->assertObjectHasAttribute('lastId', $return);
     /** @var User $lastUser */
     $lastUser = User::findOne($return->lastId);
     /** @var User $firstUser */
     $firstUser = User::findOne($return->lastId - $return->insertCount + 1);
     $this->assertNotEmpty($lastUser);
     $this->assertNotEmpty($firstUser);
     $this->assertEquals($inputModels[0]->username, $firstUser->username);
     $this->assertEquals($inputModels[9]->username, $lastUser->username);
     $this->assertArrayHasKey('inserted', $savedReturn);
     $this->assertArrayNotHasKey('updated', $savedReturn);
     for ($i = 0; $i < 10; $i++) {
         /** @var User $m */
         $m = $savedReturn['inserted'][$i];
         $this->assertEquals($return->lastId - 9 + $i, $m->id);
     }
     $inputModels = $savedReturn['inserted'];
     for ($i = 0; $i < 10; $i++) {
         $m = $inputModels[] = new User();
         $m->username = "******" . rand(1000, 9999);
         $m->password = $m->username;
     }
     $updatedLastId = $return->lastId;
     $return = DbHelper::batchSave($inputModels, [], DbHelper::SAVE_MODE_AUTO, $savedReturn);
     $this->assertObjectHasAttribute('updateCount', $return);
     $this->assertEquals(10, $return->updateCount);
     $this->assertObjectHasAttribute('insertCount', $return);
     $this->assertEquals(10, $return->insertCount);
     $this->assertObjectHasAttribute('lastId', $return);
     $this->assertArrayHasKey('inserted', $savedReturn);
     $this->assertArrayHasKey('updated', $savedReturn);
     for ($i = 0; $i < 10; $i++) {
         /** @var User $m */
         $m = $savedReturn['inserted'][$i];
         $this->assertEquals($return->lastId - 9 + $i, $m->id);
         $m = $savedReturn['updated'][$i];
         $this->assertEquals($updatedLastId - 9 + $i, $m->id);
     }
 }