Skip to content

Commit

Permalink
UtilityMethodTestCase: split a test class in two
Browse files Browse the repository at this point in the history
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
jrfnl committed Oct 17, 2022
1 parent 79164c6 commit 10dbe9f
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 133 deletions.
182 changes: 182 additions & 0 deletions Tests/TestUtils/UtilityMethodTestCase/GetTargetTokenTest.php
@@ -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);
}
}
145 changes: 12 additions & 133 deletions Tests/TestUtils/UtilityMethodTestCase/UtilityMethodTestCaseTest.php
Expand Up @@ -15,7 +15,7 @@
/**
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
*
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase
* @coversDefaultClass \PHPCSUtils\TestUtils\UtilityMethodTestCase
*
* @group testutils
*
Expand All @@ -40,6 +40,8 @@ public static function setUpTestFile()
/**
* Test that the setUpTestFile() method works correctly.
*
* @covers ::setUpTestFile
*
* @return void
*/
public function testSetUp()
Expand All @@ -53,150 +55,27 @@ public function testSetUp()
}

/**
* 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.
* Test that the skipJSCSSTestsOnPHPCS4() method does not skip non-JS/CSS files.
*
* @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.
* @depends testSetUp
*
* @see testGetTargetToken() For the array format.
* @covers ::skipJSCSSTestsOnPHPCS4
*
* @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.
* @doesNotPerformAssertions
*
* @return void
*/
public function testGetTargetTokenCommentNotFound()
public function testDontSkipPHPFiles()
{
$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');
parent::skipJSCSSTestsOnPHPCS4();
}

/**
* Test the behaviour of the getTargetToken() method when the target is not found.
* Test that the class is correct reset.
*
* @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.
* @depends testSetUp
*
* @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);
}

/**
* Test that the class is correct reset.
* @covers ::resetTestFile
*
* @return void
*/
Expand Down

0 comments on commit 10dbe9f

Please sign in to comment.