PHP 8.3.31
Preview: ExplainTest.php Size: 8.16 KB
/proc/self/root/opt/cpanel/ea-wappspector/vendor/squizlabs/php_codesniffer/tests/Core/Ruleset/ExplainTest.php

<?php
/**
 * Tests to verify that the "explain" command functions as expected.
 *
 * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
 * @copyright 2023 Juliette Reinders Folmer. All rights reserved.
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Tests\Core\Ruleset;

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

/**
 * Test the Ruleset::explain() function.
 *
 * @covers \PHP_CodeSniffer\Ruleset::explain
 */
final class ExplainTest extends TestCase
{


    /**
     * Test the output of the "explain" command.
     *
     * @return void
     */
    public function testExplain()
    {
        // Set up the ruleset.
        $config  = new ConfigDouble(['--standard=PSR1', '-e']);
        $ruleset = new Ruleset($config);

        $expected  = PHP_EOL;
        $expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
        $expected .= 'Generic (4 sniffs)'.PHP_EOL;
        $expected .= '------------------'.PHP_EOL;
        $expected .= '  Generic.Files.ByteOrderMark'.PHP_EOL;
        $expected .= '  Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
        $expected .= '  Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
        $expected .= '  Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
        $expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
        $expected .= '---------------'.PHP_EOL;
        $expected .= '  PSR1.Classes.ClassDeclaration'.PHP_EOL;
        $expected .= '  PSR1.Files.SideEffects'.PHP_EOL;
        $expected .= '  PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
        $expected .= 'Squiz (1 sniff)'.PHP_EOL;
        $expected .= '---------------'.PHP_EOL;
        $expected .= '  Squiz.Classes.ValidClassName'.PHP_EOL;

        $this->expectOutputString($expected);

        $ruleset->explain();

    }//end testExplain()


    /**
     * Test the output of the "explain" command is not influenced by a user set report width.
     *
     * @return void
     */
    public function testExplainAlwaysDisplaysCompleteSniffName()
    {
        // Set up the ruleset.
        $config  = new ConfigDouble(['--standard=PSR1', '-e', '--report-width=30']);
        $ruleset = new Ruleset($config);

        $expected  = PHP_EOL;
        $expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
        $expected .= 'Generic (4 sniffs)'.PHP_EOL;
        $expected .= '------------------'.PHP_EOL;
        $expected .= '  Generic.Files.ByteOrderMark'.PHP_EOL;
        $expected .= '  Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
        $expected .= '  Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
        $expected .= '  Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
        $expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
        $expected .= '---------------'.PHP_EOL;
        $expected .= '  PSR1.Classes.ClassDeclaration'.PHP_EOL;
        $expected .= '  PSR1.Files.SideEffects'.PHP_EOL;
        $expected .= '  PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
        $expected .= 'Squiz (1 sniff)'.PHP_EOL;
        $expected .= '---------------'.PHP_EOL;
        $expected .= '  Squiz.Classes.ValidClassName'.PHP_EOL;

        $this->expectOutputString($expected);

        $ruleset->explain();

    }//end testExplainAlwaysDisplaysCompleteSniffName()


    /**
     * Test the output of the "explain" command when a ruleset only contains a single sniff.
     *
     * This is mostly about making sure that the summary line uses the correct grammar.
     *
     * @return void
     */
    public function testExplainSingleSniff()
    {
        // Set up the ruleset.
        $standard = __DIR__.'/ExplainSingleSniffTest.xml';
        $config   = new ConfigDouble(["--standard=$standard", '-e']);
        $ruleset  = new Ruleset($config);

        $expected  = PHP_EOL;
        $expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
        $expected .= 'Squiz (1 sniff)'.PHP_EOL;
        $expected .= '---------------'.PHP_EOL;
        $expected .= '  Squiz.Scope.MethodScope'.PHP_EOL;

        $this->expectOutputString($expected);

        $ruleset->explain();

    }//end testExplainSingleSniff()


    /**
     * Test that "explain" works correctly with custom rulesets.
     *
     * Verifies that:
     * - The "standard" name is taken from the custom ruleset.
     * - Any and all sniff additions and exclusions in the ruleset are taken into account correctly.
     * - That the displayed list will have both the standards as well as the sniff names
     *   ordered alphabetically.
     *
     * @return void
     */
    public function testExplainCustomRuleset()
    {
        // Set up the ruleset.
        $standard = __DIR__.'/ExplainCustomRulesetTest.xml';
        $config   = new ConfigDouble(["--standard=$standard", '-e']);
        $ruleset  = new Ruleset($config);

        $expected  = PHP_EOL;
        $expected .= 'The ExplainCustomRulesetTest standard contains 10 sniffs'.PHP_EOL.PHP_EOL;
        $expected .= 'Generic (4 sniffs)'.PHP_EOL;
        $expected .= '------------------'.PHP_EOL;
        $expected .= '  Generic.Files.ByteOrderMark'.PHP_EOL;
        $expected .= '  Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
        $expected .= '  Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
        $expected .= '  Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
        $expected .= 'PSR1 (2 sniffs)'.PHP_EOL;
        $expected .= '---------------'.PHP_EOL;
        $expected .= '  PSR1.Classes.ClassDeclaration'.PHP_EOL;
        $expected .= '  PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
        $expected .= 'PSR12 (2 sniffs)'.PHP_EOL;
        $expected .= '----------------'.PHP_EOL;
        $expected .= '  PSR12.ControlStructures.BooleanOperatorPlacement'.PHP_EOL;
        $expected .= '  PSR12.ControlStructures.ControlStructureSpacing'.PHP_EOL.PHP_EOL;
        $expected .= 'Squiz (2 sniffs)'.PHP_EOL;
        $expected .= '----------------'.PHP_EOL;
        $expected .= '  Squiz.Classes.ValidClassName'.PHP_EOL;
        $expected .= '  Squiz.Scope.MethodScope'.PHP_EOL;

        $this->expectOutputString($expected);

        $ruleset->explain();

    }//end testExplainCustomRuleset()


    /**
     * Test the output of the "explain" command for a standard containing both deprecated
     * and non-deprecated sniffs.
     *
     * Tests that:
     * - Deprecated sniffs are marked with an asterix in the list.
     * - A footnote is displayed explaining the asterix.
     * - And that the "standard uses # deprecated sniffs" listing is **not** displayed.
     *
     * @return void
     */
    public function testExplainWithDeprecatedSniffs()
    {
        // Set up the ruleset.
        $standard = __DIR__."/ShowSniffDeprecationsTest.xml";
        $config   = new ConfigDouble(["--standard=$standard", '-e']);
        $ruleset  = new Ruleset($config);

        $expected  = PHP_EOL;
        $expected .= 'The ShowSniffDeprecationsTest standard contains 11 sniffs'.PHP_EOL.PHP_EOL;

        $expected .= 'TestStandard (11 sniffs)'.PHP_EOL;
        $expected .= '------------------------'.PHP_EOL;
        $expected .= '  TestStandard.Deprecated.WithLongReplacement *'.PHP_EOL;
        $expected .= '  TestStandard.Deprecated.WithoutReplacement *'.PHP_EOL;
        $expected .= '  TestStandard.Deprecated.WithReplacement *'.PHP_EOL;
        $expected .= '  TestStandard.Deprecated.WithReplacementContainingLinuxNewlines *'.PHP_EOL;
        $expected .= '  TestStandard.Deprecated.WithReplacementContainingNewlines *'.PHP_EOL;
        $expected .= '  TestStandard.SetProperty.AllowedAsDeclared'.PHP_EOL;
        $expected .= '  TestStandard.SetProperty.AllowedViaMagicMethod'.PHP_EOL;
        $expected .= '  TestStandard.SetProperty.AllowedViaStdClass'.PHP_EOL;
        $expected .= '  TestStandard.SetProperty.NotAllowedViaAttribute'.PHP_EOL;
        $expected .= '  TestStandard.SetProperty.PropertyTypeHandling'.PHP_EOL;
        $expected .= '  TestStandard.ValidSniffs.RegisterEmptyArray'.PHP_EOL.PHP_EOL;

        $expected .= '* Sniffs marked with an asterisk are deprecated.'.PHP_EOL;

        $this->expectOutputString($expected);

        $ruleset->explain();

    }//end testExplainWithDeprecatedSniffs()


}//end class

Directory Contents

Dirs: 1 × Files: 92

Name Size Perms Modified Actions
Fixtures DIR
- drwxr-xr-x 2025-11-04 16:30:35
Edit Download
3.83 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
217 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
11.11 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
10.83 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
512 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
547 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
425 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.48 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
337 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
643 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
330 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
2.51 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
332 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
333 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
325 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
434 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
364 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
5.35 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
621 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
508 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
515 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
469 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
2.49 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
349 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
374 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
247 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
8.16 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.42 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
742 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.23 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
556 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
4.67 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
1.55 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
465 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
4.13 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
369 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
21.06 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
1.99 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
1.18 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
345 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
592 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
394 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
6.12 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
1.39 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
0 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
420 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
201 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.13 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
362 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
439 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
997 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
12.43 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
1.67 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
11.24 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
24.71 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.31 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
317 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
12.04 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.81 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
355 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
2.08 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
446 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
444 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
454 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
445 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
2.71 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
10.27 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.28 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
482 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
3.19 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
479 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
364 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
18.56 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
1.84 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
728 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
732 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
729 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
338 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
343 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
376 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
439 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
354 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
16.73 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
371 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
367 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
373 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
373 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
369 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
513 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
360 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download
25.61 KB lrw-r--r-- 2025-11-04 16:30:35
Edit Download
622 B lrw-r--r-- 2025-11-04 16:30:35
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).