1212use Malkusch \Lock \Mutex \PostgreSQLMutex ;
1313use Malkusch \Lock \Mutex \RedisMutex ;
1414use Malkusch \Lock \Mutex \SemaphoreMutex ;
15- use Malkusch \Lock \Mutex \TransactionalMutex ;
1615use Malkusch \Lock \Util \LockUtil ;
1716use PHPUnit \Framework \Attributes \DataProvider ;
1817use PHPUnit \Framework \Constraint \IsType ;
@@ -32,8 +31,6 @@ class MutexConcurrencyTest extends TestCase
3231{
3332 /** @var list<string> */
3433 protected static $ temporaryFiles = [];
35- /** @var \PDO|null */
36- private static $ pdo ;
3734
3835 #[\Override]
3936 public static function tearDownAfterClass (): void
@@ -43,24 +40,9 @@ public static function tearDownAfterClass(): void
4340 }
4441 self ::$ temporaryFiles = [];
4542
46- self ::$ pdo = null ;
47-
4843 parent ::tearDownAfterClass ();
4944 }
5045
51- /**
52- * Gets a PDO instance.
53- */
54- private static function getPDO (string $ dsn , string $ user , string $ password ): \PDO
55- {
56- if (self ::$ pdo === null ) {
57- self ::$ pdo = new \PDO ($ dsn , $ user , $ password );
58- self ::$ pdo ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
59- }
60-
61- return self ::$ pdo ;
62- }
63-
6446 /**
6547 * Forks, runs code in the children and wait until all finished.
6648 *
@@ -135,63 +117,6 @@ static function () use ($filename): void {
135117 },
136118 ];
137119 }
138-
139- $ makePDOCase = static function (string $ dsn , string $ user , string $ password , string $ vendor ) {
140- $ pdo = self ::getPDO ($ dsn , $ user , $ password );
141-
142- $ options = ['mysql ' => 'engine=InnoDB ' ];
143- $ option = $ options [$ vendor ] ?? '' ;
144- $ pdo ->exec ('CREATE TABLE IF NOT EXISTS counter(id INT PRIMARY KEY, counter INT) ' . $ option );
145-
146- self ::$ pdo = null ;
147-
148- return [
149- static function (int $ increment ) use ($ dsn , $ user , $ password ) {
150- // This prevents using a closed connection from a child.
151- if ($ increment === 0 ) {
152- self ::$ pdo = null ;
153- }
154- $ pdo = self ::getPDO ($ dsn , $ user , $ password );
155- $ id = 1 ;
156- $ select = $ pdo ->prepare ('SELECT counter FROM counter WHERE id = ? FOR UPDATE ' );
157- $ select ->execute ([$ id ]);
158- $ counter = $ select ->fetchColumn ();
159-
160- $ counter += $ increment ;
161-
162- $ pdo ->prepare ('UPDATE counter SET counter = ? WHERE id = ? ' )
163- ->execute ([$ counter , $ id ]);
164-
165- return $ counter ;
166- },
167- static function ($ timeout ) use ($ dsn , $ user , $ password ) {
168- self ::$ pdo = null ;
169- $ pdo = self ::getPDO ($ dsn , $ user , $ password );
170-
171- return new TransactionalMutex ($ pdo , $ timeout );
172- },
173- static function () use ($ pdo ): void {
174- $ pdo ->beginTransaction ();
175- $ pdo ->exec ('DELETE FROM counter ' );
176- $ pdo ->exec ('INSERT INTO counter VALUES (1, 0) ' );
177- $ pdo ->commit ();
178- },
179- ];
180- };
181-
182- if (getenv ('MYSQL_DSN ' )) {
183- $ dsn = getenv ('MYSQL_DSN ' );
184- $ user = getenv ('MYSQL_USER ' );
185- $ password = getenv ('MYSQL_PASSWORD ' );
186- yield 'mysql ' => $ makePDOCase ($ dsn , $ user , $ password , 'mysql ' );
187- }
188-
189- if (getenv ('PGSQL_DSN ' )) {
190- $ dsn = getenv ('PGSQL_DSN ' );
191- $ user = getenv ('PGSQL_USER ' );
192- $ password = getenv ('PGSQL_PASSWORD ' );
193- yield 'postgres ' => $ makePDOCase ($ dsn , $ user , $ password , 'postgres ' );
194- }
195120 }
196121
197122 /**
0 commit comments