Skip to content

Commit

Permalink
Use attributes for test data providers
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 5, 2024
1 parent 29970e5 commit 4a67bb6
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/mutex/FlockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use malkusch\lock\exception\TimeoutException;
use malkusch\lock\mutex\FlockMutex;
use malkusch\lock\util\PcntlTimeout;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class FlockMutexTest extends TestCase
Expand Down Expand Up @@ -41,6 +42,7 @@ protected function tearDown(): void
*
* @dataProvider provideTimeoutableStrategiesCases
*/
#[DataProvider('provideTimeoutableStrategiesCases')]
public function testCodeExecutedOutsideLockIsNotThrown(int $strategy): void
{
$this->mutex->strategy = $strategy; // @phpstan-ignore property.private
Expand All @@ -57,6 +59,7 @@ public function testCodeExecutedOutsideLockIsNotThrown(int $strategy): void
*
* @dataProvider provideTimeoutableStrategiesCases
*/
#[DataProvider('provideTimeoutableStrategiesCases')]
public function testTimeoutOccurs(int $strategy): void
{
$this->expectException(TimeoutException::class);
Expand Down
2 changes: 2 additions & 0 deletions tests/mutex/MemcachedMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\exception\TimeoutException;
use malkusch\lock\mutex\MemcachedMutex;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -15,6 +16,7 @@
*
* @requires extension memcached
*/
#[RequiresPhpExtension('memcached')]
class MemcachedMutexTest extends TestCase
{
/** @var \Memcached|MockObject */
Expand Down
3 changes: 3 additions & 0 deletions tests/mutex/MutexConcurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use malkusch\lock\mutex\PredisMutex;
use malkusch\lock\mutex\SemaphoreMutex;
use malkusch\lock\mutex\TransactionalMutex;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Constraint\IsType;
use PHPUnit\Framework\TestCase;
use Predis\Client;
Expand Down Expand Up @@ -84,6 +85,7 @@ private function fork(int $concurrency, \Closure $code): void
*
* @dataProvider provideHighContentionCases
*/
#[DataProvider('provideHighContentionCases')]
public function testHighContention(\Closure $code, \Closure $mutexFactory): void
{
$concurrency = 10;
Expand Down Expand Up @@ -196,6 +198,7 @@ static function ($timeout = 3) use ($dsn, $user, $password) {
*
* @dataProvider provideExecutionIsSerializedWhenLockedCases
*/
#[DataProvider('provideExecutionIsSerializedWhenLockedCases')]
public function testExecutionIsSerializedWhenLocked(\Closure $mutexFactory): void
{
$time = \microtime(true);
Expand Down
4 changes: 4 additions & 0 deletions tests/mutex/MutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use malkusch\lock\mutex\SpinlockMutex;
use malkusch\lock\mutex\TransactionalMutex;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Predis\Client;

Expand Down Expand Up @@ -192,6 +193,7 @@ static function ($uri) {
*
* @dataProvider provideMutexFactoriesCases
*/
#[DataProvider('provideMutexFactoriesCases')]
public function testSynchronizedDelegates(\Closure $mutexFactory): void
{
/** @var Mutex $mutex */
Expand All @@ -209,6 +211,7 @@ public function testSynchronizedDelegates(\Closure $mutexFactory): void
*
* @dataProvider provideMutexFactoriesCases
*/
#[DataProvider('provideMutexFactoriesCases')]
public function testRelease(\Closure $mutexFactory): void
{
$mutex = $mutexFactory();
Expand All @@ -225,6 +228,7 @@ public function testRelease(\Closure $mutexFactory): void
*
* @dataProvider provideMutexFactoriesCases
*/
#[DataProvider('provideMutexFactoriesCases')]
public function testSynchronizedPassesExceptionThrough(\Closure $mutexFactory): void
{
$this->expectException(\DomainException::class);
Expand Down
6 changes: 6 additions & 0 deletions tests/mutex/PHPRedisMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\exception\MutexException;
use malkusch\lock\mutex\PHPRedisMutex;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

if (\PHP_MAJOR_VERSION >= 8) {
Expand Down Expand Up @@ -63,6 +66,8 @@ public function set($key, $value, $options = null)
*
* @group redis
*/
#[RequiresPhpExtension('redis')]
#[Group('redis')]
class PHPRedisMutexTest extends TestCase
{
/** @var \Redis[] */
Expand Down Expand Up @@ -210,6 +215,7 @@ public function testEvalScriptFails(): void
*
* @dataProvider provideSerializersAndCompressorsCases
*/
#[DataProvider('provideSerializersAndCompressorsCases')]
public function testSerializersAndCompressors($serializer, $compressor): void
{
foreach ($this->connections as $connection) {
Expand Down
2 changes: 2 additions & 0 deletions tests/mutex/PredisMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\mutex\PredisMutex;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Predis\ClientInterface;
Expand All @@ -16,6 +17,7 @@
/**
* @group redis
*/
#[Group('redis')]
class PredisMutexTest extends TestCase
{
/** @var ClientInterface|MockObject */
Expand Down
10 changes: 10 additions & 0 deletions tests/mutex/RedisMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
use malkusch\lock\mutex\RedisMutex;
use phpmock\environment\SleepEnvironmentBuilder;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @group redis
*/
#[Group('redis')]
class RedisMutexTest extends TestCase
{
use PHPMock;
Expand Down Expand Up @@ -64,6 +67,7 @@ static function ($id): array {
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testTooFewServerToAcquire(int $count, int $available): void
{
$this->expectException(LockAcquireException::class);
Expand Down Expand Up @@ -99,6 +103,7 @@ static function () use (&$i, $available): bool {
*
* @dataProvider provideMajorityCases
*/
#[DataProvider('provideMajorityCases')]
public function testFaultTolerance(int $count, int $available): void
{
$mutex = $this->buildRedisMutex($count);
Expand Down Expand Up @@ -132,6 +137,7 @@ static function () use (&$i, $available): bool {
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testAcquireTooFewKeys($count, $available): void
{
$this->expectException(TimeoutException::class);
Expand Down Expand Up @@ -164,6 +170,7 @@ static function () use (&$i, $available): bool {
*
* @dataProvider provideTimingOutCases
*/
#[DataProvider('provideTimingOutCases')]
public function testTimingOut(int $count, float $timeout, float $delay): void
{
$timeoutStr = (string) round($timeout, 6);
Expand Down Expand Up @@ -211,6 +218,7 @@ public static function provideTimingOutCases(): iterable
*
* @dataProvider provideMajorityCases
*/
#[DataProvider('provideMajorityCases')]
public function testAcquireWithMajority(int $count, int $available): void
{
$mutex = $this->buildRedisMutex($count);
Expand Down Expand Up @@ -240,6 +248,7 @@ static function () use (&$i, $available): bool {
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testTooFewServersToRelease(int $count, int $available): void
{
$mutex = $this->buildRedisMutex($count);
Expand Down Expand Up @@ -275,6 +284,7 @@ static function () use (&$i, $available): bool {
*
* @dataProvider provideMinorityCases
*/
#[DataProvider('provideMinorityCases')]
public function testReleaseTooFewKeys(int $count, int $available): void
{
$mutex = $this->buildRedisMutex($count);
Expand Down
3 changes: 3 additions & 0 deletions tests/mutex/TransactionalMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\mutex\TransactionalMutex;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -18,6 +19,7 @@ class TransactionalMutexTest extends TestCase
*
* @dataProvider provideInvalidErrorModeCases
*/
#[DataProvider('provideInvalidErrorModeCases')]
public function testInvalidErrorMode(int $mode): void
{
$this->expectException(\InvalidArgumentException::class);
Expand Down Expand Up @@ -109,6 +111,7 @@ public function testFailExceptionRollsback(): void
*
* @dataProvider provideReplayTransactionCases
*/
#[DataProvider('provideReplayTransactionCases')]
public function testReplayTransaction(\Exception $exception): void
{
$pdo = $this->buildMySqlPdo();
Expand Down
2 changes: 2 additions & 0 deletions tests/util/DoubleCheckedLockingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use malkusch\lock\mutex\Mutex;
use malkusch\lock\util\DoubleCheckedLocking;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -87,6 +88,7 @@ public function testLockedCheckAndExecution(): void
*
* @dataProvider provideCodeNotExecutedCases
*/
#[DataProvider('provideCodeNotExecutedCases')]
public function testCodeNotExecuted(\Closure $check): void
{
$this->mutex->expects(self::any())
Expand Down

0 comments on commit 4a67bb6

Please sign in to comment.