|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests that invalid sniffs will be rejected with an informative error message. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2025 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 PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests that invalid sniffs will be rejected with an informative error message. |
| 18 | + * |
| 19 | + * @covers \PHP_CodeSniffer\Ruleset::registerSniffs |
| 20 | + */ |
| 21 | +final class RegisterSniffsRejectsInvalidSniffTest extends AbstractRulesetTestCase |
| 22 | +{ |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Verify that an error is thrown if an invalid sniff class is loaded. |
| 27 | + * |
| 28 | + * @param string $standard The standard to use for the test. |
| 29 | + * @param string $methodName The name of the missing method. |
| 30 | + * |
| 31 | + * @dataProvider dataExceptionIsThrownOnMissingInterfaceMethod |
| 32 | + * |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + public function testExceptionIsThrownOnMissingInterfaceMethod($standard, $methodName) |
| 36 | + { |
| 37 | + // Set up the ruleset. |
| 38 | + $standard = __DIR__.'/'.$standard; |
| 39 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 40 | + |
| 41 | + $regex = "`(^|\R)ERROR: Sniff class \S+Sniff is missing required method $methodName\(\)\.\R`"; |
| 42 | + $this->expectRuntimeExceptionRegex($regex); |
| 43 | + |
| 44 | + new Ruleset($config); |
| 45 | + |
| 46 | + }//end testExceptionIsThrownOnMissingInterfaceMethod() |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Data provider. |
| 51 | + * |
| 52 | + * @see testExceptionIsThrownOnMissingInterfaceMethod() |
| 53 | + * |
| 54 | + * @return array<string, array<string, string>> |
| 55 | + */ |
| 56 | + public static function dataExceptionIsThrownOnMissingInterfaceMethod() |
| 57 | + { |
| 58 | + return [ |
| 59 | + 'Missing register() method' => [ |
| 60 | + 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml', |
| 61 | + 'methodName' => 'register', |
| 62 | + ], |
| 63 | + 'Missing process() method' => [ |
| 64 | + 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml', |
| 65 | + 'methodName' => 'process', |
| 66 | + ], |
| 67 | + 'Missing both, checking register() method' => [ |
| 68 | + 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml', |
| 69 | + 'methodName' => 'register', |
| 70 | + ], |
| 71 | + 'Missing both, checking process() method' => [ |
| 72 | + 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml', |
| 73 | + 'methodName' => 'process', |
| 74 | + ], |
| 75 | + ]; |
| 76 | + |
| 77 | + }//end dataExceptionIsThrownOnMissingInterfaceMethod() |
| 78 | + |
| 79 | + |
| 80 | +}//end class |
0 commit comments