Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UtilityMethodTestCase: split a test class in two
The tests for the `UtilityMethodTestCase::getTargetToken()` had a hidden dependency on the `testSetUp()` method. By splitting the test class, that dependency is removed. The remaining test methods in the `UtilityMethodTestCaseTest` class all depend on the `testSetUp()` method. This has now been so annotated. Includes adding stricter `@covers` tags. Includes adding a new test for the `skipJSCSSTestsOnPHPCS4()` method, as otherwise the "not a JS/CSS file" situation would no longer be marked as covered.
- Loading branch information
Showing
2 changed files
with
194 additions
and
133 deletions.
There are no files selected for viewing
182 changes: 182 additions & 0 deletions
182
Tests/TestUtils/UtilityMethodTestCase/GetTargetTokenTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?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\TestUtils\UtilityMethodTestCase; | ||
|
||
use PHPCSUtils\Tests\PolyfilledTestCase; | ||
|
||
/** | ||
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase::getTargetToken() method. | ||
* | ||
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::getTargetToken | ||
* | ||
* @group testutils | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
class GetTargetTokenTest extends PolyfilledTestCase | ||
{ | ||
|
||
/** | ||
* Initialize PHPCS & tokenize the test case file. | ||
* | ||
* @beforeClass | ||
* | ||
* @return void | ||
*/ | ||
public static function setUpTestFile() | ||
{ | ||
self::$caseFile = __DIR__ . '/UtilityMethodTestCaseTest.inc'; | ||
parent::setUpTestFile(); | ||
} | ||
|
||
/** | ||
* Test the getTargetToken() method. | ||
* | ||
* @dataProvider dataGetTargetToken | ||
* | ||
* @param int|false $expected Expected function output. | ||
* @param string $commentString The delimiter comment to look for. | ||
* @param int|string|array $tokenType The type of token(s) to look for. | ||
* @param string $tokenContent Optional. The token content for the target token. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetTargetToken($expected, $commentString, $tokenType, $tokenContent = null) | ||
{ | ||
if (isset($tokenContent)) { | ||
$result = $this->getTargetToken($commentString, $tokenType, $tokenContent); | ||
} else { | ||
$result = $this->getTargetToken($commentString, $tokenType); | ||
} | ||
|
||
$this->assertSame($expected, $result); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testGetTargetToken() For the array format. | ||
* | ||
* @return array | ||
*/ | ||
public function dataGetTargetToken() | ||
{ | ||
return [ | ||
'single-token-type' => [ | ||
'expected' => 6, | ||
'commentString' => '/* testFindingTarget */', | ||
'tokenType' => \T_VARIABLE, | ||
], | ||
'multi-token-type-1' => [ | ||
'expected' => 6, | ||
'commentString' => '/* testFindingTarget */', | ||
'tokenType' => [\T_VARIABLE, \T_FALSE], | ||
], | ||
'multi-token-type-2' => [ | ||
'expected' => 11, | ||
'commentString' => '/* testFindingTarget */', | ||
'tokenType' => [\T_FALSE, \T_LNUMBER], | ||
], | ||
'content-method' => [ | ||
'expected' => 23, | ||
'commentString' => '/* testFindingTargetWithContent */', | ||
'tokenType' => \T_STRING, | ||
'tokenContent' => 'method', | ||
], | ||
'content-otherMethod' => [ | ||
'expected' => 33, | ||
'commentString' => '/* testFindingTargetWithContent */', | ||
'tokenType' => \T_STRING, | ||
'tokenContent' => 'otherMethod', | ||
], | ||
'content-$a' => [ | ||
'expected' => 21, | ||
'commentString' => '/* testFindingTargetWithContent */', | ||
'tokenType' => \T_VARIABLE, | ||
'tokenContent' => '$a', | ||
], | ||
'content-$b' => [ | ||
'expected' => 31, | ||
'commentString' => '/* testFindingTargetWithContent */', | ||
'tokenType' => \T_VARIABLE, | ||
'tokenContent' => '$b', | ||
], | ||
'content-foo' => [ | ||
'expected' => 26, | ||
'commentString' => '/* testFindingTargetWithContent */', | ||
'tokenType' => [\T_CONSTANT_ENCAPSED_STRING, \T_DOUBLE_QUOTED_STRING], | ||
'tokenContent' => "'foo'", | ||
], | ||
'content-bar' => [ | ||
'expected' => 36, | ||
'commentString' => '/* testFindingTargetWithContent */', | ||
'tokenType' => [\T_CONSTANT_ENCAPSED_STRING, \T_DOUBLE_QUOTED_STRING], | ||
'tokenContent' => "'bar'", | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Test the behaviour of the getTargetToken() method when the test marker comment is not found. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetTargetTokenCommentNotFound() | ||
{ | ||
$msg = 'Failed to find the test marker: '; | ||
$exception = 'PHPUnit\Framework\AssertionFailedError'; | ||
if (\class_exists('PHPUnit_Framework_AssertionFailedError')) { | ||
// PHPUnit < 6. | ||
$exception = 'PHPUnit_Framework_AssertionFailedError'; | ||
} | ||
|
||
$this->expectException($exception); | ||
$this->expectExceptionMessage($msg); | ||
|
||
$this->getTargetToken('/* testCommentDoesNotExist */', [\T_VARIABLE], '$a'); | ||
} | ||
|
||
/** | ||
* Test the behaviour of the getTargetToken() method when the target is not found. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetTargetTokenNotFound() | ||
{ | ||
$msg = 'Failed to find test target token for comment string: '; | ||
$exception = 'PHPUnit\Framework\AssertionFailedError'; | ||
if (\class_exists('PHPUnit_Framework_AssertionFailedError')) { | ||
// PHPUnit < 6. | ||
$exception = 'PHPUnit_Framework_AssertionFailedError'; | ||
} | ||
|
||
$this->expectException($exception); | ||
$this->expectExceptionMessage($msg); | ||
|
||
$this->getTargetToken('/* testNotFindingTarget */', [\T_VARIABLE], '$a'); | ||
} | ||
|
||
/** | ||
* Test the behaviour of the getTargetToken() method when the target is not found. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetTargetTokenNotFoundException() | ||
{ | ||
$msg = 'Failed to find test target token for comment string: '; | ||
$exception = '\RuntimeException'; | ||
|
||
$this->expectException($exception); | ||
$this->expectExceptionMessage($msg); | ||
|
||
$this->getTargetToken('/* testNotFindingTarget */', [\T_VARIABLE], '$a', false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters