File: ProcessRulesetTest.php

package info (click to toggle)
php-codesniffer 3.11.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,772 kB
  • sloc: php: 84,771; pascal: 10,061; xml: 6,832; javascript: 2,096; sh: 11; makefile: 4
file content (263 lines) | stat: -rw-r--r-- 11,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
 * Test the Ruleset::processRuleset() method.
 *
 * @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 PHPUnit\Framework\TestCase;

/**
 * Test various aspects of the Ruleset::processRuleset() method not covered via other tests.
 *
 * @covers \PHP_CodeSniffer\Ruleset::processRuleset
 */
final class ProcessRulesetTest extends TestCase
{


    /**
     * Verify that a registered standard which doesn't have a "Sniffs" directory, but does have a file
     * called "Sniffs" doesn't result in any errors being thrown.
     *
     * @return void
     */
    public function testSniffsFileNotDirectory()
    {
        // Set up the ruleset.
        $standard = __DIR__.'/ProcessRulesetInvalidNoSniffsDirTest.xml';
        $config   = new ConfigDouble(["--standard=$standard"]);
        $ruleset  = new Ruleset($config);

        $expected = ['Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\BacktickOperatorSniff'];

        $this->assertSame($expected, $ruleset->sniffCodes);

    }//end testSniffsFileNotDirectory()


    /**
     * Verify that all sniffs in a registered standard included in a ruleset automatically get added.
     *
     * @return void
     */
    public function testAutoExpandSniffsDirectory()
    {
        // Set up the ruleset.
        $standard = __DIR__.'/ProcessRulesetAutoExpandSniffsDirectoryTest.xml';
        $config   = new ConfigDouble(["--standard=$standard"]);
        $ruleset  = new Ruleset($config);

        $std      = 'TestStandard';
        $sniffDir = 'Fixtures\TestStandard\Sniffs';
        $expected = [
            "$std.Deprecated.WithLongReplacement"                    => "$sniffDir\Deprecated\WithLongReplacementSniff",
            "$std.Deprecated.WithReplacement"                        => "$sniffDir\Deprecated\WithReplacementSniff",
            "$std.Deprecated.WithReplacementContainingLinuxNewlines" => "$sniffDir\Deprecated\WithReplacementContainingLinuxNewlinesSniff",
            "$std.Deprecated.WithReplacementContainingNewlines"      => "$sniffDir\Deprecated\WithReplacementContainingNewlinesSniff",
            "$std.Deprecated.WithoutReplacement"                     => "$sniffDir\Deprecated\WithoutReplacementSniff",
            "$std.DeprecatedInvalid.EmptyDeprecationVersion"         => "$sniffDir\DeprecatedInvalid\EmptyDeprecationVersionSniff",
            "$std.DeprecatedInvalid.EmptyRemovalVersion"             => "$sniffDir\DeprecatedInvalid\EmptyRemovalVersionSniff",
            "$std.DeprecatedInvalid.InvalidDeprecationMessage"       => "$sniffDir\DeprecatedInvalid\InvalidDeprecationMessageSniff",
            "$std.DeprecatedInvalid.InvalidDeprecationVersion"       => "$sniffDir\DeprecatedInvalid\InvalidDeprecationVersionSniff",
            "$std.DeprecatedInvalid.InvalidRemovalVersion"           => "$sniffDir\DeprecatedInvalid\InvalidRemovalVersionSniff",
            "$std.SetProperty.AllowedAsDeclared"                     => "$sniffDir\SetProperty\AllowedAsDeclaredSniff",
            "$std.SetProperty.AllowedViaMagicMethod"                 => "$sniffDir\SetProperty\AllowedViaMagicMethodSniff",
            "$std.SetProperty.AllowedViaStdClass"                    => "$sniffDir\SetProperty\AllowedViaStdClassSniff",
            "$std.SetProperty.NotAllowedViaAttribute"                => "$sniffDir\SetProperty\NotAllowedViaAttributeSniff",
            "$std.SetProperty.PropertyTypeHandling"                  => "$sniffDir\SetProperty\PropertyTypeHandlingSniff",
        ];

        // Sort the value to make the tests stable as different OSes will read directories
        // in a different order and the order is not relevant for these tests. Just the values.
        $actual = $ruleset->sniffCodes;
        ksort($actual);

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

    }//end testAutoExpandSniffsDirectory()


    /**
     * Verify handling of exclusions of groups of sniffs after inclusion via an even larger "group".
     *
     * @return void
     */
    public function testExcludeSniffGroup()
    {
        // Set up the ruleset.
        $standard = __DIR__.'/ProcessRulesetExcludeSniffGroupTest.xml';
        $config   = new ConfigDouble(["--standard=$standard"]);
        $ruleset  = new Ruleset($config);

        $expected = [
            'PSR1.Classes.ClassDeclaration'    => 'PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff',
            'PSR1.Methods.CamelCapsMethodName' => 'PHP_CodeSniffer\Standards\PSR1\Sniffs\Methods\CamelCapsMethodNameSniff',
        ];

        // Sort the value to make the tests stable as different OSes will read directories
        // in a different order and the order is not relevant for these tests. Just the values.
        $actual = $ruleset->sniffCodes;
        ksort($actual);

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

    }//end testExcludeSniffGroup()


    /*
     * No test for <ini> without "name" as there is nothing we can assert to verify it's being ignored.
     */


    /**
     * Test that an `<ini>` directive without a "value" attribute will be set to the ini equivalent of `true`.
     *
     * @return void
     */
    public function testIniWithoutValue()
    {
        $originalValue = ini_get('user_agent');

        // Set up the ruleset.
        $this->getMiscRuleset();

        $actualValue = ini_get('user_agent');
        // Reset the ini to its original value before the assertion to ensure it's never left in an incorrect state.
        if ($originalValue !== false) {
            ini_set('user_agent', $originalValue);
        }

        $this->assertSame('1', $actualValue);

    }//end testIniWithoutValue()


    /**
     * Verify that inclusion of a single error code:
     * - Includes the sniff, but sets "severity" for the sniff to 0;
     * - Sets "severity" for the specific error code included to 5.;
     *
     * @return void
     */
    public function testIncludeSingleErrorCode()
    {
        // Set up the ruleset.
        $ruleset = $this->getMiscRuleset();

        $key = 'severity';

        $sniffCode = 'Generic.PHP.RequireStrictTypes';
        $this->assertArrayHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode not registered");
        $this->assertTrue(is_array($ruleset->ruleset[$sniffCode]), "Sniff $sniffCode is not an array");
        $this->assertArrayHasKey($key, $ruleset->ruleset[$sniffCode], "Directive $key not registered for sniff $sniffCode");
        $this->assertSame(0, $ruleset->ruleset[$sniffCode][$key], "$key has unexpected value for sniff $sniffCode");

        $sniffCode = 'Generic.PHP.RequireStrictTypes.MissingDeclaration';
        $this->assertArrayHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode not registered");
        $this->assertTrue(is_array($ruleset->ruleset[$sniffCode]), "Sniff $sniffCode is not an array");
        $this->assertArrayHasKey($key, $ruleset->ruleset[$sniffCode], "Directive $key not registered for sniff $sniffCode");
        $this->assertSame(5, $ruleset->ruleset[$sniffCode][$key], "$key has unexpected value for sniff $sniffCode");

    }//end testIncludeSingleErrorCode()


    /**
     * Verify that if all error codes, save one, from a sniff were previously excluded, an include for an additional
     * error code from that same sniff will be respected.
     *
     * @return void
     */
    public function testErrorCodeIncludeAfterExclude()
    {
        // Set up the ruleset.
        $ruleset = $this->getMiscRuleset();

        $key = 'severity';

        $sniffCode = 'PEAR.Files.IncludingFile';
        $this->assertArrayHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode not registered");
        $this->assertTrue(is_array($ruleset->ruleset[$sniffCode]), "Sniff $sniffCode is not an array");
        $this->assertArrayHasKey($key, $ruleset->ruleset[$sniffCode], "Directive $key not registered for sniff $sniffCode");
        $this->assertSame(0, $ruleset->ruleset[$sniffCode][$key], "$key has unexpected value for sniff $sniffCode");

        $sniffCode = 'PEAR.Files.IncludingFile.BracketsNotRequired';
        $this->assertArrayHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode not registered");
        $this->assertTrue(is_array($ruleset->ruleset[$sniffCode]), "Sniff $sniffCode is not an array");
        $this->assertArrayHasKey($key, $ruleset->ruleset[$sniffCode], "Directive $key not registered for sniff $sniffCode");
        $this->assertSame(5, $ruleset->ruleset[$sniffCode][$key], "$key has unexpected value for sniff $sniffCode");

        $sniffCode = 'PEAR.Files.IncludingFile.UseRequire';
        $this->assertArrayHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode not registered");
        $this->assertTrue(is_array($ruleset->ruleset[$sniffCode]), "Sniff $sniffCode is not an array");
        $this->assertArrayHasKey($key, $ruleset->ruleset[$sniffCode], "Directive $key not registered for sniff $sniffCode");
        $this->assertSame(5, $ruleset->ruleset[$sniffCode][$key], "$key has unexpected value for sniff $sniffCode");

    }//end testErrorCodeIncludeAfterExclude()


    /**
     * Verify that a <rule> element without a "ref" is completely ignored.
     *
     * @return void
     */
    public function testRuleWithoutRefIsIgnored()
    {
        // Set up the ruleset.
        $ruleset = $this->getMiscRuleset();

        $sniffCode = 'Generic.Metrics.CyclomaticComplexity';
        $this->assertArrayNotHasKey($sniffCode, $ruleset->sniffCodes, "Sniff $sniffCode registered");
        $this->assertArrayNotHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode adjusted");

    }//end testRuleWithoutRefIsIgnored()


    /**
     * Verify that no "ruleset adjustments" are registered via an `<exclude>` without a "name".
     *
     * @return void
     */
    public function testRuleExcludeWithoutNameIsIgnored()
    {
        // Set up the ruleset.
        $ruleset = $this->getMiscRuleset();

        $sniffCode = 'Generic.PHP.BacktickOperator';
        $this->assertArrayHasKey($sniffCode, $ruleset->sniffCodes, "Sniff $sniffCode not registered");
        $this->assertArrayNotHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode adjusted");

        $sniffCode = 'Generic.PHP.BacktickOperator.Found';
        $this->assertArrayNotHasKey($sniffCode, $ruleset->ruleset, "Sniff $sniffCode adjusted");

    }//end testRuleExcludeWithoutNameIsIgnored()


    /**
     * Test Helper.
     *
     * @return \PHP_CodeSniffer\Sniffs\Sniff
     */
    private function getMiscRuleset()
    {
        static $ruleset;

        if (isset($ruleset) === false) {
            // Set up the ruleset.
            $standard = __DIR__.'/ProcessRulesetMiscTest.xml';
            $config   = new ConfigDouble(["--standard=$standard"]);
            $ruleset  = new Ruleset($config);
        }

        return $ruleset;

    }//end getMiscRuleset()


}//end class