1717
1818class FileLockResolverTest extends TestCase
1919{
20+ private static string $ mainLockFileDirPath ;
21+ private static string $ unWriteableFileDirPath ;
22+
2023 private FileLockResolver $ fileLocker ;
2124
22- /** @var callable */
23- private $ defer ;
25+ public static function setUpBeforeClass (): void
26+ {
27+ self ::$ mainLockFileDirPath = dirname (__DIR__ ) . '/.locks ' ;
28+ self ::$ unWriteableFileDirPath = __DIR__ . '/.locks ' ;
29+ }
2430
2531 protected function setUp (): void
2632 {
27- [$ dir , $ defer ] = $ this ->prepareLockPath ();
33+ mkdir (self ::$ mainLockFileDirPath , 0777 );
34+ mkdir (self ::$ unWriteableFileDirPath , 0444 );
2835
29- $ this ->fileLocker = new FileLockResolver ($ dir );
30- $ this ->defer = $ defer ;
36+ $ this ->fileLocker = new FileLockResolver (self ::$ mainLockFileDirPath );
3137 }
3238
3339 protected function tearDown (): void
3440 {
35- $ defer = $ this ->defer ;
36- $ defer ();
41+ $ this ->cleanUpLockFileDirs ();
3742 }
3843
3944 public function test_prepare_path (): void
@@ -48,16 +53,9 @@ public function test_prepare_path_not_writable(): void
4853 $ resolver = new FileLockResolver ('/tmp/ ' );
4954 $ this ->assertEquals ('/tmp/ ' , $ this ->invokeProperty ($ resolver , 'lockFileDir ' ));
5055
51- $ dir = __DIR__ .'/.locks/ ' ;
52- if (! is_dir ($ dir )) {
53- mkdir ($ dir , 0444 );
54- }
55-
5656 $ this ->expectException (\Exception::class);
57- $ this ->expectExceptionMessage ($ dir .' is not writable. ' );
58- $ resolver = new FileLockResolver ($ dir );
59-
60- rmdir ($ dir );
57+ $ this ->expectExceptionMessage (self ::$ unWriteableFileDirPath .' is not writable. ' );
58+ $ resolver = new FileLockResolver (self ::$ unWriteableFileDirPath );
6159 }
6260
6361 public function test_array_slice (): void
@@ -343,7 +341,7 @@ public function test_preg_match(): void
343341 /**
344342 * @throws SnowflakeException
345343 */
346- public function test_can_clean_lock_file ()
344+ public function test_can_clean_lock_file (): void
347345 {
348346 FileLockResolver::$ shardCount = 1 ;
349347 $ fileResolver = $ this ->fileLocker ;
@@ -360,34 +358,32 @@ public function test_can_clean_lock_file()
360358 $ this ->assertFileDoesNotExist ($ path );
361359 }
362360
363- private function touch ($ content = '' )
361+ private function touch (string $ content = '' ): string
364362 {
365- $ file = tempnam (dirname ( __DIR__ ). ' /.locks ' , 'snowflake ' );
363+ $ file = tempnam (self :: $ mainLockFileDirPath , 'snowflake ' );
366364
367- if ($ content ) {
365+ if ($ file === false ) {
366+ throw new \RuntimeException ('Unable to create file ' );
367+ }
368+
369+ if ($ content !== '' ) {
368370 file_put_contents ($ file , $ content );
369371 }
370372
371373 return $ file ;
372374 }
373375
374- private function prepareLockPath (): array
375- {
376- $ dir = dirname (__DIR__ ).'/.locks ' ;
377- rmdir ($ dir );
378- mkdir ($ dir , 0777 );
379-
380- return [$ dir , fn () => rmdir ($ dir )];
381- }
382-
383- public static function tearDownAfterClass (): void
376+ private function cleanUpLockFileDirs (): void
384377 {
385- $ glob = dirname ( __DIR__ ). ' /.locks /* ' ;
378+ $ glob = self :: $ mainLockFileDirPath . ' /* ' ;
386379 $ files = glob ($ glob );
387380 foreach ($ files as $ file ) {
388381 if (is_file ($ file )) {
389382 unlink ($ file );
390383 }
391384 }
385+
386+ rmdir (self ::$ mainLockFileDirPath );
387+ rmdir (self ::$ unWriteableFileDirPath );
392388 }
393389}
0 commit comments