Skip to content
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
7 changes: 4 additions & 3 deletions tests/ConfigDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ private function preventAutoDiscoveryScreenWidth()
private function getStaticConfigProperty($name)
{
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
$property->setAccessible(true);
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);

return $property->getValue();

}//end getStaticConfigProperty()
Expand All @@ -202,9 +203,9 @@ private function getStaticConfigProperty($name)
private function setStaticConfigProperty($name, $value)
{
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
$property->setAccessible(true);
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
$property->setValue(null, $value);
$property->setAccessible(false);
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);

}//end setStaticConfigProperty()

Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Config/AbstractRealConfigTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public static function resetConfigToDefaults()
protected static function setStaticConfigProperty($name, $value)
{
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
$property->setAccessible(true);
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
$property->setValue(null, $value);
$property->setAccessible(false);
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);

}//end setStaticConfigProperty()

Expand Down
2 changes: 1 addition & 1 deletion tests/Core/Filters/GitModifiedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function testExecAlwaysReturnsArray($cmd, $expected)
$filter = new GitModified($fakeDI, '/', self::$config, self::$ruleset);

$reflMethod = new ReflectionMethod($filter, 'exec');
$reflMethod->setAccessible(true);
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
$result = $reflMethod->invoke($filter, $cmd);

$this->assertSame($expected, $result);
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/Filters/GitStagedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function testExecAlwaysReturnsArray($cmd, $expected)
$filter = new GitStaged($fakeDI, '/', self::$config, self::$ruleset);

$reflMethod = new ReflectionMethod($filter, 'exec');
$reflMethod->setAccessible(true);
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
$result = $reflMethod->invoke($filter, $cmd);

$this->assertSame($expected, $result);
Expand Down
8 changes: 4 additions & 4 deletions tests/Core/Ruleset/DisplayCachedMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ private function getPlainRuleset()
private function mockCachedMessages(Ruleset $ruleset, $messages)
{
$reflProperty = new ReflectionProperty($ruleset, 'msgCache');
$reflProperty->setAccessible(true);
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true);

$msgCache = $reflProperty->getValue($ruleset);
foreach ($messages as $msg => $type) {
$msgCache->add($msg, $type);
}

$reflProperty->setAccessible(false);
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false);

}//end mockCachedMessages()

Expand All @@ -302,9 +302,9 @@ private function mockCachedMessages(Ruleset $ruleset, $messages)
private function invokeDisplayCachedMessages(Ruleset $ruleset)
{
$reflMethod = new ReflectionMethod($ruleset, 'displayCachedMessages');
$reflMethod->setAccessible(true);
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
$reflMethod->invoke($ruleset);
$reflMethod->setAccessible(false);
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false);

}//end invokeDisplayCachedMessages()

Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Ruleset/PopulateTokenListenersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public static function dataSniffListensToTokenss()
public function testRegistersWhenADeprecatedSniffIsLoaded()
{
$property = new ReflectionProperty(self::$ruleset, 'deprecatedSniffs');
$property->setAccessible(true);
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
$actualValue = $property->getValue(self::$ruleset);
$property->setAccessible(false);
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);

// Only verify there is one deprecated sniff registered.
// There are other tests which test the deprecated sniff handling in more detail.
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Tokenizers/AbstractTokenizerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ protected function getTargetToken($commentString, $tokenType, $tokenContent=null
public static function clearResolvedTokensCache()
{
$property = new ReflectionProperty('PHP_CodeSniffer\Tokenizers\PHP', 'resolveTokenCache');
$property->setAccessible(true);
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
$property->setValue(null, []);
$property->setAccessible(false);
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);

}//end clearResolvedTokensCache()

Expand Down
139 changes: 81 additions & 58 deletions tests/Core/Util/Help/HelpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ final class HelpTest extends TestCase
*/
public function testQaArgumentNamesAreWithinAcceptableBounds()
{
$help = new Help(new ConfigDouble(), []);

$reflMethod = new ReflectionMethod($help, 'getAllOptions');
$reflMethod->setAccessible(true);
$allOptions = $reflMethod->invoke($help);
$reflMethod->setAccessible(false);
$help = new Help(new ConfigDouble(), []);
$allOptions = $this->invokeReflectionMethod($help, 'getAllOptions');

$this->assertGreaterThan(0, count($allOptions), 'No categories found');

Expand Down Expand Up @@ -81,12 +77,8 @@ public function testQaArgumentNamesAreWithinAcceptableBounds()
*/
public function testQaValidCategoryOptionDefinitions()
{
$help = new Help(new ConfigDouble(), []);

$reflMethod = new ReflectionMethod($help, 'getAllOptions');
$reflMethod->setAccessible(true);
$allOptions = $reflMethod->invoke($help);
$reflMethod->setAccessible(false);
$help = new Help(new ConfigDouble(), []);
$allOptions = $this->invokeReflectionMethod($help, 'getAllOptions');

$this->assertGreaterThan(0, count($allOptions), 'No categories found');

Expand Down Expand Up @@ -185,10 +177,7 @@ public function testOptionFiltering($longOptions, $shortOptions, $expected)
{
$help = new Help(new ConfigDouble(), $longOptions, $shortOptions);

$reflProperty = new ReflectionProperty($help, 'activeOptions');
$reflProperty->setAccessible(true);
$activeOptions = $reflProperty->getValue($help);
$reflProperty->setAccessible(false);
$activeOptions = $this->getReflectionProperty($help, 'activeOptions');

// Simplify the value to make it comparible.
foreach ($activeOptions as $category => $options) {
Expand Down Expand Up @@ -324,10 +313,7 @@ public function testOptionFilteringSpacerHandling($longOptions, $shortOptions)
{
$help = new Help(new ConfigDouble(), $longOptions, $shortOptions);

$reflProperty = new ReflectionProperty($help, 'activeOptions');
$reflProperty->setAccessible(true);
$activeOptions = $reflProperty->getValue($help);
$reflProperty->setAccessible(false);
$activeOptions = $this->getReflectionProperty($help, 'activeOptions');

$this->assertNotEmpty($activeOptions, 'Active options is empty, test is invalid');

Expand Down Expand Up @@ -493,10 +479,7 @@ public function testReportWidthCalculations($reportWidth, $longOptions, $expecte
$config = new ConfigDouble(["--report-width=$reportWidth", '--no-colors']);
$help = new Help($config, $longOptions);

$reflMethod = new ReflectionMethod($help, 'printCategories');
$reflMethod->setAccessible(true);
$reflMethod->invoke($help);
$reflMethod->setAccessible(false);
$this->invokeReflectionMethod($help, 'printCategories');

$this->expectOutputString($expectedOutput);

Expand Down Expand Up @@ -572,12 +555,8 @@ public static function dataReportWidthCalculations()
*/
public function testColorizeVariableInput($input, $expected)
{
$help = new Help(new ConfigDouble(), []);

$reflMethod = new ReflectionMethod($help, 'colorizeVariableInput');
$reflMethod->setAccessible(true);
$result = $reflMethod->invoke($help, $input);
$reflMethod->setAccessible(false);
$help = new Help(new ConfigDouble(), []);
$result = $this->invokeReflectionMethod($help, 'colorizeVariableInput', $input);

$this->assertSame($expected, $result);

Expand Down Expand Up @@ -640,20 +619,9 @@ public function testPrintCategoryOptionsNoColor($input, $expectedRegex)
$config = new ConfigDouble(['--no-colors']);
$help = new Help($config, []);

$reflProperty = new ReflectionProperty($help, 'activeOptions');
$reflProperty->setAccessible(true);
$reflProperty->setValue($help, ['cat' => $input]);
$reflProperty->setAccessible(false);

$reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength');
$reflMethod->setAccessible(true);
$reflMethod->invoke($help);
$reflMethod->setAccessible(false);

$reflMethod = new ReflectionMethod($help, 'printCategoryOptions');
$reflMethod->setAccessible(true);
$reflMethod->invoke($help, $input);
$reflMethod->setAccessible(false);
$this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]);
$this->invokeReflectionMethod($help, 'setMaxOptionNameLength');
$this->invokeReflectionMethod($help, 'printCategoryOptions', $input);

$this->expectOutputRegex($expectedRegex['no-color']);

Expand All @@ -675,20 +643,9 @@ public function testPrintCategoryOptionsColor($input, $expectedRegex)
$config = new ConfigDouble(['--colors']);
$help = new Help($config, []);

$reflProperty = new ReflectionProperty($help, 'activeOptions');
$reflProperty->setAccessible(true);
$reflProperty->setValue($help, ['cat' => $input]);
$reflProperty->setAccessible(false);

$reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength');
$reflMethod->setAccessible(true);
$reflMethod->invoke($help);
$reflMethod->setAccessible(false);

$reflMethod = new ReflectionMethod($help, 'printCategoryOptions');
$reflMethod->setAccessible(true);
$reflMethod->invoke($help, $input);
$reflMethod->setAccessible(false);
$this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]);
$this->invokeReflectionMethod($help, 'setMaxOptionNameLength');
$this->invokeReflectionMethod($help, 'printCategoryOptions', $input);

$this->expectOutputRegex($expectedRegex['color']);

Expand Down Expand Up @@ -766,4 +723,70 @@ public static function dataPrintCategoryOptions()
}//end dataPrintCategoryOptions()


/**
* Test Helper: invoke a reflected method which is not publicly accessible.
*
* @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
* @param string $methodName The name of the method to invoke.
* @param mixed $params Optional. Parameters to pass to the method invocation.
*
* @return mixed
*/
private function invokeReflectionMethod(Help $help, $methodName, $params=null)
{
$reflMethod = new ReflectionMethod($help, $methodName);
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);

if ($params === null) {
$returnValue = $reflMethod->invoke($help);
} else {
$returnValue = $reflMethod->invoke($help, $params);
}

(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false);

return $returnValue;

}//end invokeReflectionMethod()


/**
* Test Helper: retrieve the value of property which is not publicly accessible.
*
* @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
* @param string $properyName The name of the property to retrieve.
*
* @return mixed
*/
private function getReflectionProperty(Help $help, $properyName)
{
$reflProperty = new ReflectionProperty($help, $properyName);
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true);
$returnValue = $reflProperty->getValue($help);
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false);

return $returnValue;

}//end getReflectionProperty()


/**
* Test Helper: set the value of property which is not publicly accessible.
*
* @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
* @param string $properyName The name of the property to set.
* @param mixed $value The value to set.
*
* @return void
*/
private function setReflectionProperty(Help $help, $properyName, $value)
{
$reflProperty = new ReflectionProperty($help, $properyName);
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true);
$reflProperty->setValue($help, $value);
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false);

}//end setReflectionProperty()


}//end class
Loading