Skip to content

Commit 81c901f

Browse files
committed
Compat fix for phpunit 7
1 parent 497cfb3 commit 81c901f

File tree

5 files changed

+76
-25
lines changed

5 files changed

+76
-25
lines changed

package.xml

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5959
-- Thanks to Juliette Reinders Folmer for the patch
6060
- Improved auto-fixing of files with incomplete comment blocks for various commenting sniffs
6161
-- Thanks to Juliette Reinders Folmer for the patch
62+
- Fixed test suite compatibility with PHPUnit 7
6263
- Fixed bug #1793 : PSR2 forcing exact indent for function call opening statements
6364
- Fixed bug #1803 : Squiz.WhiteSpace.ScopeKeywordSpacing removes member var name while fixing if no space after scope keyword
6465
- Fixed bug #1817 : Blank line not enforced after control structure if comment on same line as closing brace
@@ -124,6 +125,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
124125
<file baseinstalldir="" name="AllTests.php" role="test" />
125126
<file baseinstalldir="" name="bootstrap.php" role="test" />
126127
<file baseinstalldir="" name="TestSuite.php" role="test" />
128+
<file baseinstalldir="" name="TestSuite7.php" role="test" />
127129
</dir>
128130
<dir name="src">
129131
<file baseinstalldir="PHP/CodeSniffer" name="Config.php" role="php">
@@ -1594,6 +1596,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
15941596
<install as="phpunit.xml" name="phpunit.xml.dist" />
15951597
<install as="AllTests.php" name="tests/AllTests.php" />
15961598
<install as="TestSuite.php" name="tests/TestSuite.php" />
1599+
<install as="TestSuite7.php" name="tests/TestSuite7.php" />
15971600
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
15981601
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
15991602
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
@@ -1621,6 +1624,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
16211624
<install as="tests/AllTests.php" name="tests/AllTests.php" />
16221625
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
16231626
<install as="tests/TestSuite.php" name="tests/TestSuite.php" />
1627+
<install as="tests/TestSuite7.php" name="tests/TestSuite7.php" />
16241628
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
16251629
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
16261630
<install as="CodeSniffer/Core/ErrorSuppressionTest.php" name="tests/Core/ErrorSuppressionTest.php" />

tests/AllTests.php

+16-13
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,27 @@
2020
include_once 'CodeSniffer/Standards/AllSniffs.php';
2121
}
2222

23-
require_once 'TestSuite.php';
23+
// PHPUnit 7 made the TestSuite run() method incompatible with
24+
// older PHPUnit versions due to return type hints, so maintain
25+
// two different suite objects.
26+
$phpunit7 = false;
27+
if (class_exists('\PHPUnit\Runner\Version') === true) {
28+
$version = \PHPUnit\Runner\Version::id();
29+
if ($version[0] === '7') {
30+
$phpunit7 = true;
31+
}
32+
}
33+
34+
if ($phpunit7 === true) {
35+
include_once 'TestSuite7.php';
36+
} else {
37+
include_once 'TestSuite.php';
38+
}
2439

2540
class PHP_CodeSniffer_AllTests
2641
{
2742

2843

29-
/**
30-
* Prepare the test runner.
31-
*
32-
* @return void
33-
*/
34-
public static function main()
35-
{
36-
TestRunner::run(self::suite());
37-
38-
}//end main()
39-
40-
4144
/**
4245
* Add all PHP_CodeSniffer test suites into a single test suite.
4346
*

tests/TestSuite.php

+1-11
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,7 @@ class TestSuite extends PHPUnit_TestSuite
2626
public function run(TestResult $result=null)
2727
{
2828
$result = parent::run($result);
29-
30-
$codes = count($GLOBALS['PHP_CODESNIFFER_SNIFF_CODES']);
31-
32-
echo PHP_EOL.PHP_EOL;
33-
echo "Tests generated $codes unique error codes";
34-
if ($codes > 0) {
35-
$fixes = count($GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES']);
36-
$percent = round(($fixes / $codes * 100), 2);
37-
echo "; $fixes were fixable ($percent%)";
38-
}
39-
29+
printPHPCodeSnifferTestOutput();
4030
return $result;
4131

4232
}//end run()

tests/TestSuite7.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* A PHP_CodeSniffer specific test suite for PHPUnit.
4+
*
5+
* @author Greg Sherwood <[email protected]>
6+
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests;
11+
12+
use PHPUnit\Framework\TestSuite as PHPUnit_TestSuite;
13+
use PHPUnit\Framework\TestResult;
14+
15+
class TestSuite extends PHPUnit_TestSuite
16+
{
17+
18+
19+
/**
20+
* Runs the tests and collects their result in a TestResult.
21+
*
22+
* @param \PHPUnit\Framework\TestResult $result A test result.
23+
*
24+
* @return \PHPUnit\Framework\TestResult
25+
*/
26+
public function run(TestResult $result=null): TestResult
27+
{
28+
$result = parent::run($result);
29+
printPHPCodeSnifferTestOutput();
30+
return $result;
31+
32+
}//end run()
33+
34+
35+
}//end class

tests/bootstrap.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
$tokens = new \PHP_CodeSniffer\Util\Tokens();
2929

30-
3130
// Compatibility for PHPUnit < 6 and PHPUnit 6+.
3231
if (class_exists('PHPUnit_Framework_TestSuite') === true && class_exists('PHPUnit\Framework\TestSuite') === false) {
3332
class_alias('PHPUnit_Framework_TestSuite', 'PHPUnit'.'\Framework\TestSuite');
@@ -44,3 +43,23 @@ class_alias('PHPUnit_TextUI_TestRunner', 'PHPUnit'.'\TextUI\TestRunner');
4443
if (class_exists('PHPUnit_Framework_TestResult') === true && class_exists('PHPUnit\Framework\TestResult') === false) {
4544
class_alias('PHPUnit_Framework_TestResult', 'PHPUnit'.'\Framework\TestResult');
4645
}
46+
47+
48+
/**
49+
* A global util function to help print unit test fixing data.
50+
*
51+
* @return void
52+
*/
53+
function printPHPCodeSnifferTestOutput()
54+
{
55+
$codes = count($GLOBALS['PHP_CODESNIFFER_SNIFF_CODES']);
56+
57+
echo PHP_EOL.PHP_EOL;
58+
echo "Tests generated $codes unique error codes";
59+
if ($codes > 0) {
60+
$fixes = count($GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES']);
61+
$percent = round(($fixes / $codes * 100), 2);
62+
echo "; $fixes were fixable ($percent%)";
63+
}
64+
65+
}//end printPHPCodeSnifferTestOutput()

0 commit comments

Comments
 (0)