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: use PHPUnit Polyfills #277

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

namespace PHPCSUtils\Tests\AbstractSniffs\AbstractArrayDeclaration;

use PHPCSUtils\Tests\AssertAttributeSame;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;

/**
* Tests for the \PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff class.
Expand All @@ -22,10 +21,8 @@
*
* @since 1.0.0
*/
class AbstractArrayDeclarationSniffTest extends UtilityMethodTestCase
class AbstractArrayDeclarationSniffTest extends PolyfilledTestCase
{
// Backfill for the assertAttributeSame() method on PHPUnit 9.x.
use AssertAttributeSame;

/**
* List of methods in the abstract which should be mocked.
Expand Down
15 changes: 4 additions & 11 deletions Tests/BackCompat/Helper/ConfigDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHP_CodeSniffer\Config;
use PHPCSUtils\BackCompat\Helper;
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;

/**
* Test class.
Expand All @@ -26,6 +27,7 @@
*/
class ConfigDataTest extends TestCase
{
use ExpectException;

/**
* Test the getConfigData() and setConfigData() method when used in a cross-version compatible manner.
Expand Down Expand Up @@ -87,17 +89,8 @@ public function testConfigDataPHPCS4Exception()
$this->markTestSkipped('Test only applicable to PHPCS 4.x');
}

$msg = 'Passing the $config parameter is required in PHPCS 4.x';
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';

if (\method_exists($this, 'expectException')) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($msg);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $msg);
}
$this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException');
$this->expectExceptionMessage('Passing the $config parameter is required in PHPCS 4.x');

Helper::setConfigData('arbitrary_name', 'test', true);
}
Expand Down
65 changes: 65 additions & 0 deletions Tests/PolyfilledTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests;

use PHPCSUtils\Tests\AssertAttributeSame;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;

/**
* Abstract utilty method base test case which includes all available polyfills.
*
* This test case includes all polyfills from the PHPUnit Polyfill library to make them
* available to the tests.
*
* Generally speaking, this testcase only needs to be used when the concrete test class will
* use functionality which has changed in PHPUnit cross-version.
* In all other cases, the `UtilityMethodTestCase` can be extended directly.
*
* {@internal The list of included polyfill traits should be reviewed after each new
* release of the PHPUnit Polyfill library.}
*
* @since 1.0.0
*/
abstract class PolyfilledTestCase extends UtilityMethodTestCase
{
// PHPCSUtils native helper.
use AssertAttributeSame;

// PHPUnit Polyfills.
use AssertClosedResource;
use AssertEqualsSpecializations;
use AssertFileDirectory;
use AssertFileEqualsSpecializations;
use AssertionRenames;
use AssertIsType;
use AssertNumericType;
use AssertObjectEquals;
use AssertStringContains;
use EqualToSpecializations;
use ExpectException;
use ExpectExceptionMessageMatches;
use ExpectExceptionObject;
use ExpectPHPException;
}
14 changes: 4 additions & 10 deletions Tests/TestUtils/UtilityMethodTestCase/FailedToTokenizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace PHPCSUtils\Tests\TestUtils\UtilityMethodTestCase;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;

/**
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
Expand All @@ -21,7 +21,7 @@
*
* @since 1.0.0
*/
class FailedToTokenizeTest extends UtilityMethodTestCase
class FailedToTokenizeTest extends PolyfilledTestCase
{

/**
Expand Down Expand Up @@ -50,14 +50,8 @@ public function testMissingCaseFile()
$exception = 'PHPUnit_Framework_AssertionFailedError';
}

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

parent::setUpTestFile();
}
Expand Down
14 changes: 4 additions & 10 deletions Tests/TestUtils/UtilityMethodTestCase/MissingCaseFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace PHPCSUtils\Tests\TestUtils\UtilityMethodTestCase;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;

/**
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
Expand All @@ -21,7 +21,7 @@
*
* @since 1.0.0
*/
class MissingCaseFileTest extends UtilityMethodTestCase
class MissingCaseFileTest extends PolyfilledTestCase
{

/**
Expand Down Expand Up @@ -50,14 +50,8 @@ public function testMissingCaseFile()
$exception = 'PHPUnit_Framework_AssertionFailedError';
}

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

parent::setUpTestFile();
}
Expand Down
14 changes: 4 additions & 10 deletions Tests/TestUtils/UtilityMethodTestCase/SkipCSJSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace PHPCSUtils\Tests\TestUtils\UtilityMethodTestCase;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;

/**
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
Expand All @@ -21,7 +21,7 @@
*
* @since 1.0.0
*/
class SkipJSCSSTest extends UtilityMethodTestCase
class SkipJSCSSTest extends PolyfilledTestCase
{

/**
Expand Down Expand Up @@ -58,14 +58,8 @@ public function testSkipJsCss()
$exception = 'PHPUnit_Framework_SkippedTestError';
}

if (\method_exists($this, 'expectException')) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($msg);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $msg);
}
$this->expectException($exception);
$this->expectExceptionMessage($msg);
} else {
// Get rid of the "does not perform assertions" warning when run with PHPCS 3.x.
$this->assertTrue(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Exceptions\TokenizerException;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;

/**
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
Expand All @@ -23,7 +23,7 @@
*
* @since 1.0.0
*/
class UtilityMethodTestCaseTest extends UtilityMethodTestCase
class UtilityMethodTestCaseTest extends PolyfilledTestCase
{

/**
Expand Down Expand Up @@ -51,13 +51,7 @@ public function testSetUp()
$this->assertSame(57, self::$phpcsFile->numTokens);

$tokens = self::$phpcsFile->getTokens();
if (\method_exists($this, 'assertIsArray')) {
// PHPUnit 7+.
$this->assertIsArray($tokens);
} else {
// PHPUnit 4/5/6.
$this->assertInternalType('array', $tokens);
}
$this->assertIsArray($tokens);
}

/**
Expand Down Expand Up @@ -161,14 +155,8 @@ public function testGetTargetTokenCommentNotFound()
$exception = 'PHPUnit_Framework_AssertionFailedError';
}

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

$this->getTargetToken('/* testCommentDoesNotExist */', [\T_VARIABLE], '$a');
}
Expand All @@ -187,14 +175,8 @@ public function testGetTargetTokenNotFound()
$exception = 'PHPUnit_Framework_AssertionFailedError';
}

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

$this->getTargetToken('/* testNotFindingTarget */', [\T_VARIABLE], '$a');
}
Expand All @@ -209,14 +191,8 @@ public function testGetTargetTokenNotFoundException()
$msg = 'Failed to find test target token for comment string: ';
$exception = '\RuntimeException';

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

$this->getTargetToken('/* testNotFindingTarget */', [\T_VARIABLE], '$a', false);
}
Expand Down
13 changes: 3 additions & 10 deletions Tests/Utils/MessageHelper/HasNewLineSupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use PHP_CodeSniffer\Reporter;
use PHP_CodeSniffer\Reports\Full;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;
use PHPCSUtils\Utils\MessageHelper;

/**
Expand All @@ -27,7 +27,7 @@
*
* @since 1.0.0
*/
class HasNewLineSupportTest extends UtilityMethodTestCase
class HasNewLineSupportTest extends PolyfilledTestCase
{

/**
Expand All @@ -54,14 +54,7 @@ class HasNewLineSupportTest extends UtilityMethodTestCase
public function testHasNewLineSupport()
{
$result = MessageHelper::hasNewLineSupport();

if (\method_exists($this, 'assertIsBool') === true) {
// PHPUnit >= 7.5.
$this->assertIsBool($result);
} else {
// PHPUnit < 7.5.
$this->assertInternalType('bool', $result);
}
$this->assertIsBool($result);

if ($result === false) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace PHPCSUtils\Tests\Utils\PassedParameters;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;
use PHPCSUtils\Utils\PassedParameters;

/**
Expand All @@ -25,7 +25,7 @@
*
* @since 1.0.0
*/
class GetParametersSkipShortArrayCheckTest extends UtilityMethodTestCase
class GetParametersSkipShortArrayCheckTest extends PolyfilledTestCase
{

/**
Expand Down Expand Up @@ -54,13 +54,7 @@ public function testHasParametersDontSkipShortArrayCheck($testMarker, $targetTyp
$hasParams = PassedParameters::hasParameters(self::$phpcsFile, $target);

if ($expectException === false) {
if (\method_exists($this, 'assertIsBool') === true) {
// PHPUnit 7.5+.
$this->assertIsBool($hasParams);
} else {
// PHPUnit < 7.5.
$this->assertInternalType('bool', $hasParams);
}
$this->assertIsBool($hasParams);
}
}

Expand All @@ -82,13 +76,7 @@ public function testGetParametersSkipShortArrayCheck($testMarker, $targetType, $
$stackPtr = $this->getTargetToken($testMarker, [$targetType]);
$result = PassedParameters::getParameters(self::$phpcsFile, $stackPtr, 0, true);

if (\method_exists($this, 'assertIsArray') === true) {
// PHPUnit 7.5+.
$this->assertIsArray($result);
} else {
// PHPUnit < 7.5.
$this->assertInternalType('array', $result);
}
$this->assertIsArray($result);

// Start/end token position values in the expected array are set as offsets
// in relation to the target token.
Expand Down
Loading