From 84c3734dfd15fcc8995537404c9dd16d7058f0bb Mon Sep 17 00:00:00 2001 From: Jakub Korczowski <jakub.korczowski@jklab.net> Date: Sat, 23 Nov 2024 16:06:54 +0100 Subject: [PATCH 1/2] chore: cleaned up tests --- composer.json | 2 +- tests/BatchSnowflakeIDTest.php | 6 +-- tests/FileLockResolverTest.php | 60 ++++++++++++++-------------- tests/SwooleSequenceResolverTest.php | 2 +- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index ac5e8b7..91b6ce5 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ } }, "scripts": { - "test": "vendor/bin/phpunit", + "test": "vendor/bin/phpunit --display-incomplete --display-skipped --display-deprecations --display-errors --display-notices --display-warnings", "phpstan": "vendor/bin/phpstan", "pint": "vendor/bin/pint --config pint.json" }, diff --git a/tests/BatchSnowflakeIDTest.php b/tests/BatchSnowflakeIDTest.php index 2cf0995..748e5a5 100644 --- a/tests/BatchSnowflakeIDTest.php +++ b/tests/BatchSnowflakeIDTest.php @@ -38,7 +38,7 @@ public function test_batch_for_same_instance_with_default_driver(): void /** * @throws Throwable */ - public function test_batch_for_diff_instance_with_redis_driver() + public function test_batch_for_diff_instance_with_redis_driver(): void { if (! extension_loaded('redis') || ! getenv('REDIS_HOST') @@ -61,7 +61,7 @@ public function test_batch_for_diff_instance_with_redis_driver() $this->assertResults($results, 100, 1000); } - public function test_batch_for_diff_instance_with_predis_driver() + public function test_batch_for_diff_instance_with_predis_driver(): void { if (! class_exists('Predis\\Client') || ! getenv('REDIS_HOST') @@ -92,7 +92,7 @@ public function test_batch_for_diff_instance_with_predis_driver() /** * @throws Throwable */ - public function test_batch_for_diff_instance_with_file_driver() + public function test_batch_for_diff_instance_with_file_driver(): void { $fileResolver = new FileLockResolver(__DIR__); diff --git a/tests/FileLockResolverTest.php b/tests/FileLockResolverTest.php index f726d35..de84350 100644 --- a/tests/FileLockResolverTest.php +++ b/tests/FileLockResolverTest.php @@ -17,23 +17,27 @@ class FileLockResolverTest extends TestCase { + private static string $mainLockFileDirPath; + private static string $localLockFileDirPath; + private FileLockResolver $fileLocker; - /** @var callable */ - private $defer; + public static function setUpBeforeClass(): void + { + self::$mainLockFileDirPath = dirname(__DIR__) . '/.locks'; + self::$localLockFileDirPath = __DIR__ . '/.locks'; + } protected function setUp(): void { - [$dir, $defer] = $this->prepareLockPath(); + mkdir(self::$mainLockFileDirPath, 0777); - $this->fileLocker = new FileLockResolver($dir); - $this->defer = $defer; + $this->fileLocker = new FileLockResolver(self::$mainLockFileDirPath); } protected function tearDown(): void { - $defer = $this->defer; - $defer(); + $this->cleanUpLockFileDirs(); } public function test_prepare_path(): void @@ -48,16 +52,13 @@ public function test_prepare_path_not_writable(): void $resolver = new FileLockResolver('/tmp/'); $this->assertEquals('/tmp/', $this->invokeProperty($resolver, 'lockFileDir')); - $dir = __DIR__.'/.locks/'; - if (! is_dir($dir)) { - mkdir($dir, 0444); + if (! is_dir(self::$localLockFileDirPath)) { + mkdir(self::$localLockFileDirPath, 0444); } $this->expectException(\Exception::class); - $this->expectExceptionMessage($dir.' is not writable.'); - $resolver = new FileLockResolver($dir); - - rmdir($dir); + $this->expectExceptionMessage(self::$localLockFileDirPath.' is not writable.'); + $resolver = new FileLockResolver(self::$localLockFileDirPath); } public function test_array_slice(): void @@ -343,7 +344,7 @@ public function test_preg_match(): void /** * @throws SnowflakeException */ - public function test_can_clean_lock_file() + public function test_can_clean_lock_file(): void { FileLockResolver::$shardCount = 1; $fileResolver = $this->fileLocker; @@ -360,34 +361,35 @@ public function test_can_clean_lock_file() $this->assertFileDoesNotExist($path); } - private function touch($content = '') + private function touch(string $content = ''): string { - $file = tempnam(dirname(__DIR__).'/.locks', 'snowflake'); + $file = tempnam(self::$mainLockFileDirPath, 'snowflake'); - if ($content) { + if ($file === false) { + throw new \RuntimeException('Unable to create file'); + } + + if ($content !== '') { file_put_contents($file, $content); } return $file; } - private function prepareLockPath(): array + private function cleanUpLockFileDirs(): void { - $dir = dirname(__DIR__).'/.locks'; - rmdir($dir); - mkdir($dir, 0777); - - return [$dir, fn () => rmdir($dir)]; - } - - public static function tearDownAfterClass(): void - { - $glob = dirname(__DIR__).'/.locks/*'; + $glob = self::$mainLockFileDirPath . '/*'; $files = glob($glob); foreach ($files as $file) { if (is_file($file)) { unlink($file); } } + + rmdir(self::$mainLockFileDirPath); + + if (is_dir(self::$localLockFileDirPath)) { + rmdir(self::$localLockFileDirPath); + } } } diff --git a/tests/SwooleSequenceResolverTest.php b/tests/SwooleSequenceResolverTest.php index 8614d37..84b1b30 100644 --- a/tests/SwooleSequenceResolverTest.php +++ b/tests/SwooleSequenceResolverTest.php @@ -59,7 +59,7 @@ public function test_reset_lock(): void } } - public function test_real_swoole() + public function test_real_swoole(): void { if (! extension_loaded('swoole')) { $this->markTestSkipped('Swoole extension is not installed.'); From d2fb0ffb4005dc127ac0c53584e3eefa766688ff Mon Sep 17 00:00:00 2001 From: Jakub Korczowski <jakub.korczowski@jklab.net> Date: Sat, 23 Nov 2024 16:07:20 +0100 Subject: [PATCH 2/2] chore: enabled swoole tests for php 8.3, this extension is available now --- tests/FileLockResolverTest.php | 18 ++++++------------ tests/SwooleSequenceResolverTest.php | 4 ++-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/tests/FileLockResolverTest.php b/tests/FileLockResolverTest.php index de84350..5813b3f 100644 --- a/tests/FileLockResolverTest.php +++ b/tests/FileLockResolverTest.php @@ -18,19 +18,20 @@ class FileLockResolverTest extends TestCase { private static string $mainLockFileDirPath; - private static string $localLockFileDirPath; + private static string $unWriteableFileDirPath; private FileLockResolver $fileLocker; public static function setUpBeforeClass(): void { self::$mainLockFileDirPath = dirname(__DIR__) . '/.locks'; - self::$localLockFileDirPath = __DIR__ . '/.locks'; + self::$unWriteableFileDirPath = __DIR__ . '/.locks'; } protected function setUp(): void { mkdir(self::$mainLockFileDirPath, 0777); + mkdir(self::$unWriteableFileDirPath, 0444); $this->fileLocker = new FileLockResolver(self::$mainLockFileDirPath); } @@ -52,13 +53,9 @@ public function test_prepare_path_not_writable(): void $resolver = new FileLockResolver('/tmp/'); $this->assertEquals('/tmp/', $this->invokeProperty($resolver, 'lockFileDir')); - if (! is_dir(self::$localLockFileDirPath)) { - mkdir(self::$localLockFileDirPath, 0444); - } - $this->expectException(\Exception::class); - $this->expectExceptionMessage(self::$localLockFileDirPath.' is not writable.'); - $resolver = new FileLockResolver(self::$localLockFileDirPath); + $this->expectExceptionMessage(self::$unWriteableFileDirPath.' is not writable.'); + $resolver = new FileLockResolver(self::$unWriteableFileDirPath); } public function test_array_slice(): void @@ -387,9 +384,6 @@ private function cleanUpLockFileDirs(): void } rmdir(self::$mainLockFileDirPath); - - if (is_dir(self::$localLockFileDirPath)) { - rmdir(self::$localLockFileDirPath); - } + rmdir(self::$unWriteableFileDirPath); } } diff --git a/tests/SwooleSequenceResolverTest.php b/tests/SwooleSequenceResolverTest.php index 84b1b30..020eb1f 100644 --- a/tests/SwooleSequenceResolverTest.php +++ b/tests/SwooleSequenceResolverTest.php @@ -18,8 +18,8 @@ class SwooleSequenceResolverTest extends TestCase { public function setUp(): void { - if (version_compare(PHP_VERSION, '8.3') >= 0) { - $this->markTestSkipped('Swoole does not yet support PHP 8.3'); + if (version_compare(PHP_VERSION, '8.4') >= 0) { + $this->markTestSkipped('Swoole does not yet support PHP 8.4'); } if (! extension_loaded('swoole')) {