Skip to content

Commit 694dfb7

Browse files
committed
minor #5269 Prep work to migrate to PHPUnit 9.x (sanmai, keradus)
This PR was squashed before being merged into the 2.16 branch. Discussion ---------- Prep work to migrate to PHPUnit 9.x **This is a fully backward-compatible change.** The general idea is to make the package testable under PHP 5.6 - 7.x - 8.0. This requires using both PHPUnit 5.x and 9.x, because only the last works correctly under PHP 8 (as is - collects coverage report). But PHPUnit 9 has quite a lot of legacy methods removed, and PHPUnit 8 has these methods but with `void` return type. Therefore implementing these methods straight away is a no-go. This can be [with a version-dependent trait](https://github.com/symfony/phpunit-bridge/blob/5.x/Legacy/PolyfillAssertTrait.php), but for a small handful of functions used here it easier to do this by overloading them from `__call` and `__callStatic`. This is where `DeprecatedTestMethods` trait comes into play. And there are also `setUp(): void` etc. which are **not** taken care in this PR. - There's also above-mentioned `PolyfillAssertTrait` from `symfony/phpunit-bridge`, but it is tagged `@internal` so it's a risky game to use it. - And there's also [phpunitgoodpractices/polyfill](https://packagist.org/packages/phpunitgoodpractices/polyfill), but it should be trivial to switch to any of those down the line. --- Extracted from #5262 (it handles `setUp(): void` among other things) Commits ------- 3d8284e Prep work to migrate to PHPUnit 9.x
2 parents e3d378f + 3d8284e commit 694dfb7

File tree

67 files changed

+157
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+157
-153
lines changed

.composer-require-checker.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"PHPUnit\\Framework\\Constraint\\IsIdentical",
77
"PHPUnit\\Framework\\TestCase",
88
"PHPUnit\\Runner\\Version",
9+
"PHPUnitGoodPractices\\Polyfill\\PolyfillTrait",
910
"PHPUnitGoodPractices\\Traits\\ExpectationViaCodeOverAnnotationTrait",
1011
"PHPUnitGoodPractices\\Traits\\ExpectOverSetExceptionTrait",
1112
"PHPUnitGoodPractices\\Traits\\IdentityOverEqualityTrait",

.php_cs.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $config = PhpCsFixer\Config::create()
2323
->setRiskyAllowed(true)
2424
->setRules([
2525
'@PHP56Migration' => true,
26-
'@PHPUnit60Migration:risky' => true,
26+
'@PHPUnit75Migration:risky' => true,
2727
'@PhpCsFixer' => true,
2828
'@PhpCsFixer:risky' => true,
2929
'header_comment' => ['header' => $header],

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"php-cs-fixer/phpunit-constraint-isidenticalstring": "^1.2",
4242
"php-cs-fixer/phpunit-constraint-xmlmatchesxsd": "^1.2.1",
4343
"phpunit/phpunit": "^5.7.27 || ^6.5.14 || ^7.1",
44+
"phpunitgoodpractices/polyfill": "^1.5",
4445
"phpunitgoodpractices/traits": "^1.9.1",
4546
"symfony/phpunit-bridge": "^5.1",
4647
"symfony/yaml": "^3.0 || ^4.0 || ^5.0"

tests/AutoReview/ProjectCodeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
261261
}
262262

263263
foreach ($publicMethods as $method) {
264-
static::assertRegExp(
265-
'/^(test|provide|setUpBeforeClass$|tearDownAfterClass$)/',
264+
static::assertMatchesRegularExpression(
265+
'/^(test|expect|provide|setUpBeforeClass$|tearDownAfterClass$)/',
266266
$method->getName(),
267267
sprintf('Public method "%s::%s" is not properly named.', $reflectionClass->getName(), $method->getName())
268268
);
@@ -285,7 +285,7 @@ public function testThatDataProvidersAreCorrectlyNamed($testClassName)
285285
}
286286

287287
foreach ($usedDataProviderMethodNames as $dataProviderMethodName) {
288-
static::assertRegExp('/^provide[A-Z]\S+Cases$/', $dataProviderMethodName, sprintf(
288+
static::assertMatchesRegularExpression('/^provide[A-Z]\S+Cases$/', $dataProviderMethodName, sprintf(
289289
'Data provider in "%s" with name "%s" is not correctly named.',
290290
$testClassName,
291291
$dataProviderMethodName

tests/Cache/FileHandlerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testWriteThrowsIOExceptionIfFileCanNotBeWritten()
9999
$file = __DIR__.'/non-existent-directory/.php_cs.cache';
100100

101101
$this->expectException(\Symfony\Component\Filesystem\Exception\IOException::class);
102-
$this->expectExceptionMessageRegExp(sprintf(
102+
$this->expectExceptionMessageMatches(sprintf(
103103
'#^Directory of cache file "%s" does not exists.#',
104104
preg_quote($file, '#')
105105
));
@@ -135,7 +135,7 @@ public function testWriteCacheToDirectory()
135135
$handler = new FileHandler($dir);
136136

137137
$this->expectException(\Symfony\Component\Filesystem\Exception\IOException::class);
138-
$this->expectExceptionMessageRegExp(sprintf(
138+
$this->expectExceptionMessageMatches(sprintf(
139139
'#^%s$#',
140140
preg_quote('Cannot write cache file "'.realpath($dir).'" as the location exists as directory.', '#')
141141
));
@@ -153,7 +153,7 @@ public function testWriteCacheToNonWriteableFile()
153153
$handler = new FileHandler($file);
154154

155155
$this->expectException(\Symfony\Component\Filesystem\Exception\IOException::class);
156-
$this->expectExceptionMessageRegExp(sprintf(
156+
$this->expectExceptionMessageMatches(sprintf(
157157
'#^%s$#',
158158
preg_quote('Cannot write to file "'.realpath($file).'" as it is not writable.', '#')
159159
));
@@ -166,7 +166,7 @@ public function testWriteCacheFilePermissions()
166166
$file = __DIR__.'/../Fixtures/cache-file-handler/rw_cache.test';
167167
@unlink($file);
168168

169-
static::assertFileNotExists($file);
169+
static::assertFileDoesNotExist($file);
170170

171171
$handler = new FileHandler($file);
172172
$handler->write(new Cache($this->createSignature()));

tests/ConfigTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testConfigRulesUsingSeparateMethod()
4444
new ToolInfo()
4545
);
4646

47-
static::assertArraySubset(
47+
static::assertSame(
4848
[
4949
'cast_spaces' => true,
5050
'braces' => true,
@@ -65,7 +65,7 @@ public function testConfigRulesUsingJsonMethod()
6565
new ToolInfo()
6666
);
6767

68-
static::assertArraySubset(
68+
static::assertSame(
6969
[
7070
'array_syntax' => [
7171
'syntax' => 'short',
@@ -190,7 +190,7 @@ public function testThatMutatorHasFluentInterface()
190190
public function testRegisterCustomFixersWithInvalidArgument()
191191
{
192192
$this->expectException(\InvalidArgumentException::class);
193-
$this->expectExceptionMessageRegExp('/^Argument must be an array or a Traversable, got "\w+"\.$/');
193+
$this->expectExceptionMessageMatches('/^Argument must be an array or a Traversable, got "\w+"\.$/');
194194

195195
$config = new Config();
196196
$config->registerCustomFixers('foo');
@@ -257,7 +257,7 @@ public function testSetInvalidFinder()
257257
$config = new Config();
258258

259259
$this->expectException(\InvalidArgumentException::class);
260-
$this->expectExceptionMessageRegExp('/^Argument must be an array or a Traversable, got "integer"\.$/');
260+
$this->expectExceptionMessageMatches('/^Argument must be an array or a Traversable, got "integer"\.$/');
261261

262262
$config->setFinder(123);
263263
}

tests/Console/Command/DescribeCommandTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testExecuteWithUnknownRuleName()
129129
$commandTester = new CommandTester($command);
130130

131131
$this->expectException(\InvalidArgumentException::class);
132-
$this->expectExceptionMessageRegExp('#^Rule "Foo/bar" not found\.$#');
132+
$this->expectExceptionMessageMatches('#^Rule "Foo/bar" not found\.$#');
133133
$commandTester->execute([
134134
'command' => $command->getName(),
135135
'name' => 'Foo/bar',
@@ -146,7 +146,7 @@ public function testExecuteWithUnknownSetName()
146146
$commandTester = new CommandTester($command);
147147

148148
$this->expectException(\InvalidArgumentException::class);
149-
$this->expectExceptionMessageRegExp('#^Set "@NoSuchSet" not found\.$#');
149+
$this->expectExceptionMessageMatches('#^Set "@NoSuchSet" not found\.$#');
150150
$commandTester->execute([
151151
'command' => $command->getName(),
152152
'name' => '@NoSuchSet',
@@ -163,7 +163,7 @@ public function testExecuteWithoutName()
163163
$commandTester = new CommandTester($command);
164164

165165
$this->expectException(\RuntimeException::class);
166-
$this->expectExceptionMessageRegExp('/^Not enough arguments( \(missing: "name"\))?\.$/');
166+
$this->expectExceptionMessageMatches('/^Not enough arguments( \(missing: "name"\))?\.$/');
167167
$commandTester->execute([
168168
'command' => $command->getName(),
169169
]);
@@ -172,7 +172,7 @@ public function testExecuteWithoutName()
172172
public function testGetAlternativeSuggestion()
173173
{
174174
$this->expectException(\InvalidArgumentException::class);
175-
$this->expectExceptionMessageRegExp('#^Rule "Foo2/bar" not found\. Did you mean "Foo/bar"\?$#');
175+
$this->expectExceptionMessageMatches('#^Rule "Foo2/bar" not found\. Did you mean "Foo/bar"\?$#');
176176
$this->execute('Foo2/bar', false);
177177
}
178178

@@ -205,7 +205,7 @@ public function testFixerClassNameIsExposedWhenVerbose()
205205
]
206206
);
207207

208-
static::assertContains(\get_class($mock), $commandTester->getDisplay(true));
208+
static::assertStringContainsString(\get_class($mock), $commandTester->getDisplay(true));
209209
}
210210

211211
/**

tests/Console/Command/FixCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testEmptyRulesValue()
3131
$this->expectException(
3232
\PhpCsFixer\ConfigurationException\InvalidConfigurationException::class
3333
);
34-
$this->expectExceptionMessageRegExp(
34+
$this->expectExceptionMessageMatches(
3535
'#^Empty rules value is not allowed\.$#'
3636
);
3737

tests/Console/ConfigurationResolverTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class ConfigurationResolverTest extends TestCase
3737
public function testSetOptionWithUndefinedOption()
3838
{
3939
$this->expectException(InvalidConfigurationException::class);
40-
$this->expectExceptionMessageRegExp('/^Unknown option name: "foo"\.$/');
40+
$this->expectExceptionMessageMatches('/^Unknown option name: "foo"\.$/');
4141

4242
$this->createConfigurationResolver(['foo' => 'bar']);
4343
}
@@ -275,7 +275,7 @@ public function provideResolveConfigFileDefaultCases()
275275
public function testResolveConfigFileChooseFileWithInvalidFile()
276276
{
277277
$this->expectException(InvalidConfigurationException::class);
278-
$this->expectExceptionMessageRegExp(
278+
$this->expectExceptionMessageMatches(
279279
'#^The config file: ".+[\/\\\]Fixtures[\/\\\]ConfigurationResolverConfigFile[\/\\\]case_5[\/\\\]\.php_cs\.dist" does not return a "PhpCsFixer\\\ConfigInterface" instance\. Got: "string"\.$#'
280280
);
281281

@@ -289,7 +289,7 @@ public function testResolveConfigFileChooseFileWithInvalidFile()
289289
public function testResolveConfigFileChooseFileWithInvalidFormat()
290290
{
291291
$this->expectException(InvalidConfigurationException::class);
292-
$this->expectExceptionMessageRegExp('/^The format "xls" is not defined, supported are "checkstyle", "gitlab", "json", "junit", "txt", "xml"\.$/');
292+
$this->expectExceptionMessageMatches('/^The format "xls" is not defined, supported are "checkstyle", "gitlab", "json", "junit", "txt", "xml"\.$/');
293293

294294
$dirBase = $this->getFixtureDir();
295295

@@ -301,7 +301,7 @@ public function testResolveConfigFileChooseFileWithInvalidFormat()
301301
public function testResolveConfigFileChooseFileWithPathArrayWithoutConfig()
302302
{
303303
$this->expectException(InvalidConfigurationException::class);
304-
$this->expectExceptionMessageRegExp('/^For multiple paths config parameter is required\.$/');
304+
$this->expectExceptionMessageMatches('/^For multiple paths config parameter is required\.$/');
305305

306306
$dirBase = $this->getFixtureDir();
307307

@@ -1160,7 +1160,7 @@ public function testResolveUnknownDiffer()
11601160
]);
11611161

11621162
$this->expectException(\InvalidArgumentException::class);
1163-
$this->expectExceptionMessageRegExp('#^"diff\-format" must be any of "null", "sbd", "udiff", got "XXX"\.$#');
1163+
$this->expectExceptionMessageMatches('#^"diff\-format" must be any of "null", "sbd", "udiff", got "XXX"\.$#');
11641164

11651165
$resolver->getDiffer();
11661166
}
@@ -1210,7 +1210,7 @@ public function testWithEmptyRules()
12101210
$resolver = $this->createConfigurationResolver(['rules' => '']);
12111211

12121212
$this->expectException(InvalidConfigurationException::class);
1213-
$this->expectExceptionMessageRegExp('/^Empty rules value is not allowed\.$/');
1213+
$this->expectExceptionMessageMatches('/^Empty rules value is not allowed\.$/');
12141214

12151215
$resolver->getRules();
12161216
}

tests/Console/Output/ErrorOutputTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ public function testLintingExceptionOutputsAppliedFixersAndDiff()
134134

135135
$displayed = $this->readFullStreamOutput($output);
136136

137-
static::assertContains($fixerName, $displayed);
138-
static::assertContains($diffSpecificContext, $displayed);
137+
static::assertStringContainsString($fixerName, $displayed);
138+
static::assertStringContainsString($diffSpecificContext, $displayed);
139139

140-
static::assertContains($noDiffLintFixerName, $displayed);
140+
static::assertStringContainsString($noDiffLintFixerName, $displayed);
141141

142-
static::assertNotContains($invalidErrorFixerName, $displayed);
143-
static::assertNotContains($invalidDiff, $displayed);
142+
static::assertStringNotContainsString($invalidErrorFixerName, $displayed);
143+
static::assertStringNotContainsString($invalidDiff, $displayed);
144144
}
145145

146146
/**

tests/DocBlock/AnnotationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,9 @@ public function testSetTypesOnBadTag()
460460
public function testGetTagsWithTypes()
461461
{
462462
$tags = Annotation::getTagsWithTypes();
463-
static::assertInternalType('array', $tags);
463+
static::assertIsArray($tags);
464464
foreach ($tags as $tag) {
465-
static::assertInternalType('string', $tag);
465+
static::assertIsString($tag);
466466
static::assertNotEmpty($tag);
467467
}
468468
}

tests/DocBlock/DocBlockTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testGetLines()
6464
{
6565
$doc = new DocBlock(self::$sample);
6666

67-
static::assertInternalType('array', $doc->getLines());
67+
static::assertIsArray($doc->getLines());
6868
static::assertCount(15, $doc->getLines());
6969

7070
foreach ($doc->getLines() as $index => $line) {
@@ -79,7 +79,7 @@ public function testGetAnnotations()
7979
{
8080
$doc = new DocBlock(self::$sample);
8181

82-
static::assertInternalType('array', $doc->getAnnotations());
82+
static::assertIsArray($doc->getAnnotations());
8383
static::assertCount(5, $doc->getAnnotations());
8484

8585
foreach ($doc->getAnnotations() as $index => $annotations) {
@@ -96,7 +96,7 @@ public function testGetAnnotationsOfTypeParam()
9696

9797
$annotations = $doc->getAnnotationsOfType('param');
9898

99-
static::assertInternalType('array', $annotations);
99+
static::assertIsArray($annotations);
100100
static::assertCount(3, $annotations);
101101

102102
$first = ' * @param string $hello
@@ -118,7 +118,7 @@ public function testGetAnnotationsOfTypeThrows()
118118

119119
$annotations = $doc->getAnnotationsOfType('throws');
120120

121-
static::assertInternalType('array', $annotations);
121+
static::assertIsArray($annotations);
122122
static::assertCount(1, $annotations);
123123

124124
$content = ' * @throws \Exception asdnjkasd
@@ -135,7 +135,7 @@ public function testGetAnnotationsOfTypeReturn()
135135

136136
$annotations = $doc->getAnnotationsOfType('return');
137137

138-
static::assertInternalType('array', $annotations);
138+
static::assertIsArray($annotations);
139139
static::assertCount(1, $annotations);
140140

141141
$content = ' * @return void
@@ -150,7 +150,7 @@ public function testGetAnnotationsOfTypeFoo()
150150

151151
$annotations = $doc->getAnnotationsOfType('foo');
152152

153-
static::assertInternalType('array', $annotations);
153+
static::assertIsArray($annotations);
154154
static::assertCount(0, $annotations);
155155
}
156156

tests/Error/ErrorsManagerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testThatCanReportAndRetrieveInvalidErrors()
4848

4949
$errors = $errorsManager->getInvalidErrors();
5050

51-
static::assertInternalType('array', $errors);
51+
static::assertIsArray($errors);
5252
static::assertCount(1, $errors);
5353
static::assertSame($error, array_shift($errors));
5454

@@ -71,7 +71,7 @@ public function testThatCanReportAndRetrieveExceptionErrors()
7171

7272
$errors = $errorsManager->getExceptionErrors();
7373

74-
static::assertInternalType('array', $errors);
74+
static::assertIsArray($errors);
7575
static::assertCount(1, $errors);
7676
static::assertSame($error, array_shift($errors));
7777

@@ -94,7 +94,7 @@ public function testThatCanReportAndRetrieveInvalidFileErrors()
9494

9595
$errors = $errorsManager->getLintErrors();
9696

97-
static::assertInternalType('array', $errors);
97+
static::assertIsArray($errors);
9898
static::assertCount(1, $errors);
9999
static::assertSame($error, array_shift($errors));
100100

tests/FileReaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testThrowsExceptionOnFail()
7373
$reader = new FileReader();
7474

7575
$this->expectException(\RuntimeException::class);
76-
$this->expectExceptionMessageRegExp('#^Failed to read content from "'.preg_quote($nonExistentFilePath, '#').'.*$#');
76+
$this->expectExceptionMessageMatches('#^Failed to read content from "'.preg_quote($nonExistentFilePath, '#').'.*$#');
7777

7878
$reader->read($nonExistentFilePath);
7979
}

tests/FileRemovalTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testShutdownRemovesObservedFilesSetup()
6767
*/
6868
public function testShutdownRemovesObservedFiles()
6969
{
70-
static::assertFileNotExists(sys_get_temp_dir().'/cs_fixer_foo.php');
70+
static::assertFileDoesNotExist(sys_get_temp_dir().'/cs_fixer_foo.php');
7171
static::assertFileExists(sys_get_temp_dir().'/cs_fixer_bar.php');
7272
}
7373

@@ -82,8 +82,8 @@ public function testCleanRemovesObservedFiles()
8282

8383
$fileRemoval->clean();
8484

85-
static::assertFileNotExists($fs->url().'/foo.php');
86-
static::assertFileNotExists($fs->url().'/baz.php');
85+
static::assertFileDoesNotExist($fs->url().'/foo.php');
86+
static::assertFileDoesNotExist($fs->url().'/baz.php');
8787
static::assertFileExists($fs->url().'/bar.php');
8888
}
8989

@@ -98,8 +98,8 @@ public function testDestructRemovesObservedFiles()
9898

9999
$fileRemoval->__destruct();
100100

101-
static::assertFileNotExists($fs->url().'/foo.php');
102-
static::assertFileNotExists($fs->url().'/baz.php');
101+
static::assertFileDoesNotExist($fs->url().'/foo.php');
102+
static::assertFileDoesNotExist($fs->url().'/baz.php');
103103
static::assertFileExists($fs->url().'/bar.php');
104104
}
105105

@@ -114,7 +114,7 @@ public function testDeleteObservedFile()
114114

115115
$fileRemoval->delete($fs->url().'/foo.php');
116116

117-
static::assertFileNotExists($fs->url().'/foo.php');
117+
static::assertFileDoesNotExist($fs->url().'/foo.php');
118118
static::assertFileExists($fs->url().'/baz.php');
119119
}
120120

@@ -126,7 +126,7 @@ public function testDeleteNonObservedFile()
126126

127127
$fileRemoval->delete($fs->url().'/foo.php');
128128

129-
static::assertFileNotExists($fs->url().'/foo.php');
129+
static::assertFileDoesNotExist($fs->url().'/foo.php');
130130
}
131131

132132
private function getMockFileSystem()

0 commit comments

Comments
 (0)