PATH:
opt
/
cpanel
/
ea-wappspector
/
vendor
/
squizlabs
/
php_codesniffer
/
tests
/
Core
/
Ruleset
<?php /** * Test error handling for the Ruleset. * * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> * @copyright 2024 PHPCSStandards and contributors * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ namespace PHP_CodeSniffer\Tests\Core\Ruleset; use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Tests\ConfigDouble; use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase; use PHP_CodeSniffer\Util\MessageCollector; use ReflectionMethod; use ReflectionProperty; /** * Test error handling for the Ruleset. * * Note: this is purely a unit test of the `displayCachedMessages()` method. * The errors themselves are mocked. * * @covers \PHP_CodeSniffer\Ruleset::displayCachedMessages */ final class DisplayCachedMessagesTest extends AbstractRulesetTestCase { /** * Test that no exception nor output is generated when there are no cached messsages. * * @return void */ public function testDisplayCachedMessagesStaysSilentWithoutErrors() { $ruleset = $this->getPlainRuleset(); $this->expectOutputString(''); $this->invokeDisplayCachedMessages($ruleset); }//end testDisplayCachedMessagesStaysSilentWithoutErrors() /** * Verify that blocking errors encountered while loading the ruleset(s) result in an exception being thrown. * * @param array<string, int> $messages The messages encountered. * @param string $expected The expected function output to screen (via an internally handled exception). * * @dataProvider dataBlockingErrorsAreDisplayedViaAnException * * @return void */ public function testBlockingErrorsAreDisplayedViaAnException($messages, $expected) { $ruleset = $this->getPlainRuleset(); $this->mockCachedMessages($ruleset, $messages); $this->expectRuntimeExceptionMessage($expected); $this->invokeDisplayCachedMessages($ruleset); }//end testBlockingErrorsAreDisplayedViaAnException() /** * Data provider. * * @see testBlockingErrorsAreDisplayedViaAnException() * * @return array<string, array<string, string|array<string, int>>> */ public static function dataBlockingErrorsAreDisplayedViaAnException() { return [ 'One error' => [ 'messages' => ['This is a serious blocking issue' => MessageCollector::ERROR], 'expected' => 'ERROR: This is a serious blocking issue'.PHP_EOL.PHP_EOL, ], 'Multiple blocking errors' => [ 'messages' => [ 'This is a serious blocking issue' => MessageCollector::ERROR, 'And here is another one' => MessageCollector::ERROR, 'OMG, why do you think that would work ?' => MessageCollector::ERROR, ], // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- Test readability is more important. 'expected' => 'ERROR: This is a serious blocking issue'.PHP_EOL . 'ERROR: And here is another one'.PHP_EOL . 'ERROR: OMG, why do you think that would work ?'.PHP_EOL.PHP_EOL, // phpcs:enable ], 'Mix of blocking and non-blocking errors' => [ 'messages' => [ 'This is a serious blocking issue' => MessageCollector::ERROR, 'Something something deprecated and will be removed in v x.x.x' => MessageCollector::DEPRECATED, 'Careful, this may not be correct' => MessageCollector::NOTICE, ], // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- Test readability is more important. 'expected' => 'ERROR: This is a serious blocking issue'.PHP_EOL . 'NOTICE: Careful, this may not be correct'.PHP_EOL . 'DEPRECATED: Something something deprecated and will be removed in v x.x.x'.PHP_EOL.PHP_EOL, // phpcs:enable ], ]; }//end dataBlockingErrorsAreDisplayedViaAnException() /** * Test display of non-blocking messages encountered while loading the ruleset(s). * * @param array<string, int> $messages The messages encountered. * @param string $expected The expected function output to screen. * * @dataProvider dataNonBlockingErrorsGenerateOutput * * @return void */ public function testNonBlockingErrorsGenerateOutput($messages, $expected) { $ruleset = $this->getPlainRuleset(); $this->mockCachedMessages($ruleset, $messages); $this->expectOutputString($expected); $this->invokeDisplayCachedMessages($ruleset); }//end testNonBlockingErrorsGenerateOutput() /** * Data provider. * * @see testNonBlockingErrorsGenerateOutput() * * @return array<string, array<string, string|array<string, int>>> */ public static function dataNonBlockingErrorsGenerateOutput() { return [ 'One deprecation' => [ 'messages' => ['My deprecation message' => MessageCollector::DEPRECATED], 'expected' => 'DEPRECATED: My deprecation message'.PHP_EOL.PHP_EOL, ], 'One notice' => [ 'messages' => ['My notice message' => MessageCollector::NOTICE], 'expected' => 'NOTICE: My notice message'.PHP_EOL.PHP_EOL, ], 'One warning' => [ 'messages' => ['My warning message' => MessageCollector::WARNING], 'expected' => 'WARNING: My warning message'.PHP_EOL.PHP_EOL, ], 'Multiple non-blocking errors' => [ 'messages' => [ 'Something something deprecated and will be removed in v x.x.x' => MessageCollector::DEPRECATED, 'Something is not supported and support may be removed' => MessageCollector::WARNING, 'Some other deprecation notice' => MessageCollector::DEPRECATED, 'Careful, this may not be correct' => MessageCollector::NOTICE, ], // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- Test readability is more important. 'expected' => 'WARNING: Something is not supported and support may be removed'.PHP_EOL .'NOTICE: Careful, this may not be correct'.PHP_EOL .'DEPRECATED: Something something deprecated and will be removed in v x.x.x'.PHP_EOL .'DEPRECATED: Some other deprecation notice'.PHP_EOL.PHP_EOL, // phpcs:enable ], ]; }//end dataNonBlockingErrorsGenerateOutput() /** * Test that blocking errors will always show, independently of specific command-line options being used. * * @param array<string> $configArgs Arguments to pass to the Config. * * @dataProvider dataSelectiveDisplayOfMessages * * @return void */ public function testBlockingErrorsAlwaysShow($configArgs) { $config = new ConfigDouble($configArgs); $ruleset = new Ruleset($config); $message = 'Some serious error'; $errors = [$message => MessageCollector::ERROR]; $this->mockCachedMessages($ruleset, $errors); $this->expectRuntimeExceptionMessage('ERROR: '.$message.PHP_EOL); $this->invokeDisplayCachedMessages($ruleset); }//end testBlockingErrorsAlwaysShow() /** * Test that non-blocking messsages will not show when specific command-line options are being used. * * @param array<string> $configArgs Arguments to pass to the Config. * * @dataProvider dataSelectiveDisplayOfMessages * * @return void */ public function testNonBlockingErrorsDoNotShowUnderSpecificCircumstances($configArgs) { $config = new ConfigDouble($configArgs); $ruleset = new Ruleset($config); $this->mockCachedMessages($ruleset, ['Deprecation notice' => MessageCollector::DEPRECATED]); $this->expectOutputString(''); $this->invokeDisplayCachedMessages($ruleset); }//end testNonBlockingErrorsDoNotShowUnderSpecificCircumstances() /** * Data provider. * * @see testBlockingErrorsAlwaysShow() * @see testNonBlockingErrorsDoNotShow() * * @return array<string, array<string, string|array<string>>> */ public static function dataSelectiveDisplayOfMessages() { $data = [ 'Explain mode' => [ 'configArgs' => ['-e'], ], 'Quiet mode' => [ 'configArgs' => ['-q'], ], ]; // Setting the `--generator` arg is only supported when running `phpcs`. if (PHP_CODESNIFFER_CBF === false) { $data['Documentation is requested'] = [ 'configArgs' => ['--generator=text'], ]; } return $data; }//end dataSelectiveDisplayOfMessages() /** * Test Helper. * * @return \PHP_CodeSniffer\Ruleset */ private function getPlainRuleset() { static $ruleset; if (isset($ruleset) === false) { $config = new ConfigDouble(); $ruleset = new Ruleset($config); } return $ruleset; }//end getPlainRuleset() /** * Add mock messages to the message cache. * * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset object. * @param array<string, int> $messages The messages to add to the message cache. * * @return void */ private function mockCachedMessages(Ruleset $ruleset, $messages) { $reflProperty = new ReflectionProperty($ruleset, 'msgCache'); (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true); $msgCache = $reflProperty->getValue($ruleset); foreach ($messages as $msg => $type) { $msgCache->add($msg, $type); } (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false); }//end mockCachedMessages() /** * Invoke the display of the cached messages. * * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset object. * * @return void */ private function invokeDisplayCachedMessages(Ruleset $ruleset) { $reflMethod = new ReflectionMethod($ruleset, 'displayCachedMessages'); (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true); $reflMethod->invoke($ruleset); (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false); }//end invokeDisplayCachedMessages() }//end class
[+]
..
[-] ProcessRulesetShouldProcessElementTest.xml
[edit]
[-] ProcessRulesetAutoExpandSniffsDirectoryTest.xml
[edit]
[-] ProcessRulesetExcludeSniffGroupTest.xml
[edit]
[-] PropertyTypeHandlingTest.xml
[edit]
[-] ExplainCustomRulesetTest.xml
[edit]
[-] ShowSniffDeprecationsInvalidDeprecationVersionTest.xml
[edit]
[-] RegisterSniffsMissingInterfaceTest.php
[edit]
[-] ExpandRulesetReferenceCaseMismatch2Test.xml
[edit]
[-] PopulateTokenListenersTest.php
[edit]
[-] ExpandSniffDirectoryTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidErrorCode1Test.xml
[edit]
[-] SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandardTest.xml
[edit]
[-] ExpandRulesetReferenceTest.php
[edit]
[-] RegisterSniffsMissingInterfaceValidTest.xml
[edit]
[-] ExpandRulesetReferenceHomePathTest.php
[edit]
[-] ExpandRulesetReferenceUnknownCategoryTest.xml
[edit]
[-] ExpandSniffDirectoryTest.php
[edit]
[-] RuleInclusionAbsoluteWindowsTest.php
[edit]
[-] AbstractRulesetTestCase.php
[edit]
[-] ProcessRulesetTest.php
[edit]
[-] ShowSniffDeprecationsEmptyRemovalVersionTest.xml
[edit]
[-] GetIncludePatternsTest.xml
[edit]
[-] RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml
[edit]
[-] RegisterSniffsTest.php
[edit]
[+]
Fixtures
[-] SetPropertyAppliesPropertyToMultipleSniffsInCategoryTest.xml
[edit]
[-] ShowSniffDeprecationsTest.xml
[edit]
[-] ExpandRulesetReferenceMissingFileTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetEmptyFileTest.xml
[edit]
[-] ExpandRulesetReferenceInternalIgnoreTest.xml
[edit]
[-] ExpandRulesetReferenceInternalTest.php
[edit]
[-] ProcessRulesetMiscTest.xml
[edit]
[-] SetPropertyAllowedViaStdClassTest.xml
[edit]
[-] PopulateTokenListenersRegisterNoArrayTest.xml
[edit]
[-] RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml
[edit]
[-] ExpandRulesetReferenceUnknownSniffTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetSingleErrorTest.xml
[edit]
[-] ExpandRulesetReferenceCaseMismatch1Test.xml
[edit]
[-] SetPropertyNotAllowedViaAttributeTest.xml
[edit]
[-] GetIgnorePatternsTest.xml
[edit]
[-] RuleInclusionAbsoluteLinuxTest.xml
[edit]
[-] ShowSniffDeprecationsEmptyDeprecationVersionTest.xml
[edit]
[-] PopulateTokenListenersSupportedTokenizersTest.php
[edit]
[-] ShowSniffDeprecationsInvalidRemovalVersionTest.xml
[edit]
[-] ShowSniffDeprecationsTest.php
[edit]
[-] ProcessRuleInvalidTypeTest.php
[edit]
[-] RegisterSniffsRejectsInvalidSniffTest.php
[edit]
[-] SetPropertyAllowedAsDeclaredTest.xml
[edit]
[-] ShowSniffDeprecationsReportWidthTest.xml
[edit]
[-] ProcessRuleInvalidTypeTest.xml
[edit]
[-] ExpandRulesetReferenceInternalStandardTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidErrorCode2Test.xml
[edit]
[-] PopulateTokenListenersNamingConventionsTest.xml
[edit]
[-] SetSniffPropertyTest.php
[edit]
[-] ProcessRuleShouldProcessElementTest.xml
[edit]
[-] RuleInclusionTest-include.xml
[edit]
[-] ShowSniffDeprecationsInvalidDeprecationMessageTest.xml
[edit]
[-] PopulateTokenListenersSupportedTokenizersTest.xml
[edit]
[-] RuleInclusionTest.xml
[edit]
[-] ProcessRulesetAutoloadFileNotFoundTest.xml
[edit]
[-] RuleInclusionTest.php
[edit]
[-] PopulateTokenListenersNamingConventionsTest.php
[edit]
[-] ExplainSingleSniffTest.xml
[edit]
[-] GetIncludePatternsTest.php
[edit]
[-] GetIgnorePatternsTest.php
[edit]
[-] RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml
[edit]
[-] ExpandRulesetReferenceHomePathFailTest.xml
[edit]
[-] ProcessRuleShouldProcessElementTest.php
[edit]
[-] ExpandRulesetReferenceUnknownStandardTest.xml
[edit]
[-] ExplainTest.php
[edit]
[-] PopulateTokenListenersTest.xml
[edit]
[-] ExpandRulesetReferenceHomePathTest.xml
[edit]
[-] ConstructorTest.php
[edit]
[-] PropertyTypeHandlingTest.php
[edit]
[-] ProcessRulesetAutoloadTest.php
[edit]
[-] PropertyTypeHandlingInlineTest.xml
[edit]
[-] SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategoryTest.xml
[edit]
[-] RegisterSniffsMissingInterfaceInvalidTest.xml
[edit]
[-] ProcessRulesetAutoloadTest.xml
[edit]
[-] SetPropertyAllowedViaMagicMethodTest.xml
[edit]
[-] ProcessRulesetInvalidNoSniffsDirTest.xml
[edit]
[-] ShowSniffDeprecationsOrderTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidErrorCode3Test.xml
[edit]
[-] RuleInclusionAbsoluteLinuxTest.php
[edit]
[-] SetPropertyThrowsErrorOnInvalidPropertyTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetTest.php
[edit]
[-] ConstructorNoSniffsTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidHomePathRefTest.xml
[edit]
[-] ExpandRulesetReferenceTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetMultiErrorTest.xml
[edit]
[-] RuleInclusionAbsoluteWindowsTest.xml
[edit]
[-] ProcessRulesetShouldProcessElementTest.php
[edit]
[-] DisplayCachedMessagesTest.php
[edit]