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
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testInstantiate(int $type, string $expectedClassType): void {
|
2019-12-21 03:53:58 +05:30
|
|
|
$this->assertInstanceOf($expectedClassType, EmailActivation::findOne(['type' => $type]));
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getInstantiateTestCases(): iterable {
|
2019-12-21 03:53:58 +05:30
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testCanResend(): void {
|
2019-12-21 03:53:58 +05:30
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testCanResendWithNullTimeout(): void {
|
2019-12-21 03:53:58 +05:30
|
|
|
$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
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testIsStale(): void {
|
2019-12-21 03:53:58 +05:30
|
|
|
$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
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testIsStaleWithNullDuration(): void {
|
2019-12-21 03:53:58 +05:30
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
}
|