forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheRateLimiterTest.php
More file actions
54 lines (44 loc) · 1.72 KB
/
CacheRateLimiterTest.php
File metadata and controls
54 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
use Mockery as m;
use Illuminate\Cache\RateLimiter;
use Illuminate\Contracts\Cache\Repository as Cache;
class CacheRateLimiterTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function testTooManyAttemptsReturnTrueIfAlreadyLockedOut()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('has')->once()->with('key:lockout')->andReturn(true);
$cache->shouldReceive('add')->never();
$rateLimiter = new RateLimiter($cache);
$this->assertTrue($rateLimiter->tooManyAttempts('key', 1, 1));
}
public function testTooManyAttemptsReturnsTrueIfMaxAttemptsExceeded()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('get')->once()->with('key', 0)->andReturn(10);
$cache->shouldReceive('has')->once()->with('key:lockout')->andReturn(false);
$cache->shouldReceive('add')->once()->with('key:lockout', m::type('int'), 1);
$rateLimiter = new RateLimiter($cache);
$this->assertTrue($rateLimiter->tooManyAttempts('key', 1, 1));
}
public function testHitProperlyIncrementsAttemptCount()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('add')->once()->with('key', 1, 1);
$cache->shouldReceive('increment')->once()->with('key');
$rateLimiter = new RateLimiter($cache);
$rateLimiter->hit('key', 1);
}
public function testClearClearsTheCacheKeys()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('forget')->once()->with('key');
$cache->shouldReceive('forget')->once()->with('key:lockout');
$rateLimiter = new RateLimiter($cache);
$rateLimiter->clear('key');
}
}