diff --git a/api/controllers/AuthenticationController.php b/api/controllers/AuthenticationController.php index ee8c000..c190c16 100644 --- a/api/controllers/AuthenticationController.php +++ b/api/controllers/AuthenticationController.php @@ -107,7 +107,7 @@ class AuthenticationController extends Controller { ], ]; - if ($model->getLoginAttribute() !== 'email') { + if (strpos($model->login, '@') === false) { $response['data']['emailMask'] = StringHelper::getEmailMask($model->getAccount()->email); } diff --git a/api/models/authentication/ForgotPasswordForm.php b/api/models/authentication/ForgotPasswordForm.php index b0e5bc2..48c3296 100644 --- a/api/models/authentication/ForgotPasswordForm.php +++ b/api/models/authentication/ForgotPasswordForm.php @@ -1,10 +1,11 @@ E::LOGIN_REQUIRED], @@ -31,7 +31,7 @@ class ForgotPasswordForm extends ApiForm { ]; } - public function validateLogin($attribute) { + public function validateLogin(string $attribute): void { if (!$this->hasErrors()) { if ($this->getAccount() === null) { $this->addError($attribute, E::LOGIN_NOT_EXIST); @@ -39,7 +39,7 @@ class ForgotPasswordForm extends ApiForm { } } - public function validateActivity($attribute) { + public function validateActivity(string $attribute): void { if (!$this->hasErrors()) { $account = $this->getAccount(); if ($account->status !== Account::STATUS_ACTIVE) { @@ -48,7 +48,7 @@ class ForgotPasswordForm extends ApiForm { } } - public function validateFrequency($attribute) { + public function validateFrequency(string $attribute): void { if (!$this->hasErrors()) { $emailConfirmation = $this->getEmailActivation(); if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) { @@ -57,12 +57,15 @@ class ForgotPasswordForm extends ApiForm { } } + public function getAccount(): ?Account { + return Account::find()->andWhereLogin($this->login)->one(); + } + /** * @CollectModelMetrics(prefix="authentication.forgotPassword") * @return bool - * @throws ErrorException */ - public function forgotPassword() { + public function forgotPassword(): bool { if (!$this->validate()) { return false; } @@ -86,23 +89,13 @@ class ForgotPasswordForm extends ApiForm { return true; } - public function getLogin(): string { - return $this->login; - } - - /** - * @return EmailActivation|null - * @throws ErrorException - */ - public function getEmailActivation() { + public function getEmailActivation(): ?EmailActivation { $account = $this->getAccount(); if ($account === null) { - throw new ErrorException('Account not founded'); + return null; } - return $account->getEmailActivations() - ->andWhere(['type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY]) - ->one(); + return $account->getEmailActivations()->withType(EmailActivation::TYPE_FORGOT_PASSWORD_KEY)->one(); } } diff --git a/api/models/authentication/LoginForm.php b/api/models/authentication/LoginForm.php index 7dc91fc..ba059dd 100644 --- a/api/models/authentication/LoginForm.php +++ b/api/models/authentication/LoginForm.php @@ -5,7 +5,6 @@ namespace api\models\authentication; use api\aop\annotations\CollectModelMetrics; use api\models\base\ApiForm; -use api\traits\AccountFinder; use api\validators\TotpValidator; use common\helpers\Error as E; use common\models\Account; @@ -14,14 +13,25 @@ use Webmozart\Assert\Assert; use Yii; class LoginForm extends ApiForm { - use AccountFinder; + /** + * @var string + */ public $login; + /** + * @var string + */ public $password; + /** + * @var string|null + */ public $totp; + /** + * @var bool + */ public $rememberMe = false; public function rules(): array { @@ -90,8 +100,8 @@ class LoginForm extends ApiForm { } } - public function getLogin(): string { - return $this->login; + public function getAccount(): ?Account { + return Account::find()->andWhereLogin($this->login)->one(); } /** diff --git a/api/models/authentication/RepeatAccountActivationForm.php b/api/models/authentication/RepeatAccountActivationForm.php index f400ba2..7725ab8 100644 --- a/api/models/authentication/RepeatAccountActivationForm.php +++ b/api/models/authentication/RepeatAccountActivationForm.php @@ -1,4 +1,6 @@ 'trim'], @@ -31,7 +33,7 @@ class RepeatAccountActivationForm extends ApiForm { ]; } - public function validateEmailForAccount($attribute) { + public function validateEmailForAccount(string $attribute): void { if (!$this->hasErrors()) { $account = $this->getAccount(); if ($account === null) { @@ -45,7 +47,7 @@ class RepeatAccountActivationForm extends ApiForm { } } - public function validateExistsActivation($attribute) { + public function validateExistsActivation(string $attribute): void { if (!$this->hasErrors()) { $activation = $this->getActivation(); if ($activation !== null && !$activation->canRepeat()) { @@ -58,11 +60,12 @@ class RepeatAccountActivationForm extends ApiForm { * @CollectModelMetrics(prefix="signup.repeatEmail") * @return bool */ - public function sendRepeatMessage() { + public function sendRepeatMessage(): bool { if (!$this->validate()) { return false; } + /** @var Account $account */ $account = $this->getAccount(); $transaction = Yii::$app->db->beginTransaction(); @@ -85,27 +88,17 @@ class RepeatAccountActivationForm extends ApiForm { return true; } - /** - * @return Account|null - */ - public function getAccount() { + public function getAccount(): ?Account { return Account::find() ->andWhere(['email' => $this->email]) ->one(); } - /** - * @return EmailActivation|null - */ - public function getActivation() { - if ($this->emailActivation === null) { - $this->emailActivation = $this->getAccount() - ->getEmailActivations() - ->andWhere(['type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION]) - ->one(); - } - - return $this->emailActivation; + public function getActivation(): ?EmailActivation { + return $this->getAccount() + ->getEmailActivations() + ->withType(EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION) + ->one(); } } diff --git a/api/modules/accounts/models/SendEmailVerificationForm.php b/api/modules/accounts/models/SendEmailVerificationForm.php index c6189aa..4474a18 100644 --- a/api/modules/accounts/models/SendEmailVerificationForm.php +++ b/api/modules/accounts/models/SendEmailVerificationForm.php @@ -1,4 +1,6 @@ hasErrors()) { $emailConfirmation = $this->getEmailActivation(); if ($emailConfirmation !== null && !$emailConfirmation->canRepeat()) { @@ -82,12 +84,10 @@ class SendEmailVerificationForm extends AccountActionForm { public function getEmailActivation(): ?EmailActivation { return $this->getAccount() ->getEmailActivations() - ->andWhere([ - 'type' => [ - EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION, - EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION, - ], - ]) + ->withType( + EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION, + EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION + ) ->one(); } diff --git a/api/modules/authserver/models/AuthenticationForm.php b/api/modules/authserver/models/AuthenticationForm.php index 6af8e2a..58f04c7 100644 --- a/api/modules/authserver/models/AuthenticationForm.php +++ b/api/modules/authserver/models/AuthenticationForm.php @@ -75,10 +75,7 @@ class AuthenticationForm extends ApiForm { // The previous authorization server implementation used the nickname field instead of username, // so we keep such behavior - $attribute = $loginForm->getLoginAttribute(); - if ($attribute === 'username') { - $attribute = 'nickname'; - } + $attribute = strpos($this->username, '@') === false ? 'nickname' : 'email'; // TODO: эта логика дублируется с логикой в SignoutForm diff --git a/api/modules/authserver/models/SignoutForm.php b/api/modules/authserver/models/SignoutForm.php index f2a7ef3..154ad86 100644 --- a/api/modules/authserver/models/SignoutForm.php +++ b/api/modules/authserver/models/SignoutForm.php @@ -47,10 +47,7 @@ class SignoutForm extends ApiForm { // The previous authorization server implementation used the nickname field instead of username, // so we keep such behavior - $attribute = $loginForm->getLoginAttribute(); - if ($attribute === 'username') { - $attribute = 'nickname'; - } + $attribute = strpos($this->username, '@') === false ? 'nickname' : 'email'; throw new ForbiddenOperationException("Invalid credentials. Invalid {$attribute} or password."); } diff --git a/api/request/RequestParser.php b/api/request/RequestParser.php index a63ea0c..f54025a 100644 --- a/api/request/RequestParser.php +++ b/api/request/RequestParser.php @@ -1,4 +1,6 @@ key); + public function getEmailActivation(): ?EmailActivation { + return EmailActivation::findOne(['key' => $this->key]); } }; } diff --git a/api/tests/unit/models/authentication/RepeatAccountActivationFormTest.php b/api/tests/unit/models/authentication/RepeatAccountActivationFormTest.php index 9e9895f..3d0f2c9 100644 --- a/api/tests/unit/models/authentication/RepeatAccountActivationFormTest.php +++ b/api/tests/unit/models/authentication/RepeatAccountActivationFormTest.php @@ -100,7 +100,7 @@ class RepeatAccountActivationFormTest extends TestCase { return new class($params) extends RepeatAccountActivationForm { public $emailKey; - public function getActivation() { + public function getActivation(): ?EmailActivation { return EmailActivation::findOne($this->emailKey); } }; diff --git a/api/tests/unit/modules/accounts/models/ChangeEmailFormTest.php b/api/tests/unit/modules/accounts/models/ChangeEmailFormTest.php index d8665a0..f83afb2 100644 --- a/api/tests/unit/modules/accounts/models/ChangeEmailFormTest.php +++ b/api/tests/unit/modules/accounts/models/ChangeEmailFormTest.php @@ -29,7 +29,6 @@ class ChangeEmailFormTest extends TestCase { 'account_id' => $account->id, 'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION, ])); - /** @noinspection UnserializeExploitsInspection */ $data = unserialize($newEmailConfirmationFixture['_data']); $this->assertSame($data['newEmail'], $account->email); } diff --git a/api/tests/unit/traits/AccountFinderTest.php b/api/tests/unit/traits/AccountFinderTest.php deleted file mode 100644 index 755d750..0000000 --- a/api/tests/unit/traits/AccountFinderTest.php +++ /dev/null @@ -1,52 +0,0 @@ - AccountFixture::class, - ]; - } - - public function testGetAccount() { - $model = new AccountFinderTestTestClass(); - /** @var Account $account */ - $accountFixture = $this->tester->grabFixture('accounts', 'admin'); - $model->login = $accountFixture->email; - $account = $model->getAccount(); - $this->assertInstanceOf(Account::class, $account); - $this->assertSame($accountFixture->id, $account->id, 'founded account for passed login data'); - - $model = new AccountFinderTestTestClass(); - $model->login = 'unexpected'; - $this->assertNull($account = $model->getAccount(), 'null, if account can\'t be found'); - } - - public function testGetLoginAttribute() { - $model = new AccountFinderTestTestClass(); - $model->login = 'erickskrauch@ely.by'; - $this->assertSame('email', $model->getLoginAttribute(), 'if login look like email value, then \'email\''); - - $model = new AccountFinderTestTestClass(); - $model->login = 'erickskrauch'; - $this->assertSame('username', $model->getLoginAttribute(), 'username in any other case'); - } - -} - -class AccountFinderTestTestClass { - use AccountFinder; - - public $login; - - public function getLogin(): string { - return $this->login; - } - -} diff --git a/api/traits/AccountFinder.php b/api/traits/AccountFinder.php deleted file mode 100644 index 33a01b5..0000000 --- a/api/traits/AccountFinder.php +++ /dev/null @@ -1,24 +0,0 @@ -account === null) { - $this->account = Account::findOne([$this->getLoginAttribute() => $this->getLogin()]); - } - - return $this->account; - } - - public function getLoginAttribute(): string { - return strpos($this->getLogin(), '@') ? 'email' : 'username'; - } - -} diff --git a/api/validators/EmailActivationKeyValidator.php b/api/validators/EmailActivationKeyValidator.php index e6ee696..ce84462 100644 --- a/api/validators/EmailActivationKeyValidator.php +++ b/api/validators/EmailActivationKeyValidator.php @@ -24,7 +24,7 @@ class EmailActivationKeyValidator extends Validator { public $skipOnEmpty = false; - public function validateAttribute($model, $attribute) { + public function validateAttribute($model, $attribute): void { $value = $model->$attribute; if (empty($value)) { $this->addError($model, $attribute, $this->keyRequired); @@ -49,7 +49,7 @@ class EmailActivationKeyValidator extends Validator { $query = EmailActivation::find(); $query->andWhere(['key' => $key]); if ($type !== null) { - $query->andWhere(['type' => $type]); + $query->withType($type); } return $query->one(); diff --git a/common/models/Account.php b/common/models/Account.php index 9cf7ef0..c5cbc12 100644 --- a/common/models/Account.php +++ b/common/models/Account.php @@ -59,6 +59,10 @@ class Account extends ActiveRecord { return '{{%accounts}}'; } + public static function find(): AccountQuery { + return new AccountQuery(self::class); + } + public function behaviors(): array { return [ TimestampBehavior::class, @@ -88,7 +92,7 @@ class Account extends ActiveRecord { $this->password_changed_at = time(); } - public function getEmailActivations(): ActiveQuery { + public function getEmailActivations(): EmailActivationQuery { return $this->hasMany(EmailActivation::class, ['account_id' => 'id']); } diff --git a/common/models/AccountQuery.php b/common/models/AccountQuery.php new file mode 100644 index 0000000..c94a351 --- /dev/null +++ b/common/models/AccountQuery.php @@ -0,0 +1,21 @@ +andWhere([$this->getLoginAttribute($login) => $login]); + } + + private function getLoginAttribute(string $login): string { + return strpos($login, '@') ? 'email' : 'username'; + } + +} diff --git a/common/models/EmailActivation.php b/common/models/EmailActivation.php index 566a150..8d386c1 100644 --- a/common/models/EmailActivation.php +++ b/common/models/EmailActivation.php @@ -1,4 +1,6 @@ TimestampBehavior::class, @@ -61,7 +67,7 @@ class EmailActivation extends ActiveRecord { ]; } - public function getAccount() { + public function getAccount(): AccountQuery { return $this->hasOne(Account::class, ['id' => 'account_id']); } @@ -82,7 +88,7 @@ class EmailActivation extends ActiveRecord { return new $classMap[$type](); } - public static function getClassMap() { + public static function getClassMap(): array { return [ self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class, self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class, diff --git a/common/models/EmailActivationQuery.php b/common/models/EmailActivationQuery.php new file mode 100644 index 0000000..0798e55 --- /dev/null +++ b/common/models/EmailActivationQuery.php @@ -0,0 +1,17 @@ +andWhere(['type' => $typeId]); + } + +} diff --git a/common/models/OauthScopeQuery.php b/common/models/OauthScopeQuery.php deleted file mode 100644 index b81730d..0000000 --- a/common/models/OauthScopeQuery.php +++ /dev/null @@ -1,50 +0,0 @@ -scopes = $scopes; - } - - public function onlyPublic(): self { - $this->internal = false; - return $this; - } - - public function onlyInternal(): self { - $this->internal = true; - return $this; - } - - public function usersScopes(): self { - $this->owner = 'user'; - return $this; - } - - public function machineScopes(): self { - $this->owner = 'machine'; - return $this; - } - - public function all(): array { - return ArrayHelper::getColumn(array_filter($this->scopes, function($value) { - $shouldCheckInternal = $this->internal !== null; - $isInternalMatch = $value['internal'] === $this->internal; - $shouldCheckOwner = $this->owner !== null; - $isOwnerMatch = $value['owner'] === $this->owner; - - return (!$shouldCheckInternal || $isInternalMatch) - && (!$shouldCheckOwner || $isOwnerMatch); - }), 'value'); - } - -} diff --git a/common/models/confirmations/CurrentEmailConfirmation.php b/common/models/confirmations/CurrentEmailConfirmation.php index 945a1f1..75276fa 100644 --- a/common/models/confirmations/CurrentEmailConfirmation.php +++ b/common/models/confirmations/CurrentEmailConfirmation.php @@ -1,12 +1,19 @@ withType(EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION); + } + + public function behaviors(): array { return ArrayHelper::merge(parent::behaviors(), [ 'expirationBehavior' => [ 'repeatTimeout' => 6 * 60 * 60, // 6h @@ -15,7 +22,7 @@ class CurrentEmailConfirmation extends EmailActivation { ]); } - public function init() { + public function init(): void { parent::init(); $this->type = EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION; } diff --git a/common/models/confirmations/ForgotPassword.php b/common/models/confirmations/ForgotPassword.php index d72780b..458ad76 100644 --- a/common/models/confirmations/ForgotPassword.php +++ b/common/models/confirmations/ForgotPassword.php @@ -1,12 +1,19 @@ withType(EmailActivation::TYPE_FORGOT_PASSWORD_KEY); + } + + public function behaviors(): array { return ArrayHelper::merge(parent::behaviors(), [ 'expirationBehavior' => [ 'repeatTimeout' => 30 * 60, @@ -15,7 +22,7 @@ class ForgotPassword extends EmailActivation { ]); } - public function init() { + public function init(): void { parent::init(); $this->type = EmailActivation::TYPE_FORGOT_PASSWORD_KEY; } diff --git a/common/models/confirmations/NewEmailConfirmation.php b/common/models/confirmations/NewEmailConfirmation.php index 5f73e23..2f026ea 100644 --- a/common/models/confirmations/NewEmailConfirmation.php +++ b/common/models/confirmations/NewEmailConfirmation.php @@ -1,7 +1,10 @@ withType(EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION); + } + + public function behaviors(): array { return ArrayHelper::merge(parent::behaviors(), [ 'expirationBehavior' => [ 'repeatTimeout' => 5 * 60, @@ -21,7 +28,7 @@ class NewEmailConfirmation extends EmailActivation { ]); } - public function init() { + public function init(): void { parent::init(); $this->type = EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION; } diff --git a/common/models/confirmations/NewEmailConfirmationBehavior.php b/common/models/confirmations/NewEmailConfirmationBehavior.php index 169d88c..158d2bd 100644 --- a/common/models/confirmations/NewEmailConfirmationBehavior.php +++ b/common/models/confirmations/NewEmailConfirmationBehavior.php @@ -1,4 +1,6 @@ withType(EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION); + } + + public function init(): void { parent::init(); $this->type = EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION; } diff --git a/common/tests/unit/tasks/SendCurrentEmailConfirmationTest.php b/common/tests/unit/tasks/SendCurrentEmailConfirmationTest.php index 6301d88..6066b4e 100644 --- a/common/tests/unit/tasks/SendCurrentEmailConfirmationTest.php +++ b/common/tests/unit/tasks/SendCurrentEmailConfirmationTest.php @@ -1,7 +1,10 @@ email = 'mock@ely.by'; $account->lang = 'id'; - /** @var \Mockery\Mock|CurrentEmailConfirmation $confirmation */ - $confirmation = mock(CurrentEmailConfirmation::class)->makePartial(); + $accountQuery = $this->createMock(AccountQuery::class); + $accountQuery->method('findFor')->willReturn($account); + + $confirmation = $this->createPartialMock(CurrentEmailConfirmation::class, ['getAccount']); + $confirmation->method('getAccount')->willReturn($accountQuery); $confirmation->key = 'ABCDEFG'; - $confirmation->shouldReceive('getAccount')->andReturn($account); $result = SendCurrentEmailConfirmation::createFromConfirmation($confirmation); - $this->assertInstanceOf(SendCurrentEmailConfirmation::class, $result); $this->assertSame('mock-username', $result->username); $this->assertSame('mock@ely.by', $result->email); $this->assertSame('ABCDEFG', $result->code); @@ -33,7 +37,7 @@ class SendCurrentEmailConfirmationTest extends TestCase { $task->email = 'mock@ely.by'; $task->code = 'GFEDCBA'; - $task->execute(mock(Queue::class)); + $task->execute($this->createMock(Queue::class)); $this->tester->canSeeEmailIsSent(1); /** @var \yii\swiftmailer\Message $email */ diff --git a/common/tests/unit/tasks/SendNewEmailConfirmationTest.php b/common/tests/unit/tasks/SendNewEmailConfirmationTest.php index 33b2444..740a0ce 100644 --- a/common/tests/unit/tasks/SendNewEmailConfirmationTest.php +++ b/common/tests/unit/tasks/SendNewEmailConfirmationTest.php @@ -1,7 +1,10 @@ username = 'mock-username'; $account->lang = 'id'; - /** @var \Mockery\Mock|NewEmailConfirmation $confirmation */ - $confirmation = mock(NewEmailConfirmation::class)->makePartial(); + $accountQuery = $this->createMock(AccountQuery::class); + $accountQuery->method('findFor')->willReturn($account); + + $confirmation = $this->createPartialMock(NewEmailConfirmation::class, ['getAccount']); + $confirmation->method('getAccount')->willReturn($accountQuery); + $confirmation->setNewEmail('new-email@ely.by'); $confirmation->key = 'ABCDEFG'; - $confirmation->shouldReceive('getAccount')->andReturn($account); - $confirmation->shouldReceive('getNewEmail')->andReturn('new-email@ely.by'); $result = SendNewEmailConfirmation::createFromConfirmation($confirmation); - $this->assertInstanceOf(SendNewEmailConfirmation::class, $result); $this->assertSame('mock-username', $result->username); $this->assertSame('new-email@ely.by', $result->email); $this->assertSame('ABCDEFG', $result->code); @@ -33,7 +37,7 @@ class SendNewEmailConfirmationTest extends TestCase { $task->email = 'mock@ely.by'; $task->code = 'GFEDCBA'; - $task->execute(mock(Queue::class)); + $task->execute($this->createMock(Queue::class)); $this->tester->canSeeEmailIsSent(1); /** @var \yii\swiftmailer\Message $email */ diff --git a/common/tests/unit/tasks/SendPasswordRecoveryEmailTest.php b/common/tests/unit/tasks/SendPasswordRecoveryEmailTest.php index 0112df0..4119df3 100644 --- a/common/tests/unit/tasks/SendPasswordRecoveryEmailTest.php +++ b/common/tests/unit/tasks/SendPasswordRecoveryEmailTest.php @@ -5,6 +5,7 @@ namespace common\tests\unit\tasks; use common\emails\RendererInterface; use common\models\Account; +use common\models\AccountQuery; use common\models\confirmations\ForgotPassword; use common\tasks\SendPasswordRecoveryEmail; use common\tests\unit\TestCase; @@ -24,10 +25,12 @@ class SendPasswordRecoveryEmailTest extends TestCase { $account->email = 'mock@ely.by'; $account->lang = 'id'; - /** @var \Mockery\Mock|ForgotPassword $confirmation */ - $confirmation = mock(ForgotPassword::class)->makePartial(); + $accountQuery = $this->createMock(AccountQuery::class); + $accountQuery->method('findFor')->willReturn($account); + + $confirmation = $this->createPartialMock(ForgotPassword::class, ['getAccount']); + $confirmation->method('getAccount')->willReturn($accountQuery); $confirmation->key = 'ABCDEFG'; - $confirmation->shouldReceive('getAccount')->andReturn($account); $result = SendPasswordRecoveryEmail::createFromConfirmation($confirmation); $this->assertSame('mock-username', $result->username); @@ -51,7 +54,7 @@ class SendPasswordRecoveryEmailTest extends TestCase { 'link' => 'https://account.ely.by/recover-password/ABCDEFG', ])->willReturn('mock-template'); - $task->execute(mock(Queue::class)); + $task->execute($this->createMock(Queue::class)); $this->tester->canSeeEmailIsSent(1); /** @var \yii\swiftmailer\Message $email */ diff --git a/common/tests/unit/tasks/SendRegistrationEmailTest.php b/common/tests/unit/tasks/SendRegistrationEmailTest.php index 1cc69d4..16ad418 100644 --- a/common/tests/unit/tasks/SendRegistrationEmailTest.php +++ b/common/tests/unit/tasks/SendRegistrationEmailTest.php @@ -5,6 +5,7 @@ namespace common\tests\unit\tasks; use common\emails\RendererInterface; use common\models\Account; +use common\models\AccountQuery; use common\models\confirmations\RegistrationConfirmation; use common\tasks\SendRegistrationEmail; use common\tests\unit\TestCase; @@ -24,10 +25,12 @@ class SendRegistrationEmailTest extends TestCase { $account->email = 'mock@ely.by'; $account->lang = 'ru'; - /** @var \Mockery\Mock|RegistrationConfirmation $confirmation */ - $confirmation = mock(RegistrationConfirmation::class)->makePartial(); + $accountQuery = $this->createMock(AccountQuery::class); + $accountQuery->method('findFor')->willReturn($account); + + $confirmation = $this->createPartialMock(RegistrationConfirmation::class, ['getAccount']); + $confirmation->method('getAccount')->willReturn($accountQuery); $confirmation->key = 'ABCDEFG'; - $confirmation->shouldReceive('getAccount')->andReturn($account); $result = SendRegistrationEmail::createFromConfirmation($confirmation); $this->assertSame('mock-username', $result->username);