@@ -94,12 +94,13 @@ protected function setUp(): void {
94
94
*
95
95
* @param bool $isLocked
96
96
* @param bool $lockDoesNotExist
97
- * @param string $token
97
+ * @param int $token
98
+ * @param bool $counter
98
99
* @param bool $expectNull
99
100
* @param bool $expectNewToken
100
101
* @param bool $expectOldToken
101
102
*/
102
- public function testLock (bool $ isLocked , bool $ lockDoesNotExist , string $ token , bool $ expectNull , bool $ expectNewToken , bool $ expectOldToken ): void {
103
+ public function testLock (bool $ isLocked , bool $ lockDoesNotExist , int $ counter , string $ token , bool $ expectNull , bool $ expectNewToken , bool $ expectOldToken ): void {
103
104
$ lockManager = $ this ->getMockBuilder (LockManager::class)
104
105
->setMethods (['isLocked ' ])
105
106
->setConstructorArgs ([
@@ -114,7 +115,7 @@ public function testLock(bool $isLocked, bool $lockDoesNotExist, string $token,
114
115
115
116
$ lockManager ->expects ($ this ->once ())
116
117
->method ('isLocked ' )
117
- ->with (42 , $ token )
118
+ ->with (42 , $ token, ' userId ' )
118
119
->willReturn ($ isLocked );
119
120
120
121
if (!$ isLocked ) {
@@ -135,31 +136,41 @@ public function testLock(bool $isLocked, bool $lockDoesNotExist, string $token,
135
136
}
136
137
137
138
if ($ expectNewToken ) {
138
- $ this ->secureRandom ->expects ($ this ->once ())
139
- ->method ('generate ' )
140
- ->with (64 , ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS )
141
- ->willReturn ('new-token ' );
142
-
143
- $ this ->timeFactory ->expects ($ this ->once ())
144
- ->method ('getTime ' )
145
- ->willReturn (1337 );
146
-
147
- $ this ->lockMapper ->expects ($ this ->once ())
148
- ->method ('insert ' )
149
- ->with ($ this ->callback (static function ($ lock ) {
150
- return ($ lock instanceof Lock &&
151
- $ lock ->getId () === 42 &&
152
- $ lock ->getTimestamp () === 1337 &&
153
- $ lock ->getToken () === 'new-token ' );
154
- }));
139
+ $ this ->metaDataStorage ->expects ($ this ->once ())
140
+ ->method ('getMetaData ' )
141
+ ->with ('userId ' , 42 )
142
+ ->willReturn ('{"counter": 0} ' );
143
+
144
+ if ($ counter > 0 ) {
145
+ $ this ->secureRandom ->expects ($ this ->once ())
146
+ ->method ('generate ' )
147
+ ->with (64 , ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS )
148
+ ->willReturn ('new-token ' );
149
+
150
+ $ this ->timeFactory ->expects ($ this ->once ())
151
+ ->method ('getTime ' )
152
+ ->willReturn (1337 );
153
+
154
+ $ this ->lockMapper ->expects ($ this ->once ())
155
+ ->method ('insert ' )
156
+ ->with ($ this ->callback (static function ($ lock ) {
157
+ return ($ lock instanceof Lock &&
158
+ $ lock ->getId () === 42 &&
159
+ $ lock ->getTimestamp () === 1337 &&
160
+ $ lock ->getToken () === 'new-token ' );
161
+ }));
162
+ } else {
163
+ $ this ->expectException (NotPermittedException::class);
164
+ $ this ->expectExceptionMessage ('Received counter is not greater than the stored one ' );
165
+ }
155
166
} else {
156
167
$ this ->secureRandom ->expects ($ this ->never ())
157
168
->method ('generate ' );
158
169
$ this ->timeFactory ->expects ($ this ->never ())
159
170
->method ('getTime ' );
160
171
}
161
172
162
- $ actual = $ lockManager ->lockFile (42 , $ token );
173
+ $ actual = $ lockManager ->lockFile (42 , $ token, $ counter , ' userId ' );
163
174
164
175
if ($ expectNull ) {
165
176
$ this ->assertNull ($ actual );
@@ -174,10 +185,11 @@ public function testLock(bool $isLocked, bool $lockDoesNotExist, string $token,
174
185
175
186
public function lockDataProvider (): array {
176
187
return [
177
- [true , false , 'correct-token123 ' , true , false , false ],
178
- [false , true , 'correct-token123 ' , false , true , false ],
179
- [false , false , 'correct-token123 ' , false , false , true ],
180
- [false , false , 'wrong-token456 ' , true , false , false ],
188
+ [true , false , 1 , 'correct-token123 ' , true , false , false ],
189
+ [false , true , 1 , 'correct-token123 ' , false , true , false ],
190
+ [false , true , 0 , 'correct-token123 ' , false , true , false ],
191
+ [false , false , 1 , 'correct-token123 ' , false , false , true ],
192
+ [false , false , 1 , 'wrong-token456 ' , true , false , false ],
181
193
];
182
194
}
183
195
0 commit comments