@@ -76,4 +76,53 @@ public function testAvailableInReturnsPositiveValues()
76
76
$ this ->assertTrue ($ rateLimiter ->availableIn ('key:timer ' ) >= 0 );
77
77
$ this ->assertTrue ($ rateLimiter ->availableIn ('key:timer ' ) >= 0 );
78
78
}
79
+
80
+ public function testAttemptsCallbackReturnsTrue ()
81
+ {
82
+ $ cache = m::mock (Cache::class);
83
+ $ cache ->shouldReceive ('get ' )->once ()->with ('key ' , 0 )->andReturn (0 );
84
+ $ cache ->shouldReceive ('add ' )->once ()->with ('key:timer ' , m::type ('int ' ), 1 );
85
+ $ cache ->shouldReceive ('add ' )->once ()->with ('key ' , 0 , 1 )->andReturns (1 );
86
+ $ cache ->shouldReceive ('increment ' )->once ()->with ('key ' )->andReturn (1 );
87
+
88
+ $ executed = false ;
89
+
90
+ $ rateLimiter = new RateLimiter ($ cache );
91
+
92
+ $ this ->assertTrue ($ rateLimiter ->attempt ('key ' , 1 , function () use (&$ executed ) {
93
+ $ executed = true ;
94
+ }, 1 ));
95
+ $ this ->assertTrue ($ executed );
96
+ }
97
+
98
+ public function testAttemptsCallbackReturnsCallbackReturn ()
99
+ {
100
+ $ cache = m::mock (Cache::class);
101
+ $ cache ->shouldReceive ('get ' )->once ()->with ('key ' , 0 )->andReturn (0 );
102
+ $ cache ->shouldReceive ('add ' )->once ()->with ('key:timer ' , m::type ('int ' ), 1 );
103
+ $ cache ->shouldReceive ('add ' )->once ()->with ('key ' , 0 , 1 )->andReturns (1 );
104
+ $ cache ->shouldReceive ('increment ' )->once ()->with ('key ' )->andReturn (1 );
105
+
106
+ $ rateLimiter = new RateLimiter ($ cache );
107
+
108
+ $ this ->assertEquals ('foo ' , $ rateLimiter ->attempt ('key ' , 1 , function () {
109
+ return 'foo ' ;
110
+ }, 1 ));
111
+ }
112
+
113
+ public function testAttemptsCallbackReturnsFalse ()
114
+ {
115
+ $ cache = m::mock (Cache::class);
116
+ $ cache ->shouldReceive ('get ' )->once ()->with ('key ' , 0 )->andReturn (2 );
117
+ $ cache ->shouldReceive ('has ' )->once ()->with ('key:timer ' )->andReturn (true );
118
+
119
+ $ executed = false ;
120
+
121
+ $ rateLimiter = new RateLimiter ($ cache );
122
+
123
+ $ this ->assertFalse ($ rateLimiter ->attempt ('key ' , 1 , function () use (&$ executed ) {
124
+ $ executed = true ;
125
+ }, 1 ));
126
+ $ this ->assertFalse ($ executed );
127
+ }
79
128
}
0 commit comments