Rework email_activation model, get rid of behaviors, use json column to store additional data

This commit is contained in:
ErickSkrauch
2019-12-21 01:23:58 +03:00
parent 22e8158581
commit 666213afc7
26 changed files with 254 additions and 454 deletions

View File

@@ -1,75 +0,0 @@
<?php
namespace common\tests\unit\behaviors;
use Codeception\Specify;
use common\behaviors\DataBehavior;
use common\tests\_support\ProtectedCaller;
use common\tests\unit\TestCase;
use yii\base\ErrorException;
use yii\base\Model;
class DataBehaviorTest extends TestCase {
use Specify;
use ProtectedCaller;
public function testSetKey() {
$model = $this->createModel();
/** @var DataBehavior $behavior */
$behavior = $model->behaviors['dataBehavior'];
$this->callProtected($behavior, 'setKey', 'my-key', 'my-value');
$this->assertSame(serialize(['my-key' => 'my-value']), $model->_data);
}
public function testGetKey() {
$model = $this->createModel();
$model->_data = serialize(['some-key' => 'some-value']);
/** @var DataBehavior $behavior */
$behavior = $model->behaviors['dataBehavior'];
$this->assertSame('some-value', $this->callProtected($behavior, 'getKey', 'some-key'));
}
public function testGetData() {
$this->specify('getting value from null field should return empty array', function() {
$model = $this->createModel();
/** @var DataBehavior $behavior */
$behavior = $model->behaviors['dataBehavior'];
$this->assertSame([], $this->callProtected($behavior, 'getData'));
});
$this->specify('getting value from serialized data field should return encoded value', function() {
$model = $this->createModel();
$data = ['foo' => 'bar'];
$model->_data = serialize($data);
/** @var DataBehavior $behavior */
$behavior = $model->behaviors['dataBehavior'];
$this->assertSame($data, $this->callProtected($behavior, 'getData'));
});
$this->specify('getting value from invalid serialization string', function() {
$model = $this->createModel();
$model->_data = 'this is invalid serialization of string';
/** @var DataBehavior $behavior */
$behavior = $model->behaviors['dataBehavior'];
$this->expectException(ErrorException::class);
$this->callProtected($behavior, 'getData');
});
}
/**
* @return Model
*/
private function createModel() {
return new class extends Model {
public $_data;
public function behaviors() {
return [
'dataBehavior' => [
'class' => DataBehavior::class,
],
];
}
};
}
}

View File

@@ -1,109 +0,0 @@
<?php
namespace common\tests\unit\behaviors;
use Codeception\Specify;
use common\behaviors\EmailActivationExpirationBehavior;
use common\tests\_support\ProtectedCaller;
use common\tests\unit\TestCase;
use yii\base\Model;
class EmailActivationExpirationBehaviorTest extends TestCase {
use Specify;
use ProtectedCaller;
public function testCalculateTime() {
$behavior = $this->createBehavior();
$time = time();
$behavior->owner->created_at = $time;
$this->assertSame($time + 10, $this->callProtected($behavior, 'calculateTime', 10));
}
public function testCompareTime() {
$this->specify('expect false, if passed value is less then 0', function() {
$behavior = $this->createBehavior();
$this->assertFalse($this->callProtected($behavior, 'compareTime', -1));
});
$this->specify('expect true, if passed value is equals 0', function() {
$behavior = $this->createBehavior();
$this->assertTrue($this->callProtected($behavior, 'compareTime', 0));
});
$this->specify('expect true, if passed value is more than 0 and current time is greater then calculated', function() {
$behavior = $this->createBehavior();
$behavior->owner->created_at = time() - 10;
$this->assertTrue($this->callProtected($behavior, 'compareTime', 5));
});
$this->specify('expect false, if passed value is more than 0 and current time is less then calculated', function() {
$behavior = $this->createBehavior();
$behavior->owner->created_at = time() - 2;
$this->assertFalse($this->callProtected($behavior, 'compareTime', 7));
});
}
public function testCanRepeat() {
$this->specify('we can repeat, if created_at + repeatTimeout is greater, then current time', function() {
$behavior = $this->createBehavior();
$behavior->repeatTimeout = 30;
$behavior->owner->created_at = time() - 60;
$this->assertTrue($behavior->canRepeat());
});
$this->specify('we cannot repeat, if created_at + repeatTimeout is less, then current time', function() {
$behavior = $this->createBehavior();
$behavior->repeatTimeout = 60;
$behavior->owner->created_at = time() - 30;
$this->assertFalse($behavior->canRepeat());
});
}
public function testIsExpired() {
$this->specify('key is not expired, if created_at + expirationTimeout is greater, then current time', function() {
$behavior = $this->createBehavior();
$behavior->expirationTimeout = 30;
$behavior->owner->created_at = time() - 60;
$this->assertTrue($behavior->isExpired());
});
$this->specify('key is not expired, if created_at + expirationTimeout is less, then current time', function() {
$behavior = $this->createBehavior();
$behavior->expirationTimeout = 60;
$behavior->owner->created_at = time() - 30;
$this->assertFalse($behavior->isExpired());
});
}
public function testCanRepeatIn() {
$this->specify('get expected timestamp for repeat time moment', function() {
$behavior = $this->createBehavior();
$behavior->repeatTimeout = 30;
$behavior->owner->created_at = time() - 60;
$this->assertSame($behavior->owner->created_at + $behavior->repeatTimeout, $behavior->canRepeatIn());
});
}
public function testExpireIn() {
$this->specify('get expected timestamp for key expire moment', function() {
$behavior = $this->createBehavior();
$behavior->expirationTimeout = 30;
$behavior->owner->created_at = time() - 60;
$this->assertSame($behavior->owner->created_at + $behavior->expirationTimeout, $behavior->expireIn());
});
}
/**
* @return EmailActivationExpirationBehavior
*/
private function createBehavior() {
$behavior = new EmailActivationExpirationBehavior();
/** @var Model $model */
$model = new class extends Model {
public $created_at;
};
$model->attachBehavior('email-activation-behavior', $behavior);
return $behavior;
}
}

View File

@@ -1,10 +1,14 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\models;
use Carbon\Carbon;
use common\models\confirmations;
use common\models\EmailActivation;
use common\tests\fixtures\EmailActivationFixture;
use common\tests\unit\TestCase;
use DateInterval;
class EmailActivationTest extends TestCase {
@@ -14,22 +18,59 @@ class EmailActivationTest extends TestCase {
];
}
public function testInstantiate() {
$this->assertInstanceOf(confirmations\RegistrationConfirmation::class, EmailActivation::findOne([
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
]));
/**
* @dataProvider getInstantiateTestCases
*/
public function testInstantiate(int $type, string $expectedClassType) {
$this->assertInstanceOf($expectedClassType, EmailActivation::findOne(['type' => $type]));
}
$this->assertInstanceOf(confirmations\ForgotPassword::class, EmailActivation::findOne([
'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
]));
public function getInstantiateTestCases() {
yield [EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION, confirmations\RegistrationConfirmation::class];
yield [EmailActivation::TYPE_FORGOT_PASSWORD_KEY, confirmations\ForgotPassword::class];
yield [EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION, confirmations\CurrentEmailConfirmation::class];
yield [EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION, confirmations\NewEmailConfirmation::class];
}
$this->assertInstanceOf(confirmations\CurrentEmailConfirmation::class, EmailActivation::findOne([
'type' => EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
]));
public function testCanResend() {
$model = $this->createPartialMock(EmailActivation::class, ['getResendTimeout']);
$model->method('getResendTimeout')->willReturn(new DateInterval('PT10M'));
$this->assertInstanceOf(confirmations\NewEmailConfirmation::class, EmailActivation::findOne([
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
]));
$model->created_at = time();
$this->assertFalse($model->canResend());
$this->assertEqualsWithDelta(Carbon::now()->addMinutes(10), $model->canResendAt(), 3);
$model->created_at = time() - 60 * 10 - 1;
$this->assertTrue($model->canResend());
$this->assertEqualsWithDelta(Carbon::now()->subSecond(), $model->canResendAt(), 3);
}
public function testCanResendWithNullTimeout() {
$model = $this->createPartialMock(EmailActivation::class, ['getResendTimeout']);
$model->method('getResendTimeout')->willReturn(null);
$model->created_at = time();
$this->assertTrue($model->canResend());
$this->assertEqualsWithDelta(Carbon::now(), $model->canResendAt(), 3);
}
public function testIsStale() {
$model = $this->createPartialMock(EmailActivation::class, ['getExpireDuration']);
$model->method('getExpireDuration')->willReturn(new DateInterval('PT10M'));
$model->created_at = time();
$this->assertFalse($model->isStale());
$model->created_at = time() - 60 * 10 - 1;
$this->assertTrue($model->isStale());
}
public function testIsStaleWithNullDuration() {
$model = $this->createPartialMock(EmailActivation::class, ['getExpireDuration']);
$model->method('getExpireDuration')->willReturn(null);
$model->created_at = time();
$this->assertFalse($model->isStale());
}
}