File: ProcessRuleShouldProcessElementTest.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 (660 lines) | stat: -rw-r--r-- 25,039 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<?php
/**
 * Test handling of `phpc(cs|cbf)-only` instructions at rule level.
 *
 * @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;

/**
 * Test handling of `phpc(cs|cbf)-only` instructions at rule level.
 *
 * @covers \PHP_CodeSniffer\Ruleset::processRule
 * @covers \PHP_CodeSniffer\Ruleset::shouldProcessElement
 */
final class ProcessRuleShouldProcessElementTest extends AbstractRulesetTestCase
{

    /**
     * The Ruleset object.
     *
     * @var \PHP_CodeSniffer\Ruleset
     */
    private static $ruleset;


    /**
     * Initialize the config and ruleset objects for this test.
     *
     * @before
     *
     * @return void
     */
    protected function initializeConfigAndRuleset()
    {
        if (isset(self::$ruleset) === false) {
            // Set up the ruleset.
            $standard      = __DIR__.'/ProcessRuleShouldProcessElementTest.xml';
            $config        = new ConfigDouble(["--standard=$standard"]);
            self::$ruleset = new Ruleset($config);
        }

    }//end initializeConfigAndRuleset()


    /**
     * Verify that in CS mode, phpcs-only <severity> directives are set and phpcbf-only <severity>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessSeverityCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        $key = 'severity';

        // Verify that the non-selective severity directive IS applied.
        $sniffCode = 'PSR1.Files.SideEffects';
        $this->assertRulesetPropertySame(3, $sniffCode, $key);

        // Verify that the CS-only severity directive IS applied.
        $sniffCode = 'Generic.Metrics.CyclomaticComplexity';
        $this->assertRulesetPropertySame(2, $sniffCode, $key);

        // Verify that the CBF-only severity directive is NOT applied.
        $sniffCode = 'PSR2.Namespaces.NamespaceDeclaration';
        $this->assertNotHasRulesetDirective($sniffCode, $key);

    }//end testShouldProcessSeverityCsonly()


    /**
     * Verify that in CBF mode, phpcbf-only <severity> directives are set and phpcs-only <severity>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessSeverityCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        $key = 'severity';

        // Verify that the non-selective severity directive IS applied.
        $sniffCode = 'PSR1.Files.SideEffects';
        $this->assertRulesetPropertySame(3, $sniffCode, $key);

        // Verify that the CS-only severity directive is NOT applied.
        $sniffCode = 'Generic.Metrics.CyclomaticComplexity';
        $this->assertNotHasRulesetDirective($sniffCode, $key);

        // Verify that the CBF-only severity directive IS applied.
        $sniffCode = 'PSR2.Namespaces.NamespaceDeclaration';
        $this->assertRulesetPropertySame(4, $sniffCode, $key);

    }//end testShouldProcessSeverityCbfonly()


    /**
     * Verify that in CS mode, phpcs-only <type> directives are set and phpcbf-only <type>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessTypeCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        $key = 'type';

        // Verify that the non-selective type directive IS applied.
        $sniffCode = 'PSR1.Files.SideEffects';
        $this->assertRulesetPropertySame('warning', $sniffCode, $key);

        // Verify that the CS-only type directive IS applied.
        $sniffCode = 'Generic.Metrics.CyclomaticComplexity';
        $this->assertRulesetPropertySame('warning', $sniffCode, $key);

        // Verify that the CBF-only type directive is NOT applied.
        $sniffCode = 'PSR2.Namespaces.NamespaceDeclaration';
        $this->assertNotHasRulesetDirective($sniffCode, $key);

    }//end testShouldProcessTypeCsonly()


    /**
     * Verify that in CBF mode, phpcbf-only <type> directives are set and phpcs-only <type>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessTypeCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        $key = 'type';

        // Verify that the non-selective type directive IS applied.
        $sniffCode = 'PSR1.Files.SideEffects';
        $this->assertRulesetPropertySame('warning', $sniffCode, $key);

        // Verify that the CS-only type directive is NOT applied.
        $sniffCode = 'Generic.Metrics.CyclomaticComplexity';
        $this->assertNotHasRulesetDirective($sniffCode, $key);

        // Verify that the CBF-only type directive IS applied.
        $sniffCode = 'PSR2.Namespaces.NamespaceDeclaration';
        $this->assertRulesetPropertySame('error', $sniffCode, $key);

    }//end testShouldProcessTypeCbfonly()


    /**
     * Verify that in CS mode, phpcs-only <message> directives are set and phpcbf-only <message>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessMessageCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        $key = 'message';

        // Verify that the non-selective message directive IS applied.
        $sniffCode = 'PSR1.Files.SideEffects';
        $this->assertRulesetPropertySame('A different warning message', $sniffCode, $key);

        // Verify that the CS-only message directive IS applied.
        $sniffCode = 'Generic.Metrics.CyclomaticComplexity';
        $this->assertRulesetPropertySame('A different warning but only for phpcs', $sniffCode, $key);

        // Verify that the CBF-only message directive is NOT applied.
        $sniffCode = 'PSR2.Namespaces.NamespaceDeclaration';
        $this->assertNotHasRulesetDirective($sniffCode, $key);

    }//end testShouldProcessMessageCsonly()


    /**
     * Verify that in CBF mode, phpcbf-only <message> directives are set and phpcs-only <message>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessMessageCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        $key = 'message';

        // Verify that the non-selective message directive IS applied.
        $sniffCode = 'PSR1.Files.SideEffects';
        $this->assertRulesetPropertySame('A different warning message', $sniffCode, $key);

        // Verify that the CS-only message directive is NOT applied.
        $sniffCode = 'Generic.Metrics.CyclomaticComplexity';
        $this->assertNotHasRulesetDirective($sniffCode, $key);

        // Verify that the CBF-only message directive IS applied.
        $sniffCode = 'PSR2.Namespaces.NamespaceDeclaration';
        $this->assertRulesetPropertySame('A different warning but only for phpcbf', $sniffCode, $key);

    }//end testShouldProcessMessageCbfonly()


    /**
     * Verify that in CS mode, phpcs-only <include-pattern> directives are set and phpcbf-only <include-pattern>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessIncludePatternCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        $includedKey = './vendor/';

        // Verify that the non-selective include-pattern directive IS applied.
        $sniffCode = 'PSR1.Methods.CamelCapsMethodName';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->includePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($includedKey, self::$ruleset->includePatterns[$sniffCode], "Include pattern for sniff $sniffCode not registered");

        // Verify that the CS-only include-pattern directive IS applied.
        $sniffCode = 'Generic.Files.LineLength';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->includePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($includedKey, self::$ruleset->includePatterns[$sniffCode], "Include pattern for sniff $sniffCode not registered");

        // Verify that the CBF-only include-pattern directive is NOT applied.
        $sniffCode = 'PSR2.Files.ClosingTag';
        $this->assertArrayNotHasKey($sniffCode, self::$ruleset->includePatterns, "Sniff $sniffCode was registered");

    }//end testShouldProcessIncludePatternCsonly()


    /**
     * Verify that in CS mode, phpcbf-only <include-pattern> directives are set and phpcs-only <include-pattern>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessIncludePatternCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        $includedKey = './vendor/';

        // Verify that the non-selective include-pattern directive IS applied.
        $sniffCode = 'PSR1.Methods.CamelCapsMethodName';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->includePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($includedKey, self::$ruleset->includePatterns[$sniffCode], "Include pattern for sniff $sniffCode not registered");

        // Verify that the CS-only include-pattern directive is NOT applied.
        $sniffCode = 'Generic.Files.LineLength';
        $this->assertArrayNotHasKey($sniffCode, self::$ruleset->includePatterns, "Sniff $sniffCode was registered");

        // Verify that the CBF-only include-pattern directive is IS applied.
        $sniffCode = 'PSR2.Files.ClosingTag';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->includePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($includedKey, self::$ruleset->includePatterns[$sniffCode], "Include pattern for sniff $sniffCode not registered");

    }//end testShouldProcessIncludePatternCbfonly()


    /**
     * Verify that in CS mode, phpcs-only <exclude-pattern> directives are set and phpcbf-only <exclude-pattern>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessExcludePatternCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        $excludedKey = './tests/';

        // Verify that the non-selective exclude-pattern directive IS applied.
        $sniffCode = 'PSR1.Classes.ClassDeclaration';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->ignorePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($excludedKey, self::$ruleset->ignorePatterns[$sniffCode], "Ignore pattern for sniff $sniffCode not registered");

        // Verify that the CS-only exclude-pattern directive IS applied.
        $sniffCode = 'Generic.Formatting.SpaceAfterCast';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->ignorePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($excludedKey, self::$ruleset->ignorePatterns[$sniffCode], "Ignore pattern for sniff $sniffCode not registered");

        // Verify that the CBF-only exclude-pattern directive is NOT applied.
        $sniffCode = 'PSR2.Methods.FunctionClosingBrace';
        $this->assertArrayNotHasKey($sniffCode, self::$ruleset->ignorePatterns, "Sniff $sniffCode was registered");

    }//end testShouldProcessExcludePatternCsonly()


    /**
     * Verify that in CS mode, phpcbf-only <exclude-pattern> directives are set and phpcs-only <exclude-pattern>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessExcludePatternCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        $excludedKey = './tests/';

        // Verify that the non-selective exclude-pattern directive IS applied.
        $sniffCode = 'PSR1.Classes.ClassDeclaration';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->ignorePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($excludedKey, self::$ruleset->ignorePatterns[$sniffCode], "Ignore pattern for sniff $sniffCode not registered");

        // Verify that the CS-only exclude-pattern directive is NOT applied.
        $sniffCode = 'Generic.Formatting.SpaceAfterCast';
        $this->assertArrayNotHasKey($sniffCode, self::$ruleset->ignorePatterns, "Sniff $sniffCode was registered");

        // Verify that the CBF-only exclude-pattern directive is IS applied.
        $sniffCode = 'PSR2.Methods.FunctionClosingBrace';
        $this->assertArrayHasKey($sniffCode, self::$ruleset->ignorePatterns, "Sniff $sniffCode not registered");
        $this->assertArrayHasKey($excludedKey, self::$ruleset->ignorePatterns[$sniffCode], "Ignore pattern for sniff $sniffCode not registered");

    }//end testShouldProcessExcludePatternCbfonly()


    /**
     * Verify that in CS mode, phpcs-only <properties> directives are set and phpcbf-only <properties>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessPropertiesCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        $csSniffClass  = 'PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff';
        $cbfSniffClass = 'PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff';

        $propertyName    = 'indent';
        $propertyDefault = 4;
        $propertyChanged = '2';

        // Verify that the CS-only property directive IS applied.
        $this->assertArrayHasKey($csSniffClass, self::$ruleset->sniffs, "Sniff $csSniffClass not registered");
        $this->assertXObjectHasProperty($propertyName, self::$ruleset->sniffs[$csSniffClass]);

        $actualValue = self::$ruleset->sniffs[$csSniffClass]->$propertyName;
        $this->assertSame($propertyChanged, $actualValue, 'cs-only property change directive not applied');

        // Verify that the CBF-only property directive is NOT applied.
        $this->assertArrayHasKey($cbfSniffClass, self::$ruleset->sniffs, "Sniff $cbfSniffClass not registered");
        $this->assertXObjectHasProperty($propertyName, self::$ruleset->sniffs[$cbfSniffClass]);

        $actualValue = self::$ruleset->sniffs[$cbfSniffClass]->$propertyName;
        $this->assertSame($propertyDefault, $actualValue, 'cbf-only property change directive was applied');

    }//end testShouldProcessPropertiesCsonly()


    /**
     * Verify that in CBF mode, phpcbf-only <properties> directives are set and phpcs-only <properties>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessPropertiesCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        $csSniffClass  = 'PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff';
        $cbfSniffClass = 'PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff';

        $propertyName    = 'indent';
        $propertyDefault = 4;
        $propertyChanged = '2';

        // Verify that the CS-only property directive is NOT applied.
        $this->assertArrayHasKey($csSniffClass, self::$ruleset->sniffs, "Sniff $csSniffClass not registered");
        $this->assertXObjectHasProperty($propertyName, self::$ruleset->sniffs[$csSniffClass]);

        $actualValue = self::$ruleset->sniffs[$csSniffClass]->$propertyName;
        $this->assertSame($propertyDefault, $actualValue, 'cs-only property change directive was applied');

        // Verify that the CBF-only property directive IS applied.
        $this->assertArrayHasKey($cbfSniffClass, self::$ruleset->sniffs, "Sniff $cbfSniffClass not registered");
        $this->assertXObjectHasProperty($propertyName, self::$ruleset->sniffs[$cbfSniffClass]);

        $actualValue = self::$ruleset->sniffs[$cbfSniffClass]->$propertyName;
        $this->assertSame($propertyChanged, $actualValue, 'cbf-only property change directive not applied');

    }//end testShouldProcessPropertiesCbfonly()


    /**
     * Verify that in CS mode, phpcs-only <property> directives are set and phpcbf-only <property>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessPropertyCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        // Verify the sniff is registed.
        $sniffClass = 'PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ScopeIndentSniff';
        $this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, "Sniff $sniffClass not registered");

        $sniffObject = self::$ruleset->sniffs[$sniffClass];

        // Verify that the non-selective property directive IS applied.
        $propertyName = 'exact';
        $expected     = true;

        $this->assertXObjectHasProperty($propertyName, $sniffObject);
        $this->assertSame($expected, $sniffObject->$propertyName, 'Non-selective property change directive not applied');

        // Verify that the CS-only property directive IS applied.
        $propertyName = 'indent';
        $expected     = '2';

        $this->assertXObjectHasProperty($propertyName, $sniffObject);
        $this->assertSame($expected, $sniffObject->$propertyName, 'cs-only property change directive not applied');

        // Verify that the CBF-only property directive is NOT applied.
        $propertyName    = 'tabIndent';
        $expectedDefault = false;

        $this->assertXObjectHasProperty($propertyName, $sniffObject);
        $this->assertSame($expectedDefault, $sniffObject->$propertyName, 'cbf-only property change directive was applied');

    }//end testShouldProcessPropertyCsonly()


    /**
     * Verify that in CBF mode, phpcbf-only <property> directives are set and phpcs-only <property>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessPropertyCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        // Verify the sniff is registed.
        $sniffClass = 'PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ScopeIndentSniff';
        $this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, "Sniff $sniffClass not registered");

        $sniffObject = self::$ruleset->sniffs[$sniffClass];

        // Verify that the non-selective property directive IS applied.
        $propertyName = 'exact';
        $expected     = true;

        $this->assertXObjectHasProperty($propertyName, $sniffObject);
        $this->assertSame($expected, $sniffObject->$propertyName, 'Non-selective property change directive not applied');

        // Verify that the CS-only property directive is NOT applied.
        $propertyName    = 'indent';
        $expectedDefault = 4;

        $this->assertXObjectHasProperty($propertyName, $sniffObject);
        $this->assertSame($expectedDefault, $sniffObject->$propertyName, 'cs-only property change directive was applied');

        // Verify that the CBF-only property directive IS applied.
        $propertyName = 'tabIndent';
        $expected     = true;

        $this->assertXObjectHasProperty($propertyName, $sniffObject);
        $this->assertSame($expected, $sniffObject->$propertyName, 'cbf-only property change directive not applied');

    }//end testShouldProcessPropertyCbfonly()


    /**
     * Verify that in CS mode, phpcs-only <element> directives are set and phpcbf-only <element>
     * directives are ignored.
     *
     * @return void
     */
    public function testShouldProcessElementCsonly()
    {
        if (PHP_CODESNIFFER_CBF === true) {
            $this->markTestSkipped('This test needs CS mode to run');
        }

        $expected = [
            // Non-selective element directive.
            'T_COMMENT',
            // Phpcs-only element directive.
            'T_CLASS',
            // Non-selective element directive via `extend`.
            'T_BACKTICK',
            // Phpcs-only element directive via `extend`.
            'T_INTERFACE',
        ];

        $this->verifyShouldProcessElement($expected);

    }//end testShouldProcessElementCsonly()


    /**
     * Verify that in CBF mode, phpcbf-only <element> directives are set and phpcs-only <element>
     * directives are ignored.
     *
     * @group CBF
     *
     * @return void
     */
    public function testShouldProcessElementCbfonly()
    {
        if (PHP_CODESNIFFER_CBF === false) {
            $this->markTestSkipped('This test needs CBF mode to run');
        }

        $expected = [
            // Non-selective element directive.
            'T_COMMENT',
            // Phpcbf-only element directive.
            'T_ENUM',
            // Non-selective element directive via `extend`.
            'T_BACKTICK',
            // Phpcbf-only element directive via `extend`.
            'T_TRAIT',
        ];

        $this->verifyShouldProcessElement($expected);

    }//end testShouldProcessElementCbfonly()


    /**
     * Verify that <element> directives are set correctly.
     *
     * @param array<string> $expected Expected sniff property value.
     *
     * @return void
     */
    private function verifyShouldProcessElement($expected)
    {
        // Verify the sniff is registed.
        $sniffClass = 'PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ScopeIndentSniff';
        $this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, "Sniff $sniffClass not registered");

        // Verify the target property exists.
        $sniffObject  = self::$ruleset->sniffs[$sniffClass];
        $propertyName = 'ignoreIndentationTokens';

        $this->assertXObjectHasProperty($propertyName, $sniffObject);

        // Verify the value.
        $actualValue = $sniffObject->$propertyName;
        $this->assertSame($expected, $actualValue, 'Selective element directives not applied correctly');

    }//end verifyShouldProcessElement()


    /**
     * Custom assertion to verify that a Ruleset `$ruleset` property has a certain directive set for a certain sniff code.
     *
     * @param string $sniffCode Sniff code.
     * @param string $key       Array key.
     *
     * @return void
     */
    private function assertHasRulesetDirective($sniffCode, $key)
    {
        $this->assertArrayHasKey($sniffCode, self::$ruleset->ruleset, "Sniff $sniffCode not registered");
        $this->assertTrue(is_array(self::$ruleset->ruleset[$sniffCode]), "Sniff $sniffCode is not an array");
        $this->assertArrayHasKey($key, self::$ruleset->ruleset[$sniffCode], "Directive $key not registered for sniff $sniffCode");

    }//end assertHasRulesetDirective()


    /**
     * Custom assertion to verify that a Ruleset `$ruleset` property does NOT have a certain directive set for a certain sniff code.
     *
     * @param string $sniffCode Sniff code.
     * @param string $key       Array key.
     *
     * @return void
     */
    private function assertNotHasRulesetDirective($sniffCode, $key)
    {
        if (isset(self::$ruleset->ruleset[$sniffCode]) === true
            && is_array(self::$ruleset->ruleset[$sniffCode]) === true
            && isset(self::$ruleset->ruleset[$sniffCode][$key]) === true
        ) {
            $this->fail("Directive $key is registered for sniff $sniffCode");
        }

    }//end assertNotHasRulesetDirective()


    /**
     * Custom assertion to verify that the value of a certain directive for a certain sniff code on the ruleset is correct.
     *
     * @param mixed  $expected  Expected value.
     * @param string $sniffCode Sniff code.
     * @param string $key       Array key.
     *
     * @return void
     */
    private function assertRulesetPropertySame($expected, $sniffCode, $key)
    {
        $this->assertHasRulesetDirective($sniffCode, $key);

        $actual = self::$ruleset->ruleset[$sniffCode][$key];
        $this->assertSame($expected, $actual, "Value for $key on sniff $sniffCode does not meet expectations");

    }//end assertRulesetPropertySame()


}//end class