Passed
Pull Request — master (#204)
by
unknown
01:59
created

CounterTest::shouldNotBeAbleToSetInvalidPeriod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\RateLimiter;
4
5
use InvalidArgumentException;
6
use PHPUnit\Framework\TestCase;
7
use Yiisoft\Cache\ArrayCache;
8
use Yiisoft\Yii\Web\RateLimiter\Counter;
9
10
final class CounterTest extends TestCase
11
{
12
    /**
13
     * @test
14
     */
15
    public function statisticsShouldBeCorrectWhenLimitIsNotReached(): void
16
    {
17
        $counter = new Counter(2, 5, new ArrayCache());
18
        $counter->setId('key');
19
20
        $statistics = $counter->incrementAndGetResult();
21
        $this->assertEquals(2, $statistics->getLimit());
22
        $this->assertEquals(1, $statistics->getRemaining());
23
        $this->assertGreaterThanOrEqual(time(), $statistics->getResetTime());
24
        $this->assertFalse($statistics->isLimitReached());
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function statisticsShouldBeCorrectWhenLimitIsReached(): void
31
    {
32
        $counter = new Counter(2, 4, new ArrayCache());
33
        $counter->setId('key');
34
35
        $statistics = $counter->incrementAndGetResult();
36
        $this->assertEquals(2, $statistics->getLimit());
37
        $this->assertEquals(1, $statistics->getRemaining());
38
        $this->assertGreaterThanOrEqual(time(), $statistics->getResetTime());
39
        $this->assertFalse($statistics->isLimitReached());
40
41
        $statistics = $counter->incrementAndGetResult();
42
        $this->assertEquals(2, $statistics->getLimit());
43
        $this->assertEquals(0, $statistics->getRemaining());
44
        $this->assertGreaterThanOrEqual(time(), $statistics->getResetTime());
45
        $this->assertTrue($statistics->isLimitReached());
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function shouldNotBeAbleToSetInvalidId(): void
52
    {
53
        $this->expectException(\LogicException::class);
54
        (new Counter(10, 60, new ArrayCache()))->incrementAndGetResult();
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function shouldNotBeAbleToSetInvalidLimit(): void
61
    {
62
        $this->expectException(InvalidArgumentException::class);
63
        new Counter(0, 60, new ArrayCache());
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function shouldNotBeAbleToSetInvalidPeriod(): void
70
    {
71
        $this->expectException(InvalidArgumentException::class);
72
        new Counter(10, 0, new ArrayCache());
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function incrementMustBeUniformAfterLimitIsReached(): void
79
    {
80
        $counter = new Counter(10, 1, new ArrayCache());
81
        $counter->setId('key');
82
83
        for ($i = 0; $i < 10; $i++) {
84
            $counter->incrementAndGetResult();
85
        }
86
87
        for ($i = 0; $i < 5; $i++) {
88
            usleep(110000); // period(microseconds) / limit + 10ms(cost work)
89
            $statistics = $counter->incrementAndGetResult();
90
            $this->assertEquals(1, $statistics->getRemaining());
91
        }
92
    }
93
}
94