|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test handling of <autoload> instructions. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 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.php'), |
| 56 | + $diff, |
| 57 | + 'ProcessRulesetAutoloadLoadAlways.php autoload file was not loaded' |
| 58 | + ); |
| 59 | + $this->assertContains( |
| 60 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'), |
| 61 | + $diff, |
| 62 | + 'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was not loaded' |
| 63 | + ); |
| 64 | + $this->assertNotContains( |
| 65 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'), |
| 66 | + $diff, |
| 67 | + 'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was loaded, while it shouldn\'t have been' |
| 68 | + ); |
| 69 | + |
| 70 | + }//end testShouldProcessAutoloadCsonly() |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * Verify that in CBF mode, phpcbf-only <autoload> directives are respected and phpcs-only <autoload> |
| 75 | + * directives are ignored. |
| 76 | + * |
| 77 | + * @group CBF |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function testShouldProcessAutoloadCbfonly() |
| 82 | + { |
| 83 | + if (PHP_CODESNIFFER_CBF === false) { |
| 84 | + $this->markTestSkipped('This test needs CBF mode to run'); |
| 85 | + } |
| 86 | + |
| 87 | + $originallyIncludes = get_included_files(); |
| 88 | + |
| 89 | + // Set up the ruleset. |
| 90 | + $standard = __DIR__.'/ProcessRulesetAutoloadTest.xml'; |
| 91 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 92 | + new Ruleset($config); |
| 93 | + |
| 94 | + $finalIncludes = get_included_files(); |
| 95 | + $diff = array_diff($finalIncludes, $originallyIncludes); |
| 96 | + |
| 97 | + $this->assertContains( |
| 98 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.php'), |
| 99 | + $diff, |
| 100 | + 'ProcessRulesetAutoloadLoadAlways.php autoload file was not loaded' |
| 101 | + ); |
| 102 | + $this->assertNotContains( |
| 103 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'), |
| 104 | + $diff, |
| 105 | + 'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was loaded, while it shouldn\'t have been' |
| 106 | + ); |
| 107 | + $this->assertContains( |
| 108 | + __DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'), |
| 109 | + $diff, |
| 110 | + 'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was not loaded' |
| 111 | + ); |
| 112 | + |
| 113 | + }//end testShouldProcessAutoloadCbfonly() |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * Test an exception is thrown when the <autoload> directive points to a file which doesn't exist. |
| 118 | + * |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + public function testFileNotFoundException() |
| 122 | + { |
| 123 | + $exceptionMsg = 'The specified autoload file "./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php" does not exist'; |
| 124 | + $this->expectRuntimeExceptionMessage($exceptionMsg); |
| 125 | + |
| 126 | + // Set up the ruleset. |
| 127 | + $standard = __DIR__.'/ProcessRulesetAutoloadFileNotFoundTest.xml'; |
| 128 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 129 | + new Ruleset($config); |
| 130 | + |
| 131 | + }//end testFileNotFoundException() |
| 132 | + |
| 133 | + |
| 134 | +}//end class |
0 commit comments