Skip to content

Commit 2e07856

Browse files
committed
Make all PHPUnit data provider methods static
1 parent 62577c7 commit 2e07856

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

tests/mutex/MutexConcurrencyTest.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MutexConcurrencyTest extends TestCase
3030
/** @var list<string> */
3131
protected static $temporaryFiles = [];
3232
/** @var \PDO|null the pdo instance */
33-
private $pdo;
33+
private static $pdo;
3434

3535
#[\Override]
3636
public static function tearDownAfterClass(): void
@@ -40,6 +40,8 @@ public static function tearDownAfterClass(): void
4040
}
4141
self::$temporaryFiles = [];
4242

43+
self::$pdo = null;
44+
4345
parent::tearDownAfterClass();
4446
}
4547

@@ -52,14 +54,14 @@ public static function tearDownAfterClass(): void
5254
*
5355
* @return \PDO the PDO
5456
*/
55-
private function getPDO(string $dsn, string $user, string $password): \PDO
57+
private static function getPDO(string $dsn, string $user, string $password): \PDO
5658
{
57-
if ($this->pdo === null) {
58-
$this->pdo = new \PDO($dsn, $user, $password);
59-
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
59+
if (self::$pdo === null) {
60+
self::$pdo = new \PDO($dsn, $user, $password);
61+
self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
6062
}
6163

62-
return $this->pdo;
64+
return self::$pdo;
6365
}
6466

6567
/**
@@ -114,7 +116,7 @@ public function testHighContention(callable $code, callable $mutexFactory): void
114116
*
115117
* @return iterable<list<mixed>>
116118
*/
117-
public function provideHighContentionCases(): iterable
119+
public static function provideHighContentionCases(): iterable
118120
{
119121
$cases = array_map(static function (array $mutexFactory): array {
120122
$filename = tempnam(sys_get_temp_dir(), 'php-lock-high-contention');
@@ -136,8 +138,8 @@ static function (int $increment) use ($filename): int {
136138
];
137139
}, static::provideExecutionIsSerializedWhenLockedCases());
138140

139-
$addPDO = function ($dsn, $user, $password, $vendor) use (&$cases) {
140-
$pdo = $this->getPDO($dsn, $user, $password);
141+
$addPDO = static function ($dsn, $user, $password, $vendor) use (&$cases) {
142+
$pdo = self::getPDO($dsn, $user, $password);
141143

142144
$options = ['mysql' => 'engine=InnoDB'];
143145
$option = $options[$vendor] ?? '';
@@ -148,15 +150,15 @@ static function (int $increment) use ($filename): int {
148150
$pdo->exec('INSERT INTO counter VALUES (1, 0)');
149151
$pdo->commit();
150152

151-
$this->pdo = null;
153+
self::$pdo = null;
152154

153155
$cases[$vendor] = [
154-
function ($increment) use ($dsn, $user, $password) {
156+
static function ($increment) use ($dsn, $user, $password) {
155157
// This prevents using a closed connection from a child.
156158
if ($increment == 0) {
157-
$this->pdo = null;
159+
self::$pdo = null;
158160
}
159-
$pdo = $this->getPDO($dsn, $user, $password);
161+
$pdo = self::getPDO($dsn, $user, $password);
160162
$id = 1;
161163
$select = $pdo->prepare('SELECT counter FROM counter WHERE id = ? FOR UPDATE');
162164
$select->execute([$id]);
@@ -169,9 +171,9 @@ function ($increment) use ($dsn, $user, $password) {
169171

170172
return $counter;
171173
},
172-
function ($timeout = 3) use ($dsn, $user, $password) {
173-
$this->pdo = null;
174-
$pdo = $this->getPDO($dsn, $user, $password);
174+
static function ($timeout = 3) use ($dsn, $user, $password) {
175+
self::$pdo = null;
176+
$pdo = self::getPDO($dsn, $user, $password);
175177

176178
return new TransactionalMutex($pdo, $timeout);
177179
},

tests/mutex/MutexTest.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function setUpBeforeClass(): void
4040
*
4141
* @return callable[][] the mutex factories
4242
*/
43-
public function provideMutexFactoriesCases(): iterable
43+
public static function provideMutexFactoriesCases(): iterable
4444
{
4545
$cases = [
4646
'NoMutex' => [static function (): Mutex {
@@ -80,28 +80,34 @@ public function provideMutexFactoriesCases(): iterable
8080
return new SemaphoreMutex(sem_get(ftok(__FILE__, 'a')));
8181
}],
8282

83-
'SpinlockMutex' => [function (): Mutex {
84-
$mock = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
85-
$mock->expects(self::atLeastOnce())
86-
->method('acquire')
87-
->willReturn(true);
88-
89-
$mock->expects(self::atLeastOnce())
90-
->method('release')
91-
->willReturn(true);
92-
93-
return $mock;
83+
'SpinlockMutex' => [static function (): Mutex {
84+
$lock = new class('test') extends SpinlockMutex {
85+
#[\Override]
86+
protected function acquire(string $key, float $expire): bool
87+
{
88+
return true;
89+
}
90+
91+
#[\Override]
92+
protected function release(string $key): bool
93+
{
94+
return true;
95+
}
96+
};
97+
98+
return $lock;
9499
}],
95100

96-
'LockMutex' => [function (): Mutex {
97-
$mock = $this->getMockForAbstractClass(LockMutex::class);
98-
$mock->expects(self::atLeastOnce())
99-
->method('lock');
101+
'LockMutex' => [static function (): Mutex {
102+
$lock = new class extends LockMutex {
103+
#[\Override]
104+
protected function lock(): void {}
100105

101-
$mock->expects(self::atLeastOnce())
102-
->method('unlock');
106+
#[\Override]
107+
protected function unlock(): void {}
108+
};
103109

104-
return $mock;
110+
return $lock;
105111
}],
106112
];
107113

0 commit comments

Comments
 (0)