1717
1818class FileLockResolverTest extends TestCase
1919{
20- public function test_prepare_path (): void
20+ protected function setUp (): void
21+ {
22+ [$ dir , $ defer ] = $ this ->prepareLockPath ();
23+
24+ $ this ->fileLocker = new FileLockResolver ($ dir );
25+ $ this ->defer = $ defer ;
26+ }
27+
28+ protected function tearDown (): void
2129 {
22- $ resolver = new FileLockResolver ();
23- $ this ->assertEquals (dirname (__DIR__ ).'/.locks/ ' , $ this ->invokeProperty ($ resolver , 'lockFileDir ' ));
30+ $ defer = $ this ->defer ;
31+ $ defer ();
32+ }
2433
34+ public function test_prepare_path (): void
35+ {
2536 $ this ->expectException (\Exception::class);
2637 $ this ->expectExceptionMessage (__FILE__ .' is not a directory. ' );
27- $ resolver = new FileLockResolver (__FILE__ );
38+ new FileLockResolver (__FILE__ );
2839 }
2940
3041 public function test_prepare_path_not_writable (): void
@@ -57,23 +68,21 @@ public function test_array_slice(): void
5768
5869 public function test_clean_old_sequence (): void
5970 {
60- $ resolver = new FileLockResolver ;
71+ $ resolver = $ this -> fileLocker ;
6172
6273 $ a = ['a ' => 1 , 'b ' => 2 , 'c ' => 3 , 'd ' => 4 , 'e ' => 5 , 'f ' => 6 ];
6374 $ d = $ resolver ->cleanOldSequences ($ a );
64-
6575 $ this ->assertEquals ($ a , $ d );
6676
6777 FileLockResolver::$ maxItems = 3 ;
68- $ resolver = new FileLockResolver ;
6978 $ d = $ resolver ->cleanOldSequences ($ a );
7079
7180 $ this ->assertEquals (['d ' => 4 , 'e ' => 5 , 'f ' => 6 ], $ d );
7281 }
7382
7483 public function test_increment_sequence_with_specify_time (): void
7584 {
76- $ resolver = new FileLockResolver ;
85+ $ resolver = $ this -> fileLocker ;
7786
7887 $ this ->assertEquals (['1 ' => 1 ], $ resolver ->incrementSequenceWithSpecifyTime ([], 1 ));
7988 $ this ->assertEquals (['a ' => 1 , '1 ' => 1 ], $ resolver ->incrementSequenceWithSpecifyTime (['a ' => 1 ], 1 ));
@@ -83,7 +92,7 @@ public function test_increment_sequence_with_specify_time(): void
8392
8493 public function test_get_contents_with_empty (): void
8594 {
86- $ resolver = new FileLockResolver ;
95+ $ resolver = $ this -> fileLocker ;
8796
8897 $ path = $ this ->touch ();
8998 $ f = fopen ($ path , FileLockResolver::FileOpenMode);
@@ -98,7 +107,7 @@ public function test_get_contents_with_empty(): void
98107
99108 public function test_get_contents_with_serialized_data (): void
100109 {
101- $ resolver = new FileLockResolver ;
110+ $ resolver = $ this -> fileLocker ;
102111 $ data = serialize (['a ' => 1 ]);
103112
104113 $ path = $ this ->touch ($ data );
@@ -114,7 +123,7 @@ public function test_get_contents_with_serialized_data(): void
114123
115124 public function test_get_contents_with_invalid_data (): void
116125 {
117- $ resolver = new FileLockResolver ;
126+ $ resolver = $ this -> fileLocker ;
118127
119128 $ path = $ this ->touch ('{"1":1} ' );
120129 $ f = fopen ($ path , FileLockResolver::FileOpenMode);
@@ -129,7 +138,7 @@ public function test_get_contents_with_invalid_data(): void
129138
130139 public function test_update_contents (): void
131140 {
132- $ resolver = new FileLockResolver ;
141+ $ resolver = $ this -> fileLocker ;
133142 $ path = $ this ->touch ();
134143 $ f = fopen ($ path , FileLockResolver::FileOpenMode);
135144
@@ -145,7 +154,7 @@ public function test_get_sequence_file_not_exists(): void
145154 $ path = 'a/b/c/d/e/f ' ;
146155
147156 $ time = 1 ;
148- $ resolver = new FileLockResolver ;
157+ $ resolver = $ this -> fileLocker ;
149158
150159 $ this ->expectException (\Exception::class);
151160 $ this ->invokeMethod ($ resolver , 'getSequence ' , [$ path , $ time ]);
@@ -157,7 +166,7 @@ public function test_get_sequence_file_cannot_open_file(): void
157166 chmod ($ path , 0444 );
158167
159168 $ time = 1 ;
160- $ resolver = new FileLockResolver ;
169+ $ resolver = $ this -> fileLocker ;
161170
162171 $ this ->expectException (\Exception::class);
163172 $ this ->expectExceptionMessage (sprintf ('can not open/lock this file %s ' , $ path ));
@@ -177,7 +186,7 @@ public function test_get_sequence_with_invalid_content(): void
177186 $ path = $ this ->touch ('x ' );
178187 $ time = 1 ;
179188
180- $ resolver = new FileLockResolver ;
189+ $ resolver = $ this -> fileLocker ;
181190
182191 $ this ->expectException (\Exception::class);
183192 $ this ->invokeMethod ($ resolver , 'getSequence ' , [$ path , $ time ]);
@@ -190,7 +199,7 @@ public function test_get_sequence(): void
190199 $ path = $ this ->touch ();
191200 $ time = 1 ;
192201
193- $ resolver = new FileLockResolver ;
202+ $ resolver = $ this -> fileLocker ;
194203
195204 $ this ->invokeMethod ($ resolver , 'getSequence ' , [$ path , $ time ]);
196205 $ this ->invokeMethod ($ resolver , 'getSequence ' , [$ path , $ time ]);
@@ -211,7 +220,7 @@ public function test_get_sequence(): void
211220
212221 public function test_update_contents_with_content (): void
213222 {
214- $ resolver = new FileLockResolver ;
223+ $ resolver = $ this -> fileLocker ;
215224 $ data = ['a ' => 1 , 'c ' => 3 ];
216225 $ path = $ this ->touch (serialize ($ data ));
217226
@@ -226,7 +235,7 @@ public function test_update_contents_with_content(): void
226235
227236 public function test_fnv (): void
228237 {
229- $ resolver = new FileLockResolver ;
238+ $ resolver = $ this -> fileLocker ;
230239 $ a = $ resolver ->fnv ('1674128900558 ' );
231240
232241 $ this ->assertSame (455874157.0 , $ a );
@@ -237,7 +246,7 @@ public function test_get_shard_lock_index(): void
237246 // reset
238247 FileLockResolver::$ shardCount = 1 ;
239248
240- $ resolver = new FileLockResolver ;
249+ $ resolver = $ this -> fileLocker ;
241250 $ index = $ resolver ->getShardLockIndex (1 );
242251 $ this ->assertTrue ($ index >= 0 && $ index < FileLockResolver::$ shardCount );
243252
@@ -249,7 +258,7 @@ public function test_get_shard_lock_index(): void
249258
250259 public function test_create_shard_lock_file_with_not_exists_path (): void
251260 {
252- $ resolver = new FileLockResolver ;
261+ $ resolver = $ this -> fileLocker ;
253262 $ index = 1 ;
254263
255264 $ path = $ this ->invokeMethod ($ resolver , 'createShardLockFile ' , [$ index ]);
@@ -261,7 +270,7 @@ public function test_create_shard_lock_file_with_not_exists_path(): void
261270
262271 public function test_create_shard_lock_file_with_exists_path (): void
263272 {
264- $ resolver = new FileLockResolver ;
273+ $ resolver = $ this -> fileLocker ;
265274 $ index = 1 ;
266275
267276 $ path = $ this ->invokeMethod ($ resolver , 'filePath ' , [$ index ]);
@@ -278,7 +287,7 @@ public function test_create_shard_lock_file_with_exists_path(): void
278287
279288 public function test_file_path (): void
280289 {
281- $ resolver = new FileLockResolver ;
290+ $ resolver = $ this -> fileLocker ;
282291 $ index = 1 ;
283292
284293 $ path = $ this ->invokeMethod ($ resolver , 'filePath ' , [$ index ]);
@@ -287,7 +296,7 @@ public function test_file_path(): void
287296
288297 public function test_sequence (): void
289298 {
290- $ resolver = new FileLockResolver ;
299+ $ resolver = $ this -> fileLocker ;
291300 $ resolver ->cleanAllLocksFile ();
292301
293302 $ this ->assertEquals (1 , $ resolver ->sequence (1 ));
@@ -304,7 +313,7 @@ public function test_sequence_with_max_items(): void
304313 FileLockResolver::$ shardCount = 1 ;
305314 FileLockResolver::$ maxItems = 3 ;
306315
307- $ resolver = new FileLockResolver ;
316+ $ resolver = $ this -> fileLocker ;
308317 $ resolver ->cleanAllLocksFile ();
309318
310319 $ this ->assertEquals (1 , $ resolver ->sequence (1 ));
@@ -319,7 +328,7 @@ public function test_sequence_with_max_items(): void
319328
320329 public function test_preg_match (): void
321330 {
322- $ resolver = new FileLockResolver ;
331+ $ resolver = $ this -> fileLocker ;
323332 $ index = 1 ;
324333 $ path = $ this ->invokeMethod ($ resolver , 'filePath ' , [$ index ]);
325334
@@ -332,7 +341,7 @@ public function test_preg_match(): void
332341 public function test_can_clean_lock_file ()
333342 {
334343 FileLockResolver::$ shardCount = 1 ;
335- $ fileResolver = new FileLockResolver ;
344+ $ fileResolver = $ this -> fileLocker ;
336345
337346 // this operation will generate a lock file
338347 $ fileResolver ->sequence (1 );
@@ -357,12 +366,21 @@ private function touch($content = '')
357366 return $ file ;
358367 }
359368
369+ private function prepareLockPath (): array
370+ {
371+ $ dir = dirname (__DIR__ ).'/.locks ' ;
372+ if (! is_dir ($ dir )) {
373+ mkdir ($ dir , 0777 );
374+ }
375+
376+ return [$ dir , fn () => rmdir ($ dir )];
377+ }
378+
360379 public static function tearDownAfterClass (): void
361380 {
362381 $ glob = dirname (__DIR__ ).'/.locks/* ' ;
363382 $ files = glob ($ glob );
364383 foreach ($ files as $ file ) {
365- var_dump ($ file );
366384 if (is_file ($ file )) {
367385 unlink ($ file );
368386 }
0 commit comments