|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test the Ruleset::expandSniffDirectory() method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Ruleset; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Ruleset; |
| 13 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test handling of sniffs not following the PHPCS naming conventions in the Ruleset::populateTokenListeners() method. |
| 18 | + * |
| 19 | + * @covers \PHP_CodeSniffer\Ruleset::populateTokenListeners |
| 20 | + */ |
| 21 | +final class PopulateTokenListenersNamingConventionsTest extends TestCase |
| 22 | +{ |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Verify a warning is shown for sniffs not complying with the PHPCS naming conventions. |
| 27 | + * |
| 28 | + * Including sniffs which do not comply with the PHPCS naming conventions is soft deprecated since |
| 29 | + * PHPCS 3.12.0, hard deprecated since PHPCS 3.13.0 and support will be removed in PHPCS 4.0.0. |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function testBrokenNamingConventions() |
| 34 | + { |
| 35 | + // Set up the ruleset. |
| 36 | + $standard = __DIR__.'/PopulateTokenListenersNamingConventionsTest.xml'; |
| 37 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 38 | + $ruleset = new Ruleset($config); |
| 39 | + |
| 40 | + // The "Generic.PHP.BacktickOperator" sniff is the only valid sniff. |
| 41 | + $expectedSniffCodes = [ |
| 42 | + '..NoNamespace' => 'NoNamespaceSniff', |
| 43 | + '.Sniffs.MissingCategoryDir' => 'BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff', |
| 44 | + '.Sniffs.PartialNamespace' => 'Sniffs\\PartialNamespaceSniff', |
| 45 | + 'BrokenNamingConventions.Category.' => 'BrokenNamingConventions\\Sniffs\\Category\\Sniff', |
| 46 | + 'BrokenNamingConventions.Sniffs.CategoryCalledSniffs' => 'BrokenNamingConventions\\Sniffs\\Sniffs\\CategoryCalledSniffsSniff', |
| 47 | + 'Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\BacktickOperatorSniff', |
| 48 | + 'Sniffs.SubDir.TooDeeplyNested' => 'BrokenNamingConventions\\Sniffs\\Category\\SubDir\\TooDeeplyNestedSniff', |
| 49 | + ]; |
| 50 | + |
| 51 | + // Sort the value to make the tests stable as different OSes will read directories |
| 52 | + // in a different order and the order is not relevant for these tests. Just the values. |
| 53 | + $actual = $ruleset->sniffCodes; |
| 54 | + ksort($actual); |
| 55 | + |
| 56 | + $this->assertSame($expectedSniffCodes, $actual, 'Registered sniffs do not match expectation'); |
| 57 | + |
| 58 | + $expectedMessage = 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff does not comply'; |
| 59 | + $expectedMessage .= ' with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL; |
| 60 | + $expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL; |
| 61 | + $expectedMessage .= 'DEPRECATED: The sniff NoNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.'; |
| 62 | + $expectedMessage .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL; |
| 63 | + $expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL; |
| 64 | + $expectedMessage .= 'DEPRECATED: The sniff Sniffs\\PartialNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.'; |
| 65 | + $expectedMessage .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL; |
| 66 | + $expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL; |
| 67 | + $expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Category\\Sniff does not comply'; |
| 68 | + $expectedMessage .= ' with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL; |
| 69 | + $expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL; |
| 70 | + $expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Sniffs\\CategoryCalledSniffsSniff does not'; |
| 71 | + $expectedMessage .= ' comply with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL; |
| 72 | + $expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL; |
| 73 | + $expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Category\\SubDir\\TooDeeplyNestedSniff'; |
| 74 | + $expectedMessage .= ' does not comply with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL; |
| 75 | + $expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL.PHP_EOL; |
| 76 | + |
| 77 | + $this->expectOutputString($expectedMessage); |
| 78 | + |
| 79 | + }//end testBrokenNamingConventions() |
| 80 | + |
| 81 | + |
| 82 | +}//end class |
0 commit comments