Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: allow the test suite to run on PHPUnit 8.x and 9.x #3803

Closed
wants to merge 9 commits into from
19 changes: 1 addition & 18 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,37 +114,20 @@ jobs:

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies - normal
if: ${{ matrix.php < '8.0' }}
- name: Install Composer dependencies
uses: "ramsey/composer-install@v2"
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

# For PHP 8.0+, we need to install with ignore platform reqs as PHPUnit 7 is still used.
- name: Install Composer dependencies - with ignore platform
if: ${{ matrix.php >= '8.0' }}
uses: "ramsey/composer-install@v2"
with:
composer-options: --ignore-platform-reqs
custom-cache-suffix: $(date -u "+%Y-%m")

# Note: The code style check is run multiple times against every PHP version
# as it also acts as an integration test.
- name: 'PHPCS: set the path to PHP'
run: php bin/phpcs --config-set php_path php

- name: 'PHPUnit: run the tests'
if: ${{ matrix.php != '8.1' && matrix.php != '8.2' }}
run: vendor/bin/phpunit tests/AllTests.php

# We need to ignore the config file so that PHPUnit doesn't try to read it.
# The config file causes an error on PHP 8.1+ with PHPunit 7, but it's not needed here anyway
# as we can pass all required settings in the phpunit command.
- name: 'PHPUnit: run the tests on PHP > 8.0'
if: ${{ matrix.php == '8.1' || matrix.php == '8.2' }}
run: vendor/bin/phpunit tests/AllTests.php --no-configuration --bootstrap=tests/bootstrap.php --dont-report-useless-tests

- name: 'PHPCS: check code style without cache, no parallel'
if: ${{ matrix.custom_ini == false && matrix.php != '7.4' }}
run: php bin/phpcs --no-cache --parallel=1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.idea/*
/vendor/
composer.lock
.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"ext-simplexml": "*"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
},
"bin": [
"bin/phpcs",
Expand Down
12 changes: 11 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit beStrictAboutTestsThatDoNotTestAnything="false" bootstrap="tests/bootstrap.php">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.2/phpunit.xsd"
backupGlobals="true"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="tests/bootstrap.php"
convertErrorsToExceptions="true"
convertWarningsToExceptions="true"
convertNoticesToExceptions="true"
convertDeprecationsToExceptions="true"
>
<testsuites>
<testsuite name="PHP_CodeSniffer Test Suite">
<file>tests/AllTests.php</file>
Expand Down
16 changes: 9 additions & 7 deletions src/Standards/Generic/Tests/Debug/ESLintUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,33 @@ class ESLintUnitTest extends AbstractSniffUnitTest
/**
* Sets up this unit test.
*
* @before
*
* @return void
*/
protected function setUp()
protected function setUpPrerequisites()
{
parent::setUp();
parent::setUpPrerequisites();

$cwd = getcwd();
file_put_contents($cwd.'/.eslintrc.json', self::ESLINT_CONFIG);

}//end setUp()
}//end setUpPrerequisites()


/**
* Remove artifact.
*
* @after
*
* @return void
*/
protected function tearDown()
protected function resetProperties()
{
parent::tearDown();

$cwd = getcwd();
unlink($cwd.'/.eslintrc.json');

}//end tearDown()
}//end resetProperties()


/**
Expand Down
2 changes: 1 addition & 1 deletion tests/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$phpunit7 = false;
if (class_exists('\PHPUnit\Runner\Version') === true) {
$version = \PHPUnit\Runner\Version::id();
if ($version[0] === '7') {
if (version_compare($version, '7.0', '>=') === true) {
$phpunit7 = true;
}
}
Expand Down
12 changes: 8 additions & 4 deletions tests/Core/AbstractMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ abstract class AbstractMethodUnitTest extends TestCase
* The test case file for a unit test class has to be in the same directory
* directory and use the same file name as the test class, using the .inc extension.
*
* @beforeClass
*
* @return void
*/
public static function setUpBeforeClass()
public static function initializeFile()
{
$config = new Config();
$config->standards = ['PSR1'];
Expand All @@ -62,19 +64,21 @@ public static function setUpBeforeClass()
self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
self::$phpcsFile->process();

}//end setUpBeforeClass()
}//end initializeFile()


/**
* Clean up after finished test.
*
* @afterClass
*
* @return void
*/
public static function tearDownAfterClass()
public static function resetFile()
{
self::$phpcsFile = null;

}//end tearDownAfterClass()
}//end resetFile()


/**
Expand Down
6 changes: 4 additions & 2 deletions tests/Core/Autoloader/DetermineLoadedClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ class DetermineLoadedClassTest extends TestCase
/**
* Load the test files.
*
* @beforeClass
*
* @return void
*/
public static function setUpBeforeClass()
public static function includeFixture()
{
include __DIR__.'/TestFiles/Sub/C.inc';

}//end setUpBeforeClass()
}//end includeFixture()


/**
Expand Down
15 changes: 12 additions & 3 deletions tests/Core/File/GetClassPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ class GetClassPropertiesTest extends AbstractMethodUnitTest
*
* @dataProvider dataNotAClassException
*
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
* @expectedExceptionMessage $stackPtr must be of type T_CLASS
*
* @return void
*/
public function testNotAClassException($testMarker, $tokenType)
{
$msg = '$stackPtr must be of type T_CLASS';
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';

if (\method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($msg);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $msg);
}

$target = $this->getTargetToken($testMarker, $tokenType);
self::$phpcsFile->getClassProperties($target);

Expand Down
39 changes: 32 additions & 7 deletions tests/Core/File/GetMemberPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function testGetMemberProperties($identifier, $expected)
$variable = $this->getTargetToken($identifier, T_VARIABLE);
$result = self::$phpcsFile->getMemberProperties($variable);

$this->assertArraySubset($expected, $result, true);
// Unset those indexes which are not being tested.
unset($result['type_token'], $result['type_end_token']);

$this->assertSame($expected, $result);

}//end testGetMemberProperties()

Expand Down Expand Up @@ -764,6 +767,7 @@ public function dataGetMemberProperties()
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'is_readonly' => false,
'type' => 'Foo&Bar',
'nullable_type' => false,
],
Expand All @@ -774,6 +778,7 @@ public function dataGetMemberProperties()
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'is_readonly' => false,
'type' => 'Foo&Bar&Baz',
'nullable_type' => false,
],
Expand All @@ -784,6 +789,7 @@ public function dataGetMemberProperties()
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'is_readonly' => false,
'type' => 'int&string',
'nullable_type' => false,
],
Expand All @@ -794,6 +800,7 @@ public function dataGetMemberProperties()
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'is_readonly' => false,
'type' => '?Foo&Bar',
'nullable_type' => true,
],
Expand All @@ -808,15 +815,24 @@ public function dataGetMemberProperties()
*
* @param string $identifier Comment which precedes the test case.
*
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
* @expectedExceptionMessage $stackPtr is not a class member var
*
* @dataProvider dataNotClassProperty
*
* @return void
*/
public function testNotClassPropertyException($identifier)
{
$msg = '$stackPtr is not a class member var';
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';

if (\method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($msg);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $msg);
}

$variable = $this->getTargetToken($identifier, T_VARIABLE);
$result = self::$phpcsFile->getMemberProperties($variable);

Expand Down Expand Up @@ -848,13 +864,22 @@ public function dataNotClassProperty()
/**
* Test receiving an expected exception when a non variable is passed.
*
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
* @expectedExceptionMessage $stackPtr must be of type T_VARIABLE
*
* @return void
*/
public function testNotAVariableException()
{
$msg = '$stackPtr must be of type T_VARIABLE';
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';

if (\method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($msg);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $msg);
}

$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
$result = self::$phpcsFile->getMemberProperties($next);

Expand Down
29 changes: 25 additions & 4 deletions tests/Core/File/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public function testSingleDefaultValue()
$expected[0] = [
'name' => '$var1',
'content' => '$var1=self::CONSTANT',
'has_attributes' => false,
'default' => 'self::CONSTANT',
'has_attributes' => false,
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
Expand All @@ -119,8 +119,8 @@ public function testDefaultValues()
$expected[0] = [
'name' => '$var1',
'content' => '$var1=1',
'has_attributes' => false,
'default' => '1',
'has_attributes' => false,
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
Expand All @@ -129,8 +129,8 @@ public function testDefaultValues()
$expected[1] = [
'name' => '$var2',
'content' => "\$var2='value'",
'has_attributes' => false,
'default' => "'value'",
'has_attributes' => false,
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => '',
Expand Down Expand Up @@ -900,6 +900,7 @@ public function testPHP8ConstructorPropertyPromotionGlobalFunction()
'type_hint' => '',
'nullable_type' => false,
'property_visibility' => 'private',
'property_readonly' => false,
];

$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
Expand All @@ -924,6 +925,7 @@ public function testPHP8ConstructorPropertyPromotionAbstractMethod()
'type_hint' => 'callable',
'nullable_type' => false,
'property_visibility' => 'public',
'property_readonly' => false,
];
$expected[1] = [
'name' => '$x',
Expand All @@ -934,6 +936,7 @@ public function testPHP8ConstructorPropertyPromotionAbstractMethod()
'type_hint' => '',
'nullable_type' => false,
'property_visibility' => 'private',
'property_readonly' => false,
];

$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
Expand All @@ -953,6 +956,7 @@ public function testCommentsInParameter()
'name' => '$param',
'content' => '// Leading comment.
?MyClass /*-*/ & /*-*/.../*-*/ $param /*-*/ = /*-*/ \'default value\' . /*-*/ \'second part\' // Trailing comment.',
'default' => '\'default value\' . /*-*/ \'second part\' // Trailing comment.',
'has_attributes' => false,
'pass_by_reference' => true,
'variable_length' => true,
Expand Down Expand Up @@ -982,6 +986,7 @@ public function testParameterAttributesInFunctionDeclaration()
'type_hint' => 'string',
'nullable_type' => false,
'property_visibility' => 'private',
'property_readonly' => false,
];
$expected[1] = [
'name' => '$typedParamSingleAttribute',
Expand Down Expand Up @@ -1174,7 +1179,23 @@ private function getMethodParametersTestHelper($commentString, $expected)
$function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE, T_FN]);
$found = self::$phpcsFile->getMethodParameters($function);

$this->assertArraySubset($expected, $found, true);
// Unset those indexes which are not being tested.
foreach ($found as $i => $param) {
unset(
$found[$i]['token'],
$found[$i]['reference_token'],
$found[$i]['variadic_token'],
$found[$i]['type_hint_token'],
$found[$i]['type_hint_end_token'],
$found[$i]['comma_token'],
$found[$i]['default_token'],
$found[$i]['default_equal_token'],
$found[$i]['visibility_token'],
$found[$i]['readonly_token']
);
}

$this->assertSame($expected, $found);

}//end getMethodParametersTestHelper()

Expand Down
Loading