diff --git a/composer.json b/composer.json index e5850ee..3c96fd1 100644 --- a/composer.json +++ b/composer.json @@ -52,13 +52,13 @@ "ergebnis/phpunit-slow-test-detector": "^2.9", "friendsofphp/php-cs-fixer": "^3.0", "mikey179/vfsstream": "^1.6.11", - "php-mock/php-mock-phpunit": "^2.1", + "php-mock/php-mock-phpunit": "^2.12", "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^2.0", "phpstan/phpstan-deprecation-rules": "^2.0", "phpstan/phpstan-strict-rules": "^2.0", "phpunit/phpunit": "^9.5.25 || ^10.0 || ^11.0", - "predis/predis": "^1.1.8", + "predis/predis": "^1.1.8 || ^2.0", "spatie/async": "^1.5" }, "suggest": { diff --git a/tests/Mutex/DistributedMutexTest.php b/tests/Mutex/DistributedMutexTest.php index 883c85c..5bb3b47 100644 --- a/tests/Mutex/DistributedMutexTest.php +++ b/tests/Mutex/DistributedMutexTest.php @@ -234,6 +234,21 @@ public function testAcquireWithMajority(int $count, int $available): void $mutex->synchronized(static function () {}); } + /** + * Provides test cases with enough. + * + * @return iterable> + */ + public static function provideMajorityCases(): iterable + { + yield [1, 1]; + yield [2, 2]; + yield [3, 2]; + yield [3, 3]; + yield [4, 3]; + yield [5, 3]; + } + /** * Tests releasing fails because too few servers are available. * @@ -312,21 +327,6 @@ public static function provideMinorityCases(): iterable yield [6, 3]; } - /** - * Provides test cases with enough. - * - * @return iterable> - */ - public static function provideMajorityCases(): iterable - { - yield [1, 1]; - yield [2, 2]; - yield [3, 2]; - yield [3, 3]; - yield [4, 3]; - yield [5, 3]; - } - public function testAcquireMutexLogger(): void { $mutex = $this->createDistributedMutexMock(3); diff --git a/tests/Mutex/MutexTest.php b/tests/Mutex/MutexTest.php index 84880d2..4cad726 100644 --- a/tests/Mutex/MutexTest.php +++ b/tests/Mutex/MutexTest.php @@ -40,6 +40,60 @@ public static function setUpBeforeClass(): void vfsStream::setup('test'); } + /** + * Tests synchronized() executes the code and returns its result. + * + * @param \Closure(): Mutex $mutexFactory + * + * @dataProvider provideMutexFactoriesCases + */ + #[DataProvider('provideMutexFactoriesCases')] + public function testSynchronizedDelegates(\Closure $mutexFactory): void + { + $mutex = $mutexFactory(); + $result = $mutex->synchronized(static function () { + return 'test'; + }); + self::assertSame('test', $result); + } + + /** + * Tests that synchronized() released the lock. + * + * @param \Closure(): Mutex $mutexFactory + * + * @doesNotPerformAssertions + * + * @dataProvider provideMutexFactoriesCases + */ + #[DoesNotPerformAssertions] + #[DataProvider('provideMutexFactoriesCases')] + public function testRelease(\Closure $mutexFactory): void + { + $mutex = $mutexFactory(); + $mutex->synchronized(static function () {}); + + $mutex->synchronized(static function () {}); + } + + /** + * Tests synchronized() rethrows the exception of the code. + * + * @param \Closure(): Mutex $mutexFactory + * + * @dataProvider provideMutexFactoriesCases + */ + #[DataProvider('provideMutexFactoriesCases')] + public function testSynchronizedPassesExceptionThrough(\Closure $mutexFactory): void + { + $mutex = $mutexFactory(); + + $this->expectException(\DomainException::class); + $mutex->synchronized(static function () { + throw new \DomainException(); + }); + } + /** * Provides Mutex factories. * @@ -188,58 +242,4 @@ static function ($uri) { }]; } } - - /** - * Tests synchronized() executes the code and returns its result. - * - * @param \Closure(): Mutex $mutexFactory - * - * @dataProvider provideMutexFactoriesCases - */ - #[DataProvider('provideMutexFactoriesCases')] - public function testSynchronizedDelegates(\Closure $mutexFactory): void - { - $mutex = $mutexFactory(); - $result = $mutex->synchronized(static function () { - return 'test'; - }); - self::assertSame('test', $result); - } - - /** - * Tests that synchronized() released the lock. - * - * @param \Closure(): Mutex $mutexFactory - * - * @doesNotPerformAssertions - * - * @dataProvider provideMutexFactoriesCases - */ - #[DoesNotPerformAssertions] - #[DataProvider('provideMutexFactoriesCases')] - public function testRelease(\Closure $mutexFactory): void - { - $mutex = $mutexFactory(); - $mutex->synchronized(static function () {}); - - $mutex->synchronized(static function () {}); - } - - /** - * Tests synchronized() rethrows the exception of the code. - * - * @param \Closure(): Mutex $mutexFactory - * - * @dataProvider provideMutexFactoriesCases - */ - #[DataProvider('provideMutexFactoriesCases')] - public function testSynchronizedPassesExceptionThrough(\Closure $mutexFactory): void - { - $mutex = $mutexFactory(); - - $this->expectException(\DomainException::class); - $mutex->synchronized(static function () { - throw new \DomainException(); - }); - } } diff --git a/tests/Mutex/RedisMutexTest.php b/tests/Mutex/RedisMutexTest.php index a49136b..e63c753 100644 --- a/tests/Mutex/RedisMutexTest.php +++ b/tests/Mutex/RedisMutexTest.php @@ -258,24 +258,6 @@ public function testSerializersAndCompressors(int $serializer, int $compressor): })); } - public function testResistantToPartialClusterFailuresForAcquiringLock(): void - { - $this->closeMinorityConnections(); - - self::assertSame('test', $this->mutex->synchronized(static function () { - return 'test'; - })); - } - - public function testResistantToPartialClusterFailuresForReleasingLock(): void - { - self::assertNull($this->mutex->synchronized(function () { // @phpstan-ignore staticMethod.alreadyNarrowedType - $this->closeMinorityConnections(); - - return null; - })); - } - /** * @return iterable> */ @@ -313,4 +295,22 @@ public static function provideSerializersAndCompressorsCases(): iterable } } } + + public function testResistantToPartialClusterFailuresForAcquiringLock(): void + { + $this->closeMinorityConnections(); + + self::assertSame('test', $this->mutex->synchronized(static function () { + return 'test'; + })); + } + + public function testResistantToPartialClusterFailuresForReleasingLock(): void + { + self::assertNull($this->mutex->synchronized(function () { // @phpstan-ignore staticMethod.alreadyNarrowedType + $this->closeMinorityConnections(); + + return null; + })); + } }