UtilityMethodTestCase extends TestCase
Base class for use when testing utility methods for PHP_CodeSniffer.
This class is compatible with PHP_CodeSniffer 2.x as well as 3.x.
This class is compatible with PHPUnit 4.5 - 8.x providing the PHPCSUtils autoload file is included in the test bootstrap.
To allow for testing of tab vs space content, the tabWidth is set to 4 by default.
Typical usage:
Test case file path/to/ClassUnderTestUnitTest.inc:
<?php
/* testTestCaseDescription * /
const BAR = false;
Test file path/to/ClassUnderTestUnitTest.php:
<?php
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use YourStandard\ClassUnderTest;
class ClassUnderTestUnitTest extends UtilityMethodTestCase {
/**
* Testing utility method MyMethod.
*
* @dataProvider dataMyMethod
*
* @covers \YourStandard\ClassUnderTest::MyMethod
*
* @param string $commentString The comment which prefaces the target token in the test file.
* @param string $expected The expected return value.
*
* @return void
* /
public function testMyMethod($commentString, $expected)
{
$stackPtr = $this->getTargetToken($commentString, [\T_TOKEN_CONSTANT, \T_ANOTHER_TOKEN]);
$class = new ClassUnderTest();
$result = $class->MyMethod(self::$phpcsFile, $stackPtr);
// Or for static utility methods:
$result = ClassUnderTest::MyMethod(self::$phpcsFile, $stackPtr);
$this->assertSame($expected, $result);
}
/**
* Data Provider.
*
* @see testMyMethod() For the array format.
*
* @return array
* /
public function dataMyMethod()
{
return array(
array('/* testTestCaseDescription * /', false),
);
}
}
Note:
- Remove the space between the comment closers
* /for a working example. - Each test case separator comment MUST start with
/* test. This is to allow thegetTargetToken()method to distinquish between the test separation comments and comments which may be part of the test case. - The test case file and unit test file should be placed in the same directory.
- For working examples using this abstract class, have a look at the unit tests for the PHPCSUtils utility functions themselves.
Table of Contents
| $fileExtension | The file extension of the test case file (without leading dot). | string |
|---|---|---|
| $caseFile | Full path to the test case file associated with the concrete test class. | string |
| $tabWidth | The tab width setting to use when tokenizing the file. | int |
| $phpcsFile | The {@see \PHP_CodeSniffer\Files\File} object containing the parsed contents of the test case file. | File |
| $selectedSniff | Set the name of a sniff to pass to PHPCS to limit the run (and force it to record errors). | array |
| setUpTestFile() | Initialize PHPCS & tokenize the test case file. | void |
| resetTestFile() | Clean up after finished test. | void |
| getTargetToken() | Get the token pointer for a target token based on a specific comment found on the line before. | int |
| expectPhpcsException() | Helper method to tell PHPUnit to expect a PHPCS Exception in a PHPUnit cross-version compatible manner. | void |
Properties
$fileExtension
The file extension of the test case file (without leading dot).
protected
static string
$fileExtension
= 'inc'
$caseFile
Full path to the test case file associated with the concrete test class.
protected
static string
$caseFile
= ''
$tabWidth
The tab width setting to use when tokenizing the file.
protected
static int
$tabWidth
= 4
$phpcsFile
The {@see \PHP_CodeSniffer\Files\File} object containing the parsed contents of the test case file.
protected
static File
$phpcsFile
$selectedSniff
Set the name of a sniff to pass to PHPCS to limit the run (and force it to record errors).
protected
static array
$selectedSniff
= ['Dummy.Dummy.Dummy']
Methods
setUpTestFile()
Initialize PHPCS & tokenize the test case file.
public
static setUpTestFile(
)
: void
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.
Tags
resetTestFile()
Clean up after finished test.
public
static resetTestFile(
)
: void
Tags
getTargetToken()
Get the token pointer for a target token based on a specific comment found on the line before.
public
getTargetToken(
$commentString :
string
, $tokenType :
int|string|array
[, $tokenContent :
string
= null ]
)
: int
Note: the test delimiter comment MUST start with "/ test" to allow this function to distinguish between comments used in* a test and test delimiters.
Parameters
- $commentString : string
The delimiter comment to look for.
- $tokenType : int|string|array
The type of token(s) to look for.
- $tokenContent : string = null
Optional. The token content for the target token.
Tags
Return values
intexpectPhpcsException()
Helper method to tell PHPUnit to expect a PHPCS Exception in a PHPUnit cross-version compatible manner.
public
expectPhpcsException(
$msg :
string
[, $type :
string
= 'runtime' ]
)
: void
Parameters
- $msg : string
The expected exception message.
- $type : string = 'runtime'
The exception type to expect. Either 'runtime' or 'tokenizer'. Defaults to 'runtime'.