File: ProcessRulesetAutoloadTest.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 (164 lines) | stat: -rw-r--r-- 6,263 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
<?php
/**
 * Test handling of <autoload> instructions.
 *
 * @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 <autoload> instructions.
 *
 * Note: these tests need to run in separate processes as otherwise we cannot
 * reliably determine whether or not the correct files were loaded as the
 * underlying code uses `include_once`.
 *
 * @runTestsInSeparateProcesses
 * @preserveGlobalState         disabled
 *
 * @covers \PHP_CodeSniffer\Ruleset::processRuleset
 */
final class ProcessRulesetAutoloadTest extends AbstractRulesetTestCase
{


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

        $originallyIncludes = get_included_files();

        // Set up the ruleset.
        $standard = __DIR__.'/ProcessRulesetAutoloadTest.xml';
        $config   = new ConfigDouble(["--standard=$standard"]);
        new Ruleset($config);

        $finalIncludes = get_included_files();
        $diff          = array_diff($finalIncludes, $originallyIncludes);

        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.1.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.1.php autoload file was not loaded'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.2.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.2.php autoload file was not loaded'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.3.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.3.php autoload file was not loaded'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.4.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.4.php autoload file was not loaded'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'),
            $diff,
            'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was not loaded'
        );
        $this->assertNotContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'),
            $diff,
            'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was loaded, while it shouldn\'t have been'
        );

    }//end testShouldProcessAutoloadCsonly()


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

        $originallyIncludes = get_included_files();

        // Set up the ruleset.
        $standard = __DIR__.'/ProcessRulesetAutoloadTest.xml';
        $config   = new ConfigDouble(["--standard=$standard"]);
        new Ruleset($config);

        $finalIncludes = get_included_files();
        $diff          = array_diff($finalIncludes, $originallyIncludes);

        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.1.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.1.php autoload file was not loaded'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.2.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.2.php autoload file was not loaded'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.3.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.3.php autoload file was not loaded'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.4.php'),
            $diff,
            'ProcessRulesetAutoloadLoadAlways.4.php autoload file was not loaded'
        );
        $this->assertNotContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'),
            $diff,
            'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was loaded, while it shouldn\'t have been'
        );
        $this->assertContains(
            __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'),
            $diff,
            'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was not loaded'
        );

    }//end testShouldProcessAutoloadCbfonly()


    /**
     * Test an exception is thrown when the <autoload> directive points to a file which doesn't exist.
     *
     * @return void
     */
    public function testFileNotFoundException()
    {
        $exceptionMsg = 'The specified autoload file "./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php" does not exist';
        $this->expectRuntimeExceptionMessage($exceptionMsg);

        // Set up the ruleset.
        $standard = __DIR__.'/ProcessRulesetAutoloadFileNotFoundTest.xml';
        $config   = new ConfigDouble(["--standard=$standard"]);
        new Ruleset($config);

    }//end testFileNotFoundException()


}//end class