Skip to content

Commit 624eb4a

Browse files
committed
Replace deprecated PHPUnit usages
1 parent 48988b2 commit 624eb4a

12 files changed

+60
-58
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,3 @@ parameters:
3636
identifier: empty.notAllowed
3737
message: '~^Construct empty\(\) is not allowed\. Use more strict comparison\.$~'
3838
count: 6
39-
-
40-
path: '*'
41-
identifier: method.deprecated
42-
message: '~^Call to deprecated method getMockForAbstractClass\(\) of class PHPUnit\\Framework\\TestCase:\nhttps://github.com/sebastianbergmann/phpunit/issues/5241$~'
43-
count: 8
44-
-
45-
path: 'tests/mutex/PredisMutexTest.php'
46-
identifier: method.deprecated
47-
message: '~^Call to deprecated method addMethods\(\) of class PHPUnit\\Framework\\MockObject\\MockBuilder:\nhttps://github.com/sebastianbergmann/phpunit/issues/5320$~'
48-
count: 1

src/mutex/TransactionalMutex.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,15 @@ public function synchronized(callable $code)
118118

119119
/**
120120
* Checks if an exception or any of its previous exceptions is a \PDOException.
121-
*
122-
* @return bool True if there's a \PDOException
123121
*/
124-
private static function hasPDOException(\Throwable $exception)
122+
private static function hasPDOException(\Throwable $exception): bool
125123
{
126124
if ($exception instanceof \PDOException) {
127125
return true;
128126
}
129-
if ($exception->getPrevious() === null) {
130-
return false;
131-
}
132127

133-
return self::hasPDOException($exception->getPrevious());
128+
return $exception->getPrevious() !== null
129+
&& self::hasPDOException($exception->getPrevious());
134130
}
135131

136132
/**

tests/mutex/LockMutexTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313
class LockMutexTest extends TestCase
1414
{
15-
/** @var MockObject|LockMutex */
15+
/** @var LockMutex&MockObject */
1616
private $mutex;
1717

1818
#[\Override]
1919
protected function setUp(): void
2020
{
2121
parent::setUp();
2222

23-
$this->mutex = $this->getMockForAbstractClass(LockMutex::class);
23+
$this->mutex = $this->getMockBuilder(LockMutex::class)
24+
->onlyMethods(['lock', 'unlock'])
25+
->getMock();
2426
}
2527

2628
/**

tests/mutex/MemcachedMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#[RequiresPhpExtension('memcached')]
2020
class MemcachedMutexTest extends TestCase
2121
{
22-
/** @var \Memcached|MockObject */
22+
/** @var \Memcached&MockObject */
2323
protected $memcached;
2424

2525
/** @var MemcachedMutex */

tests/mutex/MutexConcurrencyTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ public static function provideExecutionIsSerializedWhenLockedCases(): iterable
276276

277277
$cases['PredisMutex'] = [static function ($timeout = 3) use ($uris): Mutex {
278278
$clients = array_map(
279-
static function ($uri) {
280-
return new Client($uri);
281-
},
279+
static fn ($uri) => new Client($uri),
282280
$uris
283281
);
284282

tests/mutex/MutexTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ protected function unlock(): void {}
128128

129129
$cases['PredisMutex'] = [static function () use ($uris): Mutex {
130130
$clients = array_map(
131-
static function ($uri) {
132-
return new Client($uri);
133-
},
131+
static fn ($uri) => new Client($uri),
134132
$uris
135133
);
136134

@@ -196,7 +194,6 @@ static function ($uri) {
196194
#[DataProvider('provideMutexFactoriesCases')]
197195
public function testSynchronizedDelegates(\Closure $mutexFactory): void
198196
{
199-
/** @var Mutex $mutex */
200197
$mutex = $mutexFactory();
201198
$result = $mutex->synchronized(static function (): string {
202199
return 'test';

tests/mutex/PgAdvisoryLockMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class PgAdvisoryLockMutexTest extends TestCase
1212
{
13-
/** @var \PDO|MockObject */
13+
/** @var \PDO&MockObject */
1414
private $pdo;
1515

1616
/** @var PgAdvisoryLockMutex */

tests/mutex/PredisMutexTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,40 @@
1414
use Predis\PredisException;
1515
use Psr\Log\LoggerInterface;
1616

17+
interface ClientInterfaceWithSetAndEvalMethods extends ClientInterface
18+
{
19+
/**
20+
* @return mixed
21+
*/
22+
public function eval();
23+
24+
/**
25+
* @return mixed
26+
*/
27+
public function set();
28+
}
29+
1730
/**
1831
* @group redis
1932
*/
2033
#[Group('redis')]
2134
class PredisMutexTest extends TestCase
2235
{
23-
/** @var ClientInterface|MockObject */
36+
/** @var ClientInterface&MockObject */
2437
private $client;
2538

2639
/** @var PredisMutex */
2740
private $mutex;
2841

29-
/** @var LoggerInterface|MockObject */
42+
/** @var LoggerInterface&MockObject */
3043
private $logger;
3144

3245
#[\Override]
3346
protected function setUp(): void
3447
{
3548
parent::setUp();
3649

37-
$this->client = $this->getMockBuilder(ClientInterface::class)
38-
->onlyMethods(get_class_methods(ClientInterface::class))
39-
->addMethods(['set', 'eval'])
40-
->getMock();
50+
$this->client = $this->createMock(ClientInterfaceWithSetAndEvalMethods::class);
4151

4252
$this->mutex = new PredisMutex([$this->client], 'test', 2.5);
4353

tests/mutex/RedisMutexTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,21 @@ protected function setUp(): void
4040
}
4141

4242
/**
43-
* Builds a testable RedisMutex mock.
44-
*
4543
* @param int $count The amount of redis apis
4644
*
47-
* @return MockObject|RedisMutex
45+
* @return RedisMutex&MockObject
4846
*/
49-
private function buildRedisMutex(int $count, float $timeout = 1)
47+
private function createRedisMutexMock(int $count, float $timeout = 1): RedisMutex
5048
{
5149
$redisAPIs = array_map(
52-
static function ($id): array {
53-
return ['id' => $id];
54-
},
50+
static fn ($id) => ['id' => $id],
5551
range(1, $count)
5652
);
5753

58-
return $this->getMockForAbstractClass(RedisMutex::class, [$redisAPIs, 'test', $timeout]);
54+
return $this->getMockBuilder(RedisMutex::class)
55+
->setConstructorArgs([$redisAPIs, 'test', $timeout])
56+
->onlyMethods(['add', 'evalScript'])
57+
->getMock();
5958
}
6059

6160
/**
@@ -72,7 +71,7 @@ public function testTooFewServerToAcquire(int $count, int $available): void
7271
$this->expectException(LockAcquireException::class);
7372
$this->expectExceptionCode(MutexException::REDIS_NOT_ENOUGH_SERVERS);
7473

75-
$mutex = $this->buildRedisMutex($count);
74+
$mutex = $this->createRedisMutexMock($count);
7675

7776
$i = 0;
7877
$mutex->expects(self::exactly($count))
@@ -105,7 +104,7 @@ static function () use (&$i, $available): bool {
105104
#[DataProvider('provideMajorityCases')]
106105
public function testFaultTolerance(int $count, int $available): void
107106
{
108-
$mutex = $this->buildRedisMutex($count);
107+
$mutex = $this->createRedisMutexMock($count);
109108
$mutex->expects(self::exactly($count))
110109
->method('evalScript')
111110
->willReturn(true);
@@ -142,7 +141,7 @@ public function testAcquireTooFewKeys($count, $available): void
142141
$this->expectException(TimeoutException::class);
143142
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded');
144143

145-
$mutex = $this->buildRedisMutex($count);
144+
$mutex = $this->createRedisMutexMock($count);
146145

147146
$i = 0;
148147
$mutex->expects(self::any())
@@ -180,7 +179,7 @@ public function testTimingOut(int $count, float $timeout, float $delay): void
180179
$this->expectException(TimeoutException::class);
181180
$this->expectExceptionMessage('Timeout of ' . $timeoutStr . ' seconds exceeded');
182181

183-
$mutex = $this->buildRedisMutex($count, $timeout);
182+
$mutex = $this->createRedisMutexMock($count, $timeout);
184183

185184
$mutex->expects(self::exactly($count))
186185
->method('add')
@@ -220,7 +219,7 @@ public static function provideTimingOutCases(): iterable
220219
#[DataProvider('provideMajorityCases')]
221220
public function testAcquireWithMajority(int $count, int $available): void
222221
{
223-
$mutex = $this->buildRedisMutex($count);
222+
$mutex = $this->createRedisMutexMock($count);
224223
$mutex->expects(self::exactly($count))
225224
->method('evalScript')
226225
->willReturn(true);
@@ -250,7 +249,7 @@ static function () use (&$i, $available): bool {
250249
#[DataProvider('provideMinorityCases')]
251250
public function testTooFewServersToRelease(int $count, int $available): void
252251
{
253-
$mutex = $this->buildRedisMutex($count);
252+
$mutex = $this->createRedisMutexMock($count);
254253
$mutex->expects(self::exactly($count))
255254
->method('add')
256255
->willReturn(true);
@@ -286,7 +285,7 @@ static function () use (&$i, $available): bool {
286285
#[DataProvider('provideMinorityCases')]
287286
public function testReleaseTooFewKeys(int $count, int $available): void
288287
{
289-
$mutex = $this->buildRedisMutex($count);
288+
$mutex = $this->createRedisMutexMock($count);
290289
$mutex->expects(self::exactly($count))
291290
->method('add')
292291
->willReturn(true);

tests/mutex/SpinlockMutexTest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ protected function setUp(): void
3333
$this->registerForTearDown($sleep);
3434
}
3535

36+
/**
37+
* @return SpinlockMutex&MockObject
38+
*/
39+
private function createSpinlockMutexMock(float $timeout = 3): SpinlockMutex
40+
{
41+
return $this->getMockBuilder(SpinlockMutex::class)
42+
->setConstructorArgs(['test', $timeout])
43+
->onlyMethods(['acquire', 'release'])
44+
->getMock();
45+
}
46+
3647
/**
3748
* Tests failing to acquire the lock.
3849
*/
3950
public function testFailAcquireLock(): void
4051
{
4152
$this->expectException(LockAcquireException::class);
4253

43-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
54+
$mutex = $this->createSpinlockMutexMock();
4455
$mutex->expects(self::any())
4556
->method('acquire')
4657
->willThrowException(new LockAcquireException());
@@ -58,7 +69,7 @@ public function testAcquireTimesOut(): void
5869
$this->expectException(TimeoutException::class);
5970
$this->expectExceptionMessage('Timeout of 3.0 seconds exceeded');
6071

61-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
72+
$mutex = $this->createSpinlockMutexMock();
6273
$mutex->expects(self::atLeastOnce())
6374
->method('acquire')
6475
->willReturn(false);
@@ -73,8 +84,7 @@ public function testAcquireTimesOut(): void
7384
*/
7485
public function testExecuteTooLong(): void
7586
{
76-
/** @var SpinlockMutex|MockObject $mutex */
77-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.5]); // @phpstan-ignore varTag.nativeType
87+
$mutex = $this->createSpinlockMutexMock(0.5);
7888
$mutex->expects(self::any())
7989
->method('acquire')
8090
->willReturn(true);
@@ -99,7 +109,7 @@ public function testExecuteTooLong(): void
99109
*/
100110
public function testExecuteBarelySucceeds(): void
101111
{
102-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.5]);
112+
$mutex = $this->createSpinlockMutexMock(0.5);
103113
$mutex->expects(self::any())->method('acquire')->willReturn(true);
104114
$mutex->expects(self::once())->method('release')->willReturn(true);
105115

@@ -115,7 +125,7 @@ public function testFailReleasingLock(): void
115125
{
116126
$this->expectException(LockReleaseException::class);
117127

118-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
128+
$mutex = $this->createSpinlockMutexMock();
119129
$mutex->expects(self::any())->method('acquire')->willReturn(true);
120130
$mutex->expects(self::any())->method('release')->willReturn(false);
121131

@@ -127,7 +137,7 @@ public function testFailReleasingLock(): void
127137
*/
128138
public function testExecuteTimeoutLeavesOneSecondForKeyToExpire(): void
129139
{
130-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.2]);
140+
$mutex = $this->createSpinlockMutexMock(0.2);
131141
$mutex->expects(self::once())
132142
->method('acquire')
133143
->with(self::anything(), 1.2)

tests/mutex/TransactionalMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function testRollbackAfterFailedCommitFails(): void
186186
private function buildMySqlPdo(): \PDO
187187
{
188188
if (!getenv('MYSQL_DSN')) {
189-
self::markTestSkipped();
189+
self::markTestSkipped('MySQL server is needed');
190190
}
191191

192192
$dsn = getenv('MYSQL_DSN');

tests/util/DoubleCheckedLockingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class DoubleCheckedLockingTest extends TestCase
1414
{
15-
/** @var Mutex|MockObject */
15+
/** @var Mutex&MockObject */
1616
private $mutex;
1717

1818
#[\Override]

0 commit comments

Comments
 (0)