Skip to content

Commit 33a89f1

Browse files
committed
Make the unit test compatible with PHPUnit v6
Since mid March this year, Travis uses PHPUnit 6.x for PHP 7+. The unit tests were not compatible with this and failing because of it unless another version of PHPUnit was installed. These relatively minor changes fix that and makes the tests compatible with both PHPUnit < 6 as well as 6+. Notes: * I've elected to use the PHPUnit 6 class names as the basis to allow for eventually dropping PHPUnit 4.x support. * I've moved some duplicate code which was used in the three test classes which are used as entry points to the boostrap. * The Travis image for HHVM ships with an incompatible version of PHPUnit. This is circumvented by installing a lower version PHPUnit just and only for HHVM. Ref: https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-6.0.0
1 parent 134a0ca commit 33a89f1

15 files changed

+91
-82
lines changed

.travis.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ matrix:
1919
- php: nightly
2020
- php: hhvm
2121

22+
before_install:
23+
# Circumvent a bug in the travis HHVM image - ships with incompatible PHPUnit.
24+
- if [[ $TRAVIS_PHP_VERSION == hhv* ]]; then composer self-update; fi
25+
- if [[ $TRAVIS_PHP_VERSION == hhv* ]]; then composer require phpunit/phpunit:~4.0; fi
26+
- if [[ $TRAVIS_PHP_VERSION == hhv* ]]; then composer install; fi
27+
2228
before_script:
2329
- if [[ $CUSTOM_INI == "1" && ${TRAVIS_PHP_VERSION:0:1} == "5" ]]; then phpenv config-add php5-testingConfig.ini; fi
2430
- if [[ $CUSTOM_INI == "1" ]] && [[ ${TRAVIS_PHP_VERSION:0:1} == "7" || $TRAVIS_PHP_VERSION == "nightly" ]]; then phpenv config-add php7-testingConfig.ini; fi
2531

2632
script:
27-
- composer install
2833
- if [[ $TRAVIS_PHP_VERSION != hhv* ]]; then php bin/phpcs --config-set php_path php; fi
29-
- vendor/bin/phpunit tests/AllTests.php
34+
- if [[ $TRAVIS_PHP_VERSION != hhv* ]]; then phpunit tests/AllTests.php; fi
35+
- if [[ $TRAVIS_PHP_VERSION == hhv* ]]; then vendor/bin/phpunit tests/AllTests.php; fi
3036
- if [[ $CUSTOM_INI != "1" ]]; then php bin/phpcs --no-cache --parallel=1; fi
3137
- if [[ $CUSTOM_INI != "1" && $TRAVIS_PHP_VERSION != hhv* && $TRAVIS_PHP_VERSION != "nightly" ]]; then pear package-validate package.xml; fi
3238
- if [[ $CUSTOM_INI != "1" && $TRAVIS_PHP_VERSION != hhv* ]]; then php scripts/build-phar.php; fi

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"ext-simplexml": "*"
3232
},
3333
"require-dev": {
34-
"phpunit/phpunit": "~4.0"
34+
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0"
3535
},
3636
"bin": [
3737
"bin/phpcs",

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
8282
<file baseinstalldir="" name="AbstractSniffUnitTest.php" role="test" />
8383
<file baseinstalldir="" name="AllSniffs.php" role="test" />
8484
</dir>
85+
<file baseinstalldir="" name="bootstrap.php" role="test" />
8586
<file baseinstalldir="" name="AllTests.php" role="test" />
8687
<file baseinstalldir="" name="TestSuite.php" role="test" />
8788
</dir>

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit>
2+
<phpunit backupGlobals="true" bootstrap="tests/bootstrap.php">
33
<testsuites>
44
<testsuite name="PHP_CodeSniffer Test Suite">
55
<file>tests/AllTests.php</file>

tests/AllTests.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,19 @@
99

1010
namespace PHP_CodeSniffer\Tests;
1111

12-
use PHP_CodeSniffer\Util\Tokens;
13-
14-
if (defined('PHP_CODESNIFFER_IN_TESTS') === false) {
15-
define('PHP_CODESNIFFER_IN_TESTS', true);
16-
}
17-
18-
if (defined('PHP_CODESNIFFER_CBF') === false) {
19-
define('PHP_CODESNIFFER_CBF', false);
20-
}
21-
22-
if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {
23-
define('PHP_CODESNIFFER_VERBOSITY', 0);
24-
}
12+
use PHP_CodeSniffer\Tests\TestSuite;
13+
use PHPUnit\TextUI\TestRunner;
2514

2615
if (is_file(__DIR__.'/../autoload.php') === true) {
27-
include_once __DIR__.'/../autoload.php';
2816
include_once 'Core/AllTests.php';
2917
include_once 'Standards/AllSniffs.php';
3018
} else {
31-
include_once 'PHP/CodeSniffer/autoload.php';
3219
include_once 'CodeSniffer/Core/AllTests.php';
3320
include_once 'CodeSniffer/Standards/AllSniffs.php';
3421
}
3522

3623
require_once 'TestSuite.php';
3724

38-
$tokens = new Tokens();
39-
4025
class PHP_CodeSniffer_AllTests
4126
{
4227

@@ -48,15 +33,15 @@ class PHP_CodeSniffer_AllTests
4833
*/
4934
public static function main()
5035
{
51-
\PHPUnit_TextUI_TestRunner::run(self::suite());
36+
TestRunner::run(self::suite());
5237

5338
}//end main()
5439

5540

5641
/**
5742
* Add all PHP_CodeSniffer test suites into a single test suite.
5843
*
59-
* @return \PHPUnit_Framework_TestSuite
44+
* @return \PHPUnit\Framework\TestSuite
6045
*/
6146
public static function suite()
6247
{

tests/Core/AllTests.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,8 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core;
1111

12-
use PHP_CodeSniffer\Util\Tokens;
13-
14-
if (defined('PHP_CODESNIFFER_IN_TESTS') === false) {
15-
define('PHP_CODESNIFFER_IN_TESTS', true);
16-
}
17-
18-
if (defined('PHP_CODESNIFFER_CBF') === false) {
19-
define('PHP_CODESNIFFER_CBF', false);
20-
}
21-
22-
if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {
23-
define('PHP_CODESNIFFER_VERBOSITY', 0);
24-
}
25-
26-
if (is_file(__DIR__.'/../../autoload.php') === true) {
27-
include_once __DIR__.'/../../autoload.php';
28-
} else {
29-
include_once 'PHP/CodeSniffer/autoload.php';
30-
}
31-
32-
$tokens = new Tokens();
12+
use PHPUnit\TextUI\TestRunner;
13+
use PHPUnit\Framework\TestSuite;
3314

3415
require_once 'IsCamelCapsTest.php';
3516
require_once 'ErrorSuppressionTest.php';
@@ -48,19 +29,19 @@ class AllTests
4829
*/
4930
public static function main()
5031
{
51-
\PHPUnit2_TextUI_TestRunner::run(self::suite());
32+
TestRunner::run(self::suite());
5233

5334
}//end main()
5435

5536

5637
/**
5738
* Add all core unit tests into a test suite.
5839
*
59-
* @return \PHPUnit_Framework_TestSuite
40+
* @return \PHPUnit\Framework\TestSuite
6041
*/
6142
public static function suite()
6243
{
63-
$suite = new \PHPUnit_Framework_TestSuite('PHP CodeSniffer Core');
44+
$suite = new TestSuite('PHP CodeSniffer Core');
6445
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\IsCamelCapsTest');
6546
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\ErrorSuppressionTest');
6647
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodParametersTest');

tests/Core/ErrorSuppressionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use PHP_CodeSniffer\Config;
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHP_CodeSniffer\Files\DummyFile;
15+
use PHPUnit\Framework\TestCase;
1516

16-
class ErrorSuppressionTest extends \PHPUnit_Framework_TestCase
17+
class ErrorSuppressionTest extends TestCase
1718
{
1819

1920

tests/Core/File/FindExtendedClassNameTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use PHP_CodeSniffer\Config;
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHP_CodeSniffer\Files\DummyFile;
15+
use PHPUnit\Framework\TestCase;
1516

16-
class FindExtendedClassNameTest extends \PHPUnit_Framework_TestCase
17+
class FindExtendedClassNameTest extends TestCase
1718
{
1819

1920
/**

tests/Core/File/FindImplementedInterfaceNamesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use PHP_CodeSniffer\Config;
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHP_CodeSniffer\Files\DummyFile;
15+
use PHPUnit\Framework\TestCase;
1516

16-
class FindImplementedInterfaceNamesTest extends \PHPUnit_Framework_TestCase
17+
class FindImplementedInterfaceNamesTest extends TestCase
1718
{
1819

1920
/**

tests/Core/File/GetMethodParametersTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use PHP_CodeSniffer\Config;
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHP_CodeSniffer\Files\DummyFile;
15+
use PHPUnit\Framework\TestCase;
1516

16-
class GetMethodParametersTest extends \PHPUnit_Framework_TestCase
17+
class GetMethodParametersTest extends TestCase
1718
{
1819

1920
/**

0 commit comments

Comments
 (0)