2
2
3
3
namespace Illuminate \Tests \Cache ;
4
4
5
+ use Illuminate \Cache \ArrayStore ;
5
6
use Illuminate \Cache \RateLimiter ;
6
7
use Illuminate \Contracts \Cache \Repository as Cache ;
7
8
use Mockery as m ;
@@ -20,6 +21,7 @@ public function testTooManyAttemptsReturnTrueIfAlreadyLockedOut()
20
21
$ cache ->shouldReceive ('get ' )->once ()->with ('key ' , 0 )->andReturn (1 );
21
22
$ cache ->shouldReceive ('has ' )->once ()->with ('key:timer ' )->andReturn (true );
22
23
$ cache ->shouldReceive ('add ' )->never ();
24
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
23
25
$ rateLimiter = new RateLimiter ($ cache );
24
26
25
27
$ this ->assertTrue ($ rateLimiter ->tooManyAttempts ('key ' , 1 ));
@@ -31,6 +33,7 @@ public function testHitProperlyIncrementsAttemptCount()
31
33
$ cache ->shouldReceive ('add ' )->once ()->with ('key:timer ' , m::type ('int ' ), 1 )->andReturn (true );
32
34
$ cache ->shouldReceive ('add ' )->once ()->with ('key ' , 0 , 1 )->andReturn (true );
33
35
$ cache ->shouldReceive ('increment ' )->once ()->with ('key ' , 1 )->andReturn (1 );
36
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
34
37
$ rateLimiter = new RateLimiter ($ cache );
35
38
36
39
$ rateLimiter ->hit ('key ' , 1 );
@@ -42,6 +45,7 @@ public function testIncrementProperlyIncrementsAttemptCount()
42
45
$ cache ->shouldReceive ('add ' )->once ()->with ('key:timer ' , m::type ('int ' ), 1 )->andReturn (true );
43
46
$ cache ->shouldReceive ('add ' )->once ()->with ('key ' , 0 , 1 )->andReturn (true );
44
47
$ cache ->shouldReceive ('increment ' )->once ()->with ('key ' , 5 )->andReturn (5 );
48
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
45
49
$ rateLimiter = new RateLimiter ($ cache );
46
50
47
51
$ rateLimiter ->increment ('key ' , 1 , 5 );
@@ -53,6 +57,7 @@ public function testDecrementProperlyDecrementsAttemptCount()
53
57
$ cache ->shouldReceive ('add ' )->once ()->with ('key:timer ' , m::type ('int ' ), 1 )->andReturn (true );
54
58
$ cache ->shouldReceive ('add ' )->once ()->with ('key ' , 0 , 1 )->andReturn (true );
55
59
$ cache ->shouldReceive ('increment ' )->once ()->with ('key ' , -5 )->andReturn (-5 );
60
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
56
61
$ rateLimiter = new RateLimiter ($ cache );
57
62
58
63
$ rateLimiter ->decrement ('key ' , 1 , 5 );
@@ -65,6 +70,7 @@ public function testHitHasNoMemoryLeak()
65
70
$ cache ->shouldReceive ('add ' )->once ()->with ('key ' , 0 , 1 )->andReturn (false );
66
71
$ cache ->shouldReceive ('increment ' )->once ()->with ('key ' , 1 )->andReturn (1 );
67
72
$ cache ->shouldReceive ('put ' )->once ()->with ('key ' , 1 , 1 );
73
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
68
74
$ rateLimiter = new RateLimiter ($ cache );
69
75
70
76
$ rateLimiter ->hit ('key ' , 1 );
@@ -74,6 +80,7 @@ public function testRetriesLeftReturnsCorrectCount()
74
80
{
75
81
$ cache = m::mock (Cache::class);
76
82
$ cache ->shouldReceive ('get ' )->once ()->with ('key ' , 0 )->andReturn (3 );
83
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
77
84
$ rateLimiter = new RateLimiter ($ cache );
78
85
79
86
$ this ->assertEquals (2 , $ rateLimiter ->retriesLeft ('key ' , 5 ));
@@ -84,6 +91,7 @@ public function testClearClearsTheCacheKeys()
84
91
$ cache = m::mock (Cache::class);
85
92
$ cache ->shouldReceive ('forget ' )->once ()->with ('key ' );
86
93
$ cache ->shouldReceive ('forget ' )->once ()->with ('key:timer ' );
94
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
87
95
$ rateLimiter = new RateLimiter ($ cache );
88
96
89
97
$ rateLimiter ->clear ('key ' );
@@ -93,6 +101,7 @@ public function testAvailableInReturnsPositiveValues()
93
101
{
94
102
$ cache = m::mock (Cache::class);
95
103
$ cache ->shouldReceive ('get ' )->andReturn (now ()->subSeconds (60 )->getTimestamp (), null );
104
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
96
105
$ rateLimiter = new RateLimiter ($ cache );
97
106
98
107
$ this ->assertTrue ($ rateLimiter ->availableIn ('key:timer ' ) >= 0 );
@@ -106,6 +115,7 @@ public function testAttemptsCallbackReturnsTrue()
106
115
$ cache ->shouldReceive ('add ' )->once ()->with ('key:timer ' , m::type ('int ' ), 1 );
107
116
$ cache ->shouldReceive ('add ' )->once ()->with ('key ' , 0 , 1 )->andReturns (1 );
108
117
$ cache ->shouldReceive ('increment ' )->once ()->with ('key ' , 1 )->andReturn (1 );
118
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
109
119
110
120
$ executed = false ;
111
121
@@ -124,6 +134,7 @@ public function testAttemptsCallbackReturnsCallbackReturn()
124
134
$ cache ->shouldReceive ('add ' )->times (6 )->with ('key:timer ' , m::type ('int ' ), 1 );
125
135
$ cache ->shouldReceive ('add ' )->times (6 )->with ('key ' , 0 , 1 )->andReturns (1 );
126
136
$ cache ->shouldReceive ('increment ' )->times (6 )->with ('key ' , 1 )->andReturn (1 );
137
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
127
138
128
139
$ rateLimiter = new RateLimiter ($ cache );
129
140
@@ -157,6 +168,7 @@ public function testAttemptsCallbackReturnsFalse()
157
168
$ cache = m::mock (Cache::class);
158
169
$ cache ->shouldReceive ('get ' )->once ()->with ('key ' , 0 )->andReturn (2 );
159
170
$ cache ->shouldReceive ('has ' )->once ()->with ('key:timer ' )->andReturn (true );
171
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
160
172
161
173
$ executed = false ;
162
174
@@ -174,6 +186,7 @@ public function testKeysAreSanitizedFromUnicodeCharacters()
174
186
$ cache ->shouldReceive ('get ' )->once ()->with ('john ' , 0 )->andReturn (1 );
175
187
$ cache ->shouldReceive ('has ' )->once ()->with ('john:timer ' )->andReturn (true );
176
188
$ cache ->shouldReceive ('add ' )->never ();
189
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
177
190
$ rateLimiter = new RateLimiter ($ cache );
178
191
179
192
$ this ->assertTrue ($ rateLimiter ->tooManyAttempts ('jôhn ' , 1 ));
@@ -190,6 +203,7 @@ public function testKeyIsSanitizedOnlyOnce()
190
203
$ cache ->shouldReceive ('get ' )->once ()->with ($ cleanedKey , 0 )->andReturn (1 );
191
204
$ cache ->shouldReceive ('has ' )->once ()->with ("$ cleanedKey:timer " )->andReturn (true );
192
205
$ cache ->shouldReceive ('add ' )->never ();
206
+ $ cache ->shouldReceive ('getStore ' )->andReturn (new ArrayStore );
193
207
194
208
$ this ->assertTrue ($ rateLimiter ->tooManyAttempts ($ key , 1 ));
195
209
}
0 commit comments