Passed
Push — master ( 81ac1b...8d070d )
by Alexander
07:32
created

CounterTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 8
1
<?php
2
3
namespace Yiisoft\Yii\RateLimiter\Tests;
4
5
use InvalidArgumentException;
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\Cache\ArrayCache;
8
use Yiisoft\Yii\RateLimiter\Counter;
9
10
final class CounterTest extends TestCase
11
{
12
    public function testStatisticsShouldBeCorrectWhenLimitIsNotReached(): void
13
    {
14
        $counter = new Counter(2, 5, new ArrayCache());
15
        $counter->setId('key');
16
17
        $statistics = $counter->incrementAndGetState();
18
        $this->assertEquals(2, $statistics->getLimit());
19
        $this->assertEquals(1, $statistics->getRemaining());
20
        $this->assertGreaterThanOrEqual(time(), $statistics->getResetTime());
21
        $this->assertFalse($statistics->isLimitReached());
22
    }
23
24
    public function testStatisticsShouldBeCorrectWhenLimitIsReached(): void
25
    {
26
        $counter = new Counter(2, 4, new ArrayCache());
27
        $counter->setId('key');
28
29
        $statistics = $counter->incrementAndGetState();
30
        $this->assertEquals(2, $statistics->getLimit());
31
        $this->assertEquals(1, $statistics->getRemaining());
32
        $this->assertGreaterThanOrEqual(time(), $statistics->getResetTime());
33
        $this->assertFalse($statistics->isLimitReached());
34
35
        $statistics = $counter->incrementAndGetState();
36
        $this->assertEquals(2, $statistics->getLimit());
37
        $this->assertEquals(0, $statistics->getRemaining());
38
        $this->assertGreaterThanOrEqual(time(), $statistics->getResetTime());
39
        $this->assertTrue($statistics->isLimitReached());
40
    }
41
42
    public function testShouldNotBeAbleToSetInvalidId(): void
43
    {
44
        $this->expectException(\LogicException::class);
45
        (new Counter(10, 60, new ArrayCache()))->incrementAndGetState();
46
    }
47
48
    public function testShouldNotBeAbleToSetInvalidLimit(): void
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
        new Counter(0, 60, new ArrayCache());
52
    }
53
54
    public function testShouldNotBeAbleToSetInvalidPeriod(): void
55
    {
56
        $this->expectException(InvalidArgumentException::class);
57
        new Counter(10, 0, new ArrayCache());
58
    }
59
60
    public function testIncrementMustBeUniformAfterLimitIsReached(): void
61
    {
62
        $counter = new Counter(10, 1, new ArrayCache());
63
        $counter->setId('key');
64
65
        for ($i = 0; $i < 10; $i++) {
66
            $counter->incrementAndGetState();
67
        }
68
69
        for ($i = 0; $i < 5; $i++) {
70
            usleep(110000); // period(microseconds) / limit + 10ms(cost work)
71
            $statistics = $counter->incrementAndGetState();
72
            $this->assertEquals(1, $statistics->getRemaining());
73
        }
74
    }
75
}
76