|  | 
|  | 1 | +<?php | 
|  | 2 | +/** | 
|  | 3 | + * Test the Ruleset::expandRulesetReference() 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 PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase; | 
|  | 15 | + | 
|  | 16 | +/** | 
|  | 17 | + * Test various aspects of the Ruleset::expandRulesetReference() method not covered by other tests. | 
|  | 18 | + * | 
|  | 19 | + * @covers \PHP_CodeSniffer\Ruleset::expandRulesetReference | 
|  | 20 | + */ | 
|  | 21 | +final class ExpandRulesetReferenceTest extends AbstractRulesetTestCase | 
|  | 22 | +{ | 
|  | 23 | + | 
|  | 24 | + | 
|  | 25 | +    /** | 
|  | 26 | +     * Test handling of path references relative to the originally included ruleset. | 
|  | 27 | +     * | 
|  | 28 | +     * @return void | 
|  | 29 | +     */ | 
|  | 30 | +    public function testRulesetRelativePathReferences() | 
|  | 31 | +    { | 
|  | 32 | +        // Set up the ruleset. | 
|  | 33 | +        $standard = __DIR__.'/ExpandRulesetReferenceTest.xml'; | 
|  | 34 | +        $config   = new ConfigDouble(["--standard=$standard"]); | 
|  | 35 | +        $ruleset  = new Ruleset($config); | 
|  | 36 | + | 
|  | 37 | +        $expected = [ | 
|  | 38 | +            'ExternalA.CheckSomething.Valid'              => 'Fixtures\\ExternalA\\Sniffs\\CheckSomething\\ValidSniff', | 
|  | 39 | +            'TestStandard.ValidSniffs.RegisterEmptyArray' => 'Fixtures\\TestStandard\\Sniffs\\ValidSniffs\\RegisterEmptyArraySniff', | 
|  | 40 | +            'ExternalB.CheckMore.Valid'                   => 'Fixtures\\ExternalB\\Sniffs\\CheckMore\\ValidSniff', | 
|  | 41 | +        ]; | 
|  | 42 | + | 
|  | 43 | +        $this->assertSame($expected, $ruleset->sniffCodes); | 
|  | 44 | + | 
|  | 45 | +    }//end testRulesetRelativePathReferences() | 
|  | 46 | + | 
|  | 47 | + | 
|  | 48 | +    /** | 
|  | 49 | +     * Test that an exception is thrown if a ruleset contains an unresolvable reference. | 
|  | 50 | +     * | 
|  | 51 | +     * @param string $standard    The standard to use for the test. | 
|  | 52 | +     * @param string $replacement The reference which will be used in the exception message. | 
|  | 53 | +     * | 
|  | 54 | +     * @dataProvider dataUnresolvableReferenceThrowsException | 
|  | 55 | +     * | 
|  | 56 | +     * @return void | 
|  | 57 | +     */ | 
|  | 58 | +    public function testUnresolvableReferenceThrowsException($standard, $replacement) | 
|  | 59 | +    { | 
|  | 60 | +        // Set up the ruleset. | 
|  | 61 | +        $standard = __DIR__.'/'.$standard; | 
|  | 62 | +        $config   = new ConfigDouble(["--standard=$standard"]); | 
|  | 63 | + | 
|  | 64 | +        $exceptionMessage = 'Referenced sniff "%s" does not exist'; | 
|  | 65 | +        $this->expectRuntimeExceptionMessage(sprintf($exceptionMessage, $replacement)); | 
|  | 66 | + | 
|  | 67 | +        new Ruleset($config); | 
|  | 68 | + | 
|  | 69 | +    }//end testUnresolvableReferenceThrowsException() | 
|  | 70 | + | 
|  | 71 | + | 
|  | 72 | +    /** | 
|  | 73 | +     * Data provider. | 
|  | 74 | +     * | 
|  | 75 | +     * @see testUnresolvableReferenceThrowsException() | 
|  | 76 | +     * | 
|  | 77 | +     * @return array<string, array<string, string>> | 
|  | 78 | +     */ | 
|  | 79 | +    public static function dataUnresolvableReferenceThrowsException() | 
|  | 80 | +    { | 
|  | 81 | +        $data = [ | 
|  | 82 | +            'Referencing a non-existent XML file'                     => [ | 
|  | 83 | +                'standard'    => 'ExpandRulesetReferenceMissingFileTest.xml', | 
|  | 84 | +                'replacement' => './MissingFile.xml', | 
|  | 85 | +            ], | 
|  | 86 | +            'Referencing an invalid directory starting with "~"'      => [ | 
|  | 87 | +                'standard'    => 'ExpandRulesetReferenceInvalidHomePathRefTest.xml', | 
|  | 88 | +                'replacement' => '~/src/Standards/Squiz/Sniffs/Files/', | 
|  | 89 | +            ], | 
|  | 90 | +            'Referencing an unknown standard'                         => [ | 
|  | 91 | +                'standard'    => 'ExpandRulesetReferenceUnknownStandardTest.xml', | 
|  | 92 | +                'replacement' => 'UnknownStandard', | 
|  | 93 | +            ], | 
|  | 94 | +            'Referencing a non-existent category in a known standard' => [ | 
|  | 95 | +                'standard'    => 'ExpandRulesetReferenceUnknownCategoryTest.xml', | 
|  | 96 | +                'replacement' => 'TestStandard.UnknownCategory', | 
|  | 97 | +            ], | 
|  | 98 | +            'Referencing a non-existent sniff in a known standard'    => [ | 
|  | 99 | +                'standard'    => 'ExpandRulesetReferenceUnknownSniffTest.xml', | 
|  | 100 | +                'replacement' => 'TestStandard.InvalidSniffs.UnknownRule', | 
|  | 101 | +            ], | 
|  | 102 | +            'Referencing an invalid error code - no standard name'    => [ | 
|  | 103 | +                'standard'    => 'ExpandRulesetReferenceInvalidErrorCode1Test.xml', | 
|  | 104 | +                'replacement' => '.Invalid.Undetermined.Found', | 
|  | 105 | +            ], | 
|  | 106 | +            'Referencing an invalid error code - no category name'    => [ | 
|  | 107 | +                'standard'    => 'ExpandRulesetReferenceInvalidErrorCode2Test.xml', | 
|  | 108 | +                'replacement' => 'Standard..Undetermined.Found', | 
|  | 109 | +            ], | 
|  | 110 | +            'Referencing an invalid error code - no sniff name'       => [ | 
|  | 111 | +                'standard'    => 'ExpandRulesetReferenceInvalidErrorCode3Test.xml', | 
|  | 112 | +                'replacement' => 'Standard.Invalid..Found', | 
|  | 113 | +            ], | 
|  | 114 | +        ]; | 
|  | 115 | + | 
|  | 116 | +        // Add tests which are only relevant for case-sensitive OSes. | 
|  | 117 | +        if (stripos(PHP_OS, 'WIN') === false) { | 
|  | 118 | +            $data['Referencing an existing sniff, but there is a case mismatch (OS-dependent) [1]'] = [ | 
|  | 119 | +                'standard'    => 'ExpandRulesetReferenceCaseMismatch1Test.xml', | 
|  | 120 | +                'replacement' => 'psr12.functions.nullabletypedeclaration', | 
|  | 121 | +            ]; | 
|  | 122 | +            $data['Referencing an existing sniff, but there is a case mismatch (OS-dependent) [2]'] = [ | 
|  | 123 | +                'standard'    => 'ExpandRulesetReferenceCaseMismatch2Test.xml', | 
|  | 124 | +                'replacement' => 'PSR12.Functions.ReturntypeDeclaration', | 
|  | 125 | +            ]; | 
|  | 126 | +        } | 
|  | 127 | + | 
|  | 128 | +        return $data; | 
|  | 129 | + | 
|  | 130 | +    }//end dataUnresolvableReferenceThrowsException() | 
|  | 131 | + | 
|  | 132 | + | 
|  | 133 | +}//end class | 
0 commit comments