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

<?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/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 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.MissingInterface.ValidImplements"                  => "$sniffDir\MissingInterface\ValidImplementsSniff",
            "$std.MissingInterface.ValidImplementsViaAbstract"       => "$sniffDir\MissingInterface\ValidImplementsViaAbstractSniff",
            "$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",
            "$std.ValidSniffs.RegisterEmptyArray"                    => "$sniffDir\ValidSniffs\RegisterEmptyArraySniff",
        ];

        // 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\Ruleset
     */
    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

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).