2016-05-11 01:10:06 +05:30
|
|
|
<?php
|
2019-12-21 03:53:58 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\models;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
use Carbon\Carbon;
|
2016-10-29 05:53:29 +05:30
|
|
|
use common\models\confirmations;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\models\EmailActivation;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\EmailActivationFixture;
|
|
|
|
use common\tests\unit\TestCase;
|
2019-12-21 03:53:58 +05:30
|
|
|
use DateInterval;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
2016-10-29 05:53:29 +05:30
|
|
|
class EmailActivationTest extends TestCase {
|
2016-05-11 01:10:06 +05:30
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-05-11 01:10:06 +05:30
|
|
|
return [
|
2016-05-16 04:03:19 +05:30
|
|
|
'emailActivations' => EmailActivationFixture::class,
|
2016-05-11 01:10:06 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
/**
|
|
|
|
* @dataProvider getInstantiateTestCases
|
|
|
|
*/
|
|
|
|
public function testInstantiate(int $type, string $expectedClassType) {
|
|
|
|
$this->assertInstanceOf($expectedClassType, EmailActivation::findOne(['type' => $type]));
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanResend() {
|
|
|
|
$model = $this->createPartialMock(EmailActivation::class, ['getResendTimeout']);
|
|
|
|
$model->method('getResendTimeout')->willReturn(new DateInterval('PT10M'));
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
2016-10-29 05:53:29 +05:30
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
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());
|
|
|
|
}
|
2016-10-29 05:53:29 +05:30
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
public function testIsStaleWithNullDuration() {
|
|
|
|
$model = $this->createPartialMock(EmailActivation::class, ['getExpireDuration']);
|
|
|
|
$model->method('getExpireDuration')->willReturn(null);
|
2016-10-29 05:53:29 +05:30
|
|
|
|
2019-12-21 03:53:58 +05:30
|
|
|
$model->created_at = time();
|
|
|
|
$this->assertFalse($model->isStale());
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|