Упразднён контроллер PasswordProtectedController и заменён на валидатор PasswordRequiredValidator

Реорганизованы тесты для ChangeUsernameFormTest
This commit is contained in:
ErickSkrauch
2016-07-25 14:07:14 +03:00
parent a4dad11be5
commit 31679b84cb
9 changed files with 121 additions and 105 deletions

View File

@@ -1,38 +0,0 @@
<?php
namespace tests\codeception\api\models\base;
use api\models\base\PasswordProtectedForm;
use Codeception\Specify;
use common\models\Account;
use tests\codeception\api\unit\TestCase;
class PasswordProtectedFormTest extends TestCase {
use Specify;
public function testValidatePassword() {
$this->specify('error.password_invalid on passing invalid account password', function() {
$model = new DummyBasePasswordProtectedForm();
$model->password = 'some-invalid-password';
$model->validatePassword();
expect($model->getErrors('password'))->equals(['error.password_invalid']);
});
$this->specify('no errors on passing valid account password', function() {
$model = new DummyBasePasswordProtectedForm();
$model->password = 'password_0';
$model->validatePassword();
expect($model->getErrors('password'))->isEmpty();
});
}
}
class DummyBasePasswordProtectedForm extends PasswordProtectedForm {
protected function getAccount() {
return new Account([
'password' => 'password_0',
]);
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace tests\codeception\api\models\profile;
use api\models\AccountIdentity;
use api\models\profile\ChangeUsernameForm;
use Codeception\Specify;
use common\models\Account;
@@ -8,6 +9,7 @@ use common\models\UsernameHistory;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use tests\codeception\common\fixtures\UsernameHistoryFixture;
use Yii;
/**
* @property AccountFixture $accounts
@@ -22,9 +24,15 @@ class ChangeUsernameFormTest extends DbTestCase {
];
}
public function setUp() {
parent::setUp();
$account = AccountIdentity::findOne($this->getAccountId());
Yii::$app->user->setIdentity($account);
}
public function testChange() {
$this->specify('successfully change username to new one', function() {
$model = $this->createModel([
$model = new ChangeUsernameForm([
'password' => 'password_0',
'username' => 'my_new_nickname',
]);
@@ -36,7 +44,7 @@ class ChangeUsernameFormTest extends DbTestCase {
public function testChangeWithoutChange() {
$this->specify('no new UsernameHistory record, if we don\'t change nickname', function() {
$model = $this->createModel([
$model = new ChangeUsernameForm([
'password' => 'password_0',
'username' => $this->accounts['admin']['username'],
]);
@@ -53,7 +61,7 @@ class ChangeUsernameFormTest extends DbTestCase {
public function testChangeCase() {
$this->specify('username should change, if we change case of some letters', function() {
$newUsername = mb_strtoupper($this->accounts['admin']['username']);
$model = $this->createModel([
$model = new ChangeUsernameForm([
'password' => 'password_0',
'username' => $newUsername,
]);
@@ -65,7 +73,7 @@ class ChangeUsernameFormTest extends DbTestCase {
public function testValidateUsername() {
$this->specify('error.username_not_available expected if username is already taken', function() {
$model = $this->createModel([
$model = new ChangeUsernameForm([
'password' => 'password_0',
'username' => 'Jon',
]);
@@ -74,7 +82,7 @@ class ChangeUsernameFormTest extends DbTestCase {
});
$this->specify('error.username_not_available is NOT expected if username is already taken by CURRENT user', function() {
$model = $this->createModel([
$model = new ChangeUsernameForm([
'password' => 'password_0',
'username' => $this->accounts['admin']['username'],
]);
@@ -84,27 +92,12 @@ class ChangeUsernameFormTest extends DbTestCase {
}
public function testCreateTask() {
$model = $this->createModel();
$model = new ChangeUsernameForm();
$model->createEventTask('1', 'test1', 'test');
// TODO: у меня пока нет идей о том, чтобы это как-то успешно протестировать, увы
// но по крайней мере можно убедиться, что оно не падает где-то на этом шаге
}
private function createModel(array $params = []) : ChangeUsernameForm {
/** @noinspection PhpUnusedLocalVariableInspection */
$params = array_merge($params, [
'accountId' => $this->getAccountId(),
]);
return new class($params) extends ChangeUsernameForm {
public $accountId;
protected function getAccount() {
return Account::findOne($this->accountId);
}
};
}
private function getAccountId() {
return $this->accounts['admin']['id'];
}