Documentation

GetConditionTest extends AbstractMethodUnitTest
in package

FinalYes

Tests for the \PHP_CodeSniffer\Files\File:getCondition and \PHP_CodeSniffer\Files\File:hasCondition methods.

Tags
covers
covers

Table of Contents

Properties

$conditionDefaults  : array<string, bool>
Base array with all the scope opening tokens.
$conditionMarkers  : array<string|int, string>
List of all the condition markers in the test case file.
$fileExtension  : string
The file extension of the test case file (without leading dot).
$markerTokens  : array<string, int>
Cache for the marker token stack pointers.
$phpcsFile  : File
The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.
$tabWidth  : int
The tab width setting to use when tokenizing the file.
$testTargets  : array<int|string, string>
List of all the test markers with their target token in the test case file.
$testTokens  : array<string, int>
Cache for the test token stack pointers.

Methods

dataGetCondition()  : array<string, array<string, string|array<string, string>>>
Data provider.
dataGetConditionReversed()  : array<string, array<string, string|array<string, string>>>
Data provider.
dataHasCondition()  : array<string, array<string, string|array<string, bool>>>
Data Provider.
expectRunTimeException()  : void
Helper method to tell PHPUnit to expect a PHPCS RuntimeException in a PHPUnit cross-version compatible manner.
getTargetToken()  : int
Get the token pointer for a target token based on a specific comment found on the line before.
getTargetTokenFromFile()  : int
Get the token pointer for a target token based on a specific comment found on the line before.
initializeFile()  : void
Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
testGetCondition()  : void
Test retrieving a specific condition from a tokens "conditions" array.
testGetConditionReversed()  : void
Test retrieving a specific condition from a tokens "conditions" array.
testHasCondition()  : void
Test whether a token has a condition of a certain type.
testHasConditionMultipleTypes()  : void
Test whether a token has a condition of a certain type, with multiple allowed possibilities.
testNonConditionalToken()  : void
Test passing a non conditional token.
testNonExistentToken()  : void
Test passing a non-existent token pointer.
setUpCaches()  : void
Set up the token position caches for the tests.

Properties

$conditionDefaults

Base array with all the scope opening tokens.

protected array<string, bool> $conditionDefaults = ['T_CLASS' => false, 'T_ANON_CLASS' => false, 'T_INTERFACE' => false, 'T_TRAIT' => false, 'T_NAMESPACE' => false, 'T_FUNCTION' => false, 'T_CLOSURE' => false, 'T_IF' => false, 'T_SWITCH' => false, 'T_CASE' => false, 'T_DECLARE' => false, 'T_DEFAULT' => false, 'T_WHILE' => false, 'T_ELSE' => false, 'T_ELSEIF' => false, 'T_FOR' => false, 'T_FOREACH' => false, 'T_DO' => false, 'T_TRY' => false, 'T_CATCH' => false, 'T_FINALLY' => false, 'T_PROPERTY' => false, 'T_OBJECT' => false, 'T_USE' => false]

This array is merged with expected result arrays for various unit tests to make sure all possible conditions are tested.

This array should be kept in sync with the Tokens::$scopeOpeners array. This array isn't auto-generated based on the array in Tokens as for these tests we want to have access to the token constant names, not just their values.

$conditionMarkers

List of all the condition markers in the test case file.

protected array<string|int, string> $conditionMarkers = ['/* condition 0: namespace */', '/* condition 1: if */', '/* condition 2: function */', '/* condition 3-1: if */', '/* condition 3-2: else */', '/* condition 4: if */', '/* condition 5: nested class */', '/* condition 6: class method */', '/* condition 7: switch */', '/* condition 8a: case */', '/* condition 9: while */', '/* condition 10-1: if */', '/* condition 11-1: nested anonymous class */', '/* condition 12: nested anonymous class method */', '/* condition 13: closure */', '/* condition 10-2: elseif */', '/* condition 10-3: foreach */', '/* condition 11-2: try */', '/* condition 11-3: catch */', '/* condition 11-4: finally */', '/* condition 8b: default */']

$fileExtension

The file extension of the test case file (without leading dot).

protected static string $fileExtension = 'inc'

This allows child classes to overrule the default inc with, for instance, js or css when applicable.

$markerTokens

Cache for the marker token stack pointers.

protected static array<string, int> $markerTokens = []

$phpcsFile

The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.

protected static File $phpcsFile

$tabWidth

The tab width setting to use when tokenizing the file.

protected static int $tabWidth = 4

This allows for test case files to use a different tab width than the default.

$testTargets

List of all the test markers with their target token in the test case file.

protected static array<int|string, string> $testTargets = [T_VARIABLE => '/* testSeriouslyNestedMethod */', T_RETURN => '/* testDeepestNested */', T_ECHO => '/* testInException */', T_CONSTANT_ENCAPSED_STRING => '/* testInDefault */']
  • The startPoint token is left out as it is tested separately.
  • The key is the type of token to look for after the test marker.

$testTokens

Cache for the test token stack pointers.

protected static array<string, int> $testTokens = []

Methods

dataGetCondition()

Data provider.

public static dataGetCondition() : array<string, array<string, string|array<string, string>>>

Only the conditions which are expected to be found need to be listed here. All other potential conditions will automatically also be tested and will expect false as a result.

Tags
see
testGetCondition()

For the array format.

Return values
array<string, array<string, string|array<string, string>>>

dataGetConditionReversed()

Data provider.

public static dataGetConditionReversed() : array<string, array<string, string|array<string, string>>>

Only the conditions which are expected to be found need to be listed here. All other potential conditions will automatically also be tested and will expect false as a result.

Tags
see
testGetConditionReversed()

For the array format.

Return values
array<string, array<string, string|array<string, string>>>

dataHasCondition()

Data Provider.

public static dataHasCondition() : array<string, array<string, string|array<string, bool>>>

Only list the "true" conditions in the $results array. All other potential conditions will automatically also be tested and will expect "false" as a result.

Tags
see
testHasCondition()

For the array format.

Return values
array<string, array<string, string|array<string, bool>>>

expectRunTimeException()

Helper method to tell PHPUnit to expect a PHPCS RuntimeException in a PHPUnit cross-version compatible manner.

public expectRunTimeException(string $message) : void
Parameters
$message : string

The expected exception message.

getTargetToken()

Get the token pointer for a target token based on a specific comment found on the line before.

public getTargetToken(string $commentString, int|string|array<string|int, mixed> $tokenType[, string $tokenContent = 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<string|int, mixed>

The type of token(s) to look for.

$tokenContent : string = null

Optional. The token content for the target token.

Return values
int

getTargetTokenFromFile()

Get the token pointer for a target token based on a specific comment found on the line before.

public static getTargetTokenFromFile(File $phpcsFile, string $commentString, int|string|array<string|int, mixed> $tokenType[, string $tokenContent = 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
$phpcsFile : File

The file to find the token in.

$commentString : string

The delimiter comment to look for.

$tokenType : int|string|array<string|int, mixed>

The type of token(s) to look for.

$tokenContent : string = null

Optional. The token content for the target token.

Tags
throws
Exception

When the test delimiter comment is not found.

throws
Exception

When the test target token is not found.

Return values
int

initializeFile()

Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.

public static initializeFile() : 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
beforeClass

testGetCondition()

Test retrieving a specific condition from a tokens "conditions" array.

public testGetCondition(string $testMarker, array<string, string> $expectedResults) : void
Parameters
$testMarker : string

The comment which prefaces the target token in the test file.

$expectedResults : array<string, string>

Array with the condition token type to search for as key and the marker for the expected stack pointer result as a value.

Tags
dataProvider

dataGetCondition

testGetConditionReversed()

Test retrieving a specific condition from a tokens "conditions" array.

public testGetConditionReversed(string $testMarker, array<string, string> $expectedResults) : void
Parameters
$testMarker : string

The comment which prefaces the target token in the test file.

$expectedResults : array<string, string>

Array with the condition token type to search for as key and the marker for the expected stack pointer result as a value.

Tags
dataProvider

dataGetConditionReversed

testHasCondition()

Test whether a token has a condition of a certain type.

public testHasCondition(string $testMarker, array<string, bool> $expectedResults) : void
Parameters
$testMarker : string

The comment which prefaces the target token in the test file.

$expectedResults : array<string, bool>

Array with the condition token type to search for as key and the expected result as a value.

Tags
dataProvider

dataHasCondition

testHasConditionMultipleTypes()

Test whether a token has a condition of a certain type, with multiple allowed possibilities.

public testHasConditionMultipleTypes() : void

testNonConditionalToken()

Test passing a non conditional token.

public testNonConditionalToken() : void

testNonExistentToken()

Test passing a non-existent token pointer.

public testNonExistentToken() : void

setUpCaches()

Set up the token position caches for the tests.

protected setUpCaches() : void

Retrieves the test tokens and marker token stack pointer positions only once and caches them as they won't change between the tests anyway.

Tags
before

        
On this page

Search results