2016-05-17 20:32:15 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\behaviors;
|
2016-05-17 20:32:15 +05:30
|
|
|
|
|
|
|
use Codeception\Specify;
|
|
|
|
use common\behaviors\EmailActivationExpirationBehavior;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\_support\ProtectedCaller;
|
|
|
|
use common\tests\unit\TestCase;
|
2016-05-17 20:32:15 +05:30
|
|
|
use yii\base\Model;
|
|
|
|
|
|
|
|
class EmailActivationExpirationBehaviorTest extends TestCase {
|
|
|
|
use Specify;
|
|
|
|
use ProtectedCaller;
|
|
|
|
|
|
|
|
public function testCalculateTime() {
|
2016-10-29 05:53:29 +05:30
|
|
|
$behavior = $this->createBehavior();
|
|
|
|
$time = time();
|
|
|
|
$behavior->owner->created_at = $time;
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame($time + 10, $this->callProtected($behavior, 'calculateTime', 10));
|
2016-05-17 20:32:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompareTime() {
|
|
|
|
$this->specify('expect false, if passed value is less then 0', function() {
|
|
|
|
$behavior = $this->createBehavior();
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertFalse($this->callProtected($behavior, 'compareTime', -1));
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('expect true, if passed value is equals 0', function() {
|
|
|
|
$behavior = $this->createBehavior();
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertTrue($this->callProtected($behavior, 'compareTime', 0));
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertTrue($this->callProtected($behavior, 'compareTime', 5));
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertFalse($this->callProtected($behavior, 'compareTime', 7));
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertTrue($behavior->canRepeat());
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertFalse($behavior->canRepeat());
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertTrue($behavior->isExpired());
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertFalse($behavior->isExpired());
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame($behavior->owner->created_at + $behavior->repeatTimeout, $behavior->canRepeatIn());
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame($behavior->owner->created_at + $behavior->expirationTimeout, $behavior->expireIn());
|
2016-05-17 20:32:15 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|