|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test handling of <autoload> instructions. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Ruleset; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Ruleset; |
| 13 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 14 | +use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test handling of <autoload> instructions. |
| 18 | + * |
| 19 | + * Note: these tests need to run in separate processes as otherwise we cannot |
| 20 | + * reliably determine whether or not the correct files were loaded as the |
| 21 | + * underlying code uses `include_once`. |
| 22 | + * |
| 23 | + * @runTestsInSeparateProcesses |
| 24 | + * @preserveGlobalState disabled |
| 25 | + * |
| 26 | + * @covers \PHP_CodeSniffer\Ruleset::processRuleset |
| 27 | + */ |
| 28 | +final class ProcessRulesetAutoloadTest extends AbstractRulesetTestCase |
| 29 | +{ |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * Verify that in CS mode, phpcs-only <autoload> directives are respected and phpcbf-only <autoload> |
| 34 | + * directives are ignored. |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function testShouldProcessAutoloadCsonly() |
| 39 | + { |
| 40 | + if (PHP_CODESNIFFER_CBF === true) { |
| 41 | + $this->markTestSkipped('This test needs CS mode to run'); |
| 42 | + } |
| 43 | + |
| 44 | + $originallyIncludes = get_included_files(); |
| 45 | + |
| 46 | + // Set up the ruleset. |
| 47 | + $standard = __DIR__.'/ProcessRulesetAutoloadTest.xml'; |
| 48 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 49 | + new Ruleset($config); |
| 50 | + |
| 51 | + $finalIncludes = get_included_files(); |
| 52 | + $diff = array_diff($finalIncludes, $originallyIncludes); |
| 53 | + |
| 54 | + $this->assertContains( |
| 55 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.1.php'), |
| 56 | + $diff, |
| 57 | + 'ProcessRulesetAutoloadLoadAlways.1.php autoload file was not loaded' |
| 58 | + ); |
| 59 | + $this->assertContains( |
| 60 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.2.php'), |
| 61 | + $diff, |
| 62 | + 'ProcessRulesetAutoloadLoadAlways.2.php autoload file was not loaded' |
| 63 | + ); |
| 64 | + $this->assertContains( |
| 65 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.3.php'), |
| 66 | + $diff, |
| 67 | + 'ProcessRulesetAutoloadLoadAlways.3.php autoload file was not loaded' |
| 68 | + ); |
| 69 | + $this->assertContains( |
| 70 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.4.php'), |
| 71 | + $diff, |
| 72 | + 'ProcessRulesetAutoloadLoadAlways.4.php autoload file was not loaded' |
| 73 | + ); |
| 74 | + $this->assertContains( |
| 75 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'), |
| 76 | + $diff, |
| 77 | + 'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was not loaded' |
| 78 | + ); |
| 79 | + $this->assertNotContains( |
| 80 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'), |
| 81 | + $diff, |
| 82 | + 'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was loaded, while it shouldn\'t have been' |
| 83 | + ); |
| 84 | + |
| 85 | + }//end testShouldProcessAutoloadCsonly() |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Verify that in CBF mode, phpcbf-only <autoload> directives are respected and phpcs-only <autoload> |
| 90 | + * directives are ignored. |
| 91 | + * |
| 92 | + * @group CBF |
| 93 | + * |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function testShouldProcessAutoloadCbfonly() |
| 97 | + { |
| 98 | + if (PHP_CODESNIFFER_CBF === false) { |
| 99 | + $this->markTestSkipped('This test needs CBF mode to run'); |
| 100 | + } |
| 101 | + |
| 102 | + $originallyIncludes = get_included_files(); |
| 103 | + |
| 104 | + // Set up the ruleset. |
| 105 | + $standard = __DIR__.'/ProcessRulesetAutoloadTest.xml'; |
| 106 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 107 | + new Ruleset($config); |
| 108 | + |
| 109 | + $finalIncludes = get_included_files(); |
| 110 | + $diff = array_diff($finalIncludes, $originallyIncludes); |
| 111 | + |
| 112 | + $this->assertContains( |
| 113 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.1.php'), |
| 114 | + $diff, |
| 115 | + 'ProcessRulesetAutoloadLoadAlways.1.php autoload file was not loaded' |
| 116 | + ); |
| 117 | + $this->assertContains( |
| 118 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.2.php'), |
| 119 | + $diff, |
| 120 | + 'ProcessRulesetAutoloadLoadAlways.2.php autoload file was not loaded' |
| 121 | + ); |
| 122 | + $this->assertContains( |
| 123 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.3.php'), |
| 124 | + $diff, |
| 125 | + 'ProcessRulesetAutoloadLoadAlways.3.php autoload file was not loaded' |
| 126 | + ); |
| 127 | + $this->assertContains( |
| 128 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.4.php'), |
| 129 | + $diff, |
| 130 | + 'ProcessRulesetAutoloadLoadAlways.4.php autoload file was not loaded' |
| 131 | + ); |
| 132 | + $this->assertNotContains( |
| 133 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'), |
| 134 | + $diff, |
| 135 | + 'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was loaded, while it shouldn\'t have been' |
| 136 | + ); |
| 137 | + $this->assertContains( |
| 138 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'), |
| 139 | + $diff, |
| 140 | + 'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was not loaded' |
| 141 | + ); |
| 142 | + |
| 143 | + }//end testShouldProcessAutoloadCbfonly() |
| 144 | + |
| 145 | + |
| 146 | + /** |
| 147 | + * Test an exception is thrown when the <autoload> directive points to a file which doesn't exist. |
| 148 | + * |
| 149 | + * @return void |
| 150 | + */ |
| 151 | + public function testFileNotFoundException() |
| 152 | + { |
| 153 | + $exceptionMsg = 'The specified autoload file "./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php" does not exist'; |
| 154 | + $this->expectRuntimeExceptionMessage($exceptionMsg); |
| 155 | + |
| 156 | + // Set up the ruleset. |
| 157 | + $standard = __DIR__.'/ProcessRulesetAutoloadFileNotFoundTest.xml'; |
| 158 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 159 | + new Ruleset($config); |
| 160 | + |
| 161 | + }//end testFileNotFoundException() |
| 162 | + |
| 163 | + |
| 164 | +}//end class |
0 commit comments