Skip to content

Commit 7e3dcdb

Browse files
Merge pull request #17874 from MoonE/phpunit-compat
Simpler PHPUnit 8 compatibility
2 parents a0e4aff + b3498a2 commit 7e3dcdb

12 files changed

Lines changed: 58 additions & 85 deletions

phpstan-baseline.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8935,6 +8935,16 @@ parameters:
89358935
count: 1
89368936
path: libraries/classes/ZipExtension.php
89378937

8938+
-
8939+
message: "#^Call to an undefined static method PHPUnit\\\\Framework\\\\TestCase\\:\\:assertFileDoesNotExist\\(\\)\\.$#"
8940+
count: 1
8941+
path: test/classes/AbstractTestCase.php
8942+
8943+
-
8944+
message: "#^Call to an undefined static method PHPUnit\\\\Framework\\\\TestCase\\:\\:assertMatchesRegularExpression\\(\\)\\.$#"
8945+
count: 1
8946+
path: test/classes/AbstractTestCase.php
8947+
89388948
-
89398949
message: "#^Method PhpMyAdmin\\\\Tests\\\\AbstractTestCase\\:\\:callFunction\\(\\) has parameter \\$params with no value type specified in iterable type array\\.$#"
89408950
count: 1

test/classes/AbstractTestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
use function array_keys;
2323
use function in_array;
24+
use function method_exists;
2425

2526
use const DIRECTORY_SEPARATOR;
2627

@@ -44,6 +45,32 @@ abstract class AbstractTestCase extends TestCase
4445
'__PHPUNIT_BOOTSTRAP',
4546
];
4647

48+
/**
49+
* For PHPUnit < 9.1 compatibility
50+
*/
51+
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
52+
{
53+
if (method_exists(parent::class, 'assertMatchesRegularExpression')) {
54+
parent::assertMatchesRegularExpression($pattern, $string, $message);
55+
} else {
56+
/** @psalm-suppress DeprecatedMethod */
57+
parent::assertRegExp($pattern, $string, $message);
58+
}
59+
}
60+
61+
/**
62+
* For PHPUnit < 9.1 compatibility
63+
*/
64+
public static function assertFileDoesNotExist(string $filename, string $message = ''): void
65+
{
66+
if (method_exists(parent::class, 'assertFileDoesNotExist')) {
67+
parent::assertFileDoesNotExist($filename, $message);
68+
} else {
69+
/** @psalm-suppress DeprecatedMethod */
70+
parent::assertFileNotExists($filename, $message);
71+
}
72+
}
73+
4774
/**
4875
* Prepares environment for the test.
4976
* Clean all variables

test/classes/Config/FormTest.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use ReflectionProperty;
1212

1313
use function array_keys;
14-
use function method_exists;
1514
use function preg_match;
1615

1716
/**
@@ -151,13 +150,7 @@ public function testReadFormPathsCallBack(): void
151150
$this->assertIsString($result[1]);
152151

153152
// needs regexp because the counter is static
154-
155-
if (method_exists($this, 'assertMatchesRegularExpression')) {
156-
$this->assertMatchesRegularExpression('/^preffoo\/foo\/bar\/\:group\:end\:\d+$/', $result[1]);
157-
} else {
158-
/** @psalm-suppress DeprecatedMethod */
159-
$this->assertRegExp('/^preffoo\/foo\/bar\/\:group\:end\:\d+$/', $result[1]);
160-
}
153+
$this->assertMatchesRegularExpression('/^preffoo\/foo\/bar\/\:group\:end\:\d+$/', $result[1]);
161154
}
162155

163156
/**
@@ -189,17 +182,10 @@ public function testReadFormPaths(): void
189182
unset($result['test']);
190183

191184
// needs regexp because the counter is static
192-
193-
$keys = array_keys($result);
194-
$key = $keys[0];
185+
$key = array_keys($result)[0];
195186
$this->assertIsString($key);
196187

197-
if (method_exists($this, 'assertMatchesRegularExpression')) {
198-
$this->assertMatchesRegularExpression('/^\:group\:end\:(\d+)$/', $key);
199-
} else {
200-
/** @psalm-suppress DeprecatedMethod */
201-
$this->assertRegExp('/^\:group\:end\:(\d+)$/', $key);
202-
}
188+
$this->assertMatchesRegularExpression('/^\:group\:end\:(\d+)$/', $key);
203189

204190
preg_match('/^\:group\:end\:(\d+)$/', $key, $matches);
205191
$digit = $matches[1];

test/classes/Gis/GisGeometryCollectionTest.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
use PhpMyAdmin\Tests\AbstractTestCase;
1010
use TCPDF;
1111

12-
use function method_exists;
13-
use function preg_match;
14-
1512
/**
1613
* @covers \PhpMyAdmin\Gis\GisGeometryCollection
1714
* @runTestsInSeparateProcesses
@@ -253,21 +250,8 @@ public function testPrepareRowAsSvg(
253250
array $scaleData,
254251
string $output
255252
): void {
256-
$string = $this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData);
257-
$this->assertEquals(1, preg_match($output, $string));
258-
259-
if (method_exists($this, 'assertMatchesRegularExpression')) {
260-
$this->assertMatchesRegularExpression(
261-
$output,
262-
$this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData)
263-
);
264-
} else {
265-
/** @psalm-suppress DeprecatedMethod */
266-
$this->assertRegExp(
267-
$output,
268-
$this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData)
269-
);
270-
}
253+
$svg = $this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData);
254+
$this->assertMatchesRegularExpression($output, $svg);
271255
}
272256

273257
/**

test/classes/Gis/GisLineStringTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use PhpMyAdmin\Image\ImageWrapper;
99
use TCPDF;
1010

11-
use function preg_match;
12-
1311
/**
1412
* @covers \PhpMyAdmin\Gis\GisLineString
1513
* @runTestsInSeparateProcesses
@@ -246,8 +244,8 @@ public function testPrepareRowAsSvg(
246244
array $scaleData,
247245
string $output
248246
): void {
249-
$string = $this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData);
250-
$this->assertEquals(1, preg_match($output, $string));
247+
$svg = $this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData);
248+
$this->assertMatchesRegularExpression($output, $svg);
251249
}
252250

253251
/**

test/classes/Gis/GisMultiLineStringTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use PhpMyAdmin\Image\ImageWrapper;
99
use TCPDF;
1010

11-
use function preg_match;
12-
1311
/**
1412
* @covers \PhpMyAdmin\Gis\GisMultiLineString
1513
* @runTestsInSeparateProcesses
@@ -328,8 +326,8 @@ public function testPrepareRowAsSvg(
328326
array $scaleData,
329327
string $output
330328
): void {
331-
$string = $this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData);
332-
$this->assertEquals(1, preg_match($output, $string));
329+
$svg = $this->object->prepareRowAsSvg($spatial, $label, $lineColor, $scaleData);
330+
$this->assertMatchesRegularExpression($output, $svg);
333331
}
334332

335333
/**

test/classes/Gis/GisMultiPointTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use PhpMyAdmin\Image\ImageWrapper;
99
use TCPDF;
1010

11-
use function preg_match;
12-
1311
/**
1412
* @covers \PhpMyAdmin\Gis\GisMultiPoint
1513
* @runTestsInSeparateProcesses
@@ -248,8 +246,8 @@ public function testPrepareRowAsSvg(
248246
array $scaleData,
249247
string $output
250248
): void {
251-
$string = $this->object->prepareRowAsSvg($spatial, $label, $pointColor, $scaleData);
252-
$this->assertEquals(1, preg_match($output, $string));
249+
$svg = $this->object->prepareRowAsSvg($spatial, $label, $pointColor, $scaleData);
250+
$this->assertMatchesRegularExpression($output, $svg);
253251
}
254252

255253
/**

test/classes/Gis/GisMultiPolygonTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use PhpMyAdmin\Image\ImageWrapper;
99
use TCPDF;
1010

11-
use function preg_match;
12-
1311
/**
1412
* @covers \PhpMyAdmin\Gis\GisMultiPolygon
1513
* @runTestsInSeparateProcesses
@@ -413,8 +411,8 @@ public function testPrepareRowAsSvg(
413411
array $scaleData,
414412
string $output
415413
): void {
416-
$string = $this->object->prepareRowAsSvg($spatial, $label, $fillColor, $scaleData);
417-
$this->assertEquals(1, preg_match($output, $string));
414+
$svg = $this->object->prepareRowAsSvg($spatial, $label, $fillColor, $scaleData);
415+
$this->assertMatchesRegularExpression($output, $svg);
418416
}
419417

420418
/**

test/classes/Gis/GisPointTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,8 @@ public function testPrepareRowAsSvg(
262262
array $scaleData,
263263
string $output
264264
): void {
265-
$this->assertEquals(
266-
$output,
267-
$this->object->prepareRowAsSvg(
268-
$spatial,
269-
$label,
270-
$pointColor,
271-
$scaleData
272-
)
273-
);
265+
$svg = $this->object->prepareRowAsSvg($spatial, $label, $pointColor, $scaleData);
266+
$this->assertMatchesRegularExpression($output, $svg);
274267
}
275268

276269
/**
@@ -291,7 +284,7 @@ public function providerForPrepareRowAsSvg(): array
291284
'scale' => 2,
292285
'height' => 150,
293286
],
294-
'',
287+
'/^$/',
295288
],
296289
];
297290
}

test/classes/Gis/GisPolygonTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use PhpMyAdmin\Image\ImageWrapper;
99
use TCPDF;
1010

11-
use function preg_match;
12-
1311
/**
1412
* @covers \PhpMyAdmin\Gis\GisPolygon
1513
* @runTestsInSeparateProcesses
@@ -499,8 +497,8 @@ public function testPrepareRowAsSvg(
499497
array $scaleData,
500498
string $output
501499
): void {
502-
$string = $this->object->prepareRowAsSvg($spatial, $label, $fillColor, $scaleData);
503-
$this->assertEquals(1, preg_match($output, $string));
500+
$svg = $this->object->prepareRowAsSvg($spatial, $label, $fillColor, $scaleData);
501+
$this->assertMatchesRegularExpression($output, $svg);
504502
}
505503

506504
/**

0 commit comments

Comments
 (0)