|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\RateLimiter; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Yiisoft\Cache\ArrayCache; |
|
7
|
|
|
use Yiisoft\Yii\Web\RateLimiter\CacheStorage; |
|
8
|
|
|
|
|
9
|
|
|
final class CacheStorageTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @test |
|
13
|
|
|
*/ |
|
14
|
|
|
public function getCounterNonExistValue(): void |
|
15
|
|
|
{ |
|
16
|
|
|
$storage = new CacheStorage(new ArrayCache()); |
|
17
|
|
|
$this->assertEquals(0, $storage->getCounterValue('test')); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @test |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getCounterExistValue(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$cache = new ArrayCache(); |
|
26
|
|
|
$cache->set('test', 100); |
|
27
|
|
|
|
|
28
|
|
|
$storage = new CacheStorage($cache); |
|
29
|
|
|
$this->assertEquals(100, $storage->getCounterValue('test')); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @test |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setCounterValue(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$cache = new ArrayCache(); |
|
38
|
|
|
$storage = new CacheStorage($cache); |
|
39
|
|
|
|
|
40
|
|
|
$storage->setCounterValue('test', 1000, 10); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals(1000, $cache->get('test')); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @test |
|
47
|
|
|
*/ |
|
48
|
|
|
public function hasCounterValueExist(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$cache = new ArrayCache(); |
|
51
|
|
|
$cache->set('test', 10); |
|
52
|
|
|
$storage = new CacheStorage($cache); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertTrue($storage->hasCounterValue('test')); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @test |
|
59
|
|
|
*/ |
|
60
|
|
|
public function hasCounterValueNotExist(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$storage = new CacheStorage(new ArrayCache()); |
|
63
|
|
|
$this->assertFalse($storage->hasCounterValue('test')); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|