Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup of tests (testing with swoole on 8.3, removed all warnings) #79

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
godruoyi marked this conversation as resolved.
Show resolved Hide resolved
"phpstan": "vendor/bin/phpstan",
"pint": "vendor/bin/pint --config pint.json"
},
Expand Down
6 changes: 3 additions & 3 deletions tests/BatchSnowflakeIDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
jklab marked this conversation as resolved.
Show resolved Hide resolved
{
if (! extension_loaded('redis')
|| ! getenv('REDIS_HOST')
Expand All @@ -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')
Expand Down Expand Up @@ -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__);

Expand Down
58 changes: 27 additions & 31 deletions tests/FileLockResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@

class FileLockResolverTest extends TestCase
{
private static string $mainLockFileDirPath;
private static string $unWriteableFileDirPath;

private FileLockResolver $fileLocker;

/** @var callable */
private $defer;
public static function setUpBeforeClass(): void
{
self::$mainLockFileDirPath = dirname(__DIR__) . '/.locks';
self::$unWriteableFileDirPath = __DIR__ . '/.locks';
}

protected function setUp(): void
{
[$dir, $defer] = $this->prepareLockPath();
mkdir(self::$mainLockFileDirPath, 0777);
mkdir(self::$unWriteableFileDirPath, 0444);

$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
Expand All @@ -48,16 +53,9 @@ 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);
}

$this->expectException(\Exception::class);
$this->expectExceptionMessage($dir.' is not writable.');
$resolver = new FileLockResolver($dir);

rmdir($dir);
$this->expectExceptionMessage(self::$unWriteableFileDirPath.' is not writable.');
$resolver = new FileLockResolver(self::$unWriteableFileDirPath);
}

public function test_array_slice(): void
Expand Down Expand Up @@ -343,7 +341,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;
Expand All @@ -360,34 +358,32 @@ 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
{
$dir = dirname(__DIR__).'/.locks';
rmdir($dir);
mkdir($dir, 0777);

return [$dir, fn () => rmdir($dir)];
}

public static function tearDownAfterClass(): void
private function cleanUpLockFileDirs(): void
{
$glob = dirname(__DIR__).'/.locks/*';
$glob = self::$mainLockFileDirPath . '/*';
$files = glob($glob);
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}

rmdir(self::$mainLockFileDirPath);
rmdir(self::$unWriteableFileDirPath);
}
}
6 changes: 3 additions & 3 deletions tests/SwooleSequenceResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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.');
Expand Down