mirror of
https://github.com/elyby/accounts.git
synced 2024-11-30 02:32:26 +05:30
Написан тест для поведения beforeSave модели EmailActivation
This commit is contained in:
parent
c25bfeb8bc
commit
ed6bc672cb
@ -64,7 +64,7 @@ class EmailActivation extends ActiveRecord {
|
||||
public static function instantiate($row) {
|
||||
$type = ArrayHelper::getValue($row, 'type');
|
||||
if ($type === null) {
|
||||
return new static;
|
||||
return parent::instantiate($row);
|
||||
}
|
||||
|
||||
$classMap = self::getClassMap();
|
||||
@ -78,9 +78,9 @@ class EmailActivation extends ActiveRecord {
|
||||
public static function getClassMap() {
|
||||
return [
|
||||
self::TYPE_REGISTRATION_EMAIL_CONFIRMATION => confirmations\RegistrationConfirmation::class,
|
||||
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
||||
self::TYPE_CURRENT_EMAIL_CONFIRMATION => confirmations\CurrentEmailConfirmation::class,
|
||||
self::TYPE_NEW_EMAIL_CONFIRMATION => confirmations\NewEmailConfirmation::class,
|
||||
self::TYPE_FORGOT_PASSWORD_KEY => confirmations\ForgotPassword::class,
|
||||
self::TYPE_CURRENT_EMAIL_CONFIRMATION => confirmations\CurrentEmailConfirmation::class,
|
||||
self::TYPE_NEW_EMAIL_CONFIRMATION => confirmations\NewEmailConfirmation::class,
|
||||
];
|
||||
}
|
||||
|
||||
@ -91,11 +91,19 @@ class EmailActivation extends ActiveRecord {
|
||||
|
||||
if ($this->key === null) {
|
||||
do {
|
||||
$this->key = UserFriendlyRandomKey::make();
|
||||
} while (EmailActivation::find()->andWhere(['key' => $this->key])->exists());
|
||||
$this->key = $this->generateKey();
|
||||
} while ($this->isKeyExists($this->key));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function generateKey() : string {
|
||||
return UserFriendlyRandomKey::make();
|
||||
}
|
||||
|
||||
protected function isKeyExists(string $key) : bool {
|
||||
return self::find()->andWhere(['key' => $key])->exists();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -28,4 +28,30 @@ class EmailActivationTest extends DbTestCase {
|
||||
});
|
||||
}
|
||||
|
||||
public function testBeforeSave() {
|
||||
$this->specify('method should generate value for key field if it empty', function() {
|
||||
$model = new EmailActivation();
|
||||
$model->beforeSave(true);
|
||||
expect($model->key)->notNull();
|
||||
});
|
||||
|
||||
$this->specify('method should repeat code generation if code duplicate with exists', function() {
|
||||
/** @var EmailActivation|\PHPUnit_Framework_MockObject_MockObject $model */
|
||||
$model = $this->getMockBuilder(EmailActivation::class)
|
||||
->setMethods(['generateKey', 'isKeyExists'])
|
||||
->getMock();
|
||||
|
||||
$model->expects($this->exactly(3))
|
||||
->method('generateKey')
|
||||
->will($this->onConsecutiveCalls('1', '2', '3'));
|
||||
|
||||
$model->expects($this->exactly(3))
|
||||
->method('isKeyExists')
|
||||
->will($this->onConsecutiveCalls(true, true, false));
|
||||
|
||||
$model->beforeSave(true);
|
||||
expect($model->key)->equals('3');
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user