Passed
Pull Request — master (#204)
by
unknown
02:16
created

CacheCounterTest::limitNotExhausted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\RateLimiter;
4
5
use RuntimeException;
6
use InvalidArgumentException;
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Cache\ArrayCache;
9
use Yiisoft\Yii\Web\RateLimiter\CacheCounter;
10
11
final class CacheCounterTest extends TestCase
12
{
13
    /**
14
     * @test
15
     */
16
    public function limitNotExhausted(): void
17
    {
18
        $counter = new CacheCounter(2, 5, new ArrayCache());
19
        $counter->setId('key');
20
21
        $this->assertFalse($counter->limitIsReached());
22
    }
23
24
    /**
25
     * @test
26
     */
27
    public function limitIsExhausted(): void
28
    {
29
        $cache = new ArrayCache();
30
        $cache->set('key', time() + 55);
31
32
        $counter = new CacheCounter(10, 60, $cache);
33
        $counter->setId('key');
34
35
        $this->assertTrue($counter->limitIsReached());
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function invalidIdArgument(): void
42
    {
43
        $this->expectException(RuntimeException::class);
44
        (new CacheCounter(10, 60, new ArrayCache()))->limitIsReached();
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function invalidLimitArgument(): void
51
    {
52
        $this->expectException(InvalidArgumentException::class);
53
        $counter = new CacheCounter(0, 60, new ArrayCache());
54
        $counter->setId('key');
55
        $counter->limitIsReached();
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function invalidPeriodArgument(): void
62
    {
63
        $this->expectException(InvalidArgumentException::class);
64
        $counter = new CacheCounter(10, 0, new ArrayCache());
65
        $counter->setId('key');
66
        $counter->limitIsReached();
67
    }
68
}
69