|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test the Ruleset::expandRulesetReference() method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2025 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 home path handling in the Ruleset::expandRulesetReference() method. |
| 18 | + * |
| 19 | + * @covers \PHP_CodeSniffer\Ruleset::expandRulesetReference |
| 20 | + */ |
| 21 | +final class ExpandRulesetReferenceHomePathTest extends AbstractRulesetTestCase |
| 22 | +{ |
| 23 | + |
| 24 | + /** |
| 25 | + * Original value of the user's home path environment variable. |
| 26 | + * |
| 27 | + * @var string|false Path or false is the `HOME` environment variable is not available. |
| 28 | + */ |
| 29 | + private static $homepath = false; |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * Store the user's home path. |
| 34 | + * |
| 35 | + * @beforeClass |
| 36 | + * |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + protected function storeHomePath() |
| 40 | + { |
| 41 | + $this->homepath = getenv('HOME'); |
| 42 | + |
| 43 | + }//end storeHomePath() |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Restore the user's home path environment variable in case the test changed it or created it. |
| 48 | + * |
| 49 | + * @afterClass |
| 50 | + * |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + protected function restoreHomePath() |
| 54 | + { |
| 55 | + if (is_string($this->homepath) === true) { |
| 56 | + putenv('HOME='.$this->homepath); |
| 57 | + } else { |
| 58 | + // Remove the environment variable as it didn't exist before. |
| 59 | + putenv('HOME'); |
| 60 | + } |
| 61 | + |
| 62 | + }//end restoreHomePath() |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * Set the home path to an alternative location. |
| 67 | + * |
| 68 | + * @before |
| 69 | + * |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + protected function setHomePath() |
| 73 | + { |
| 74 | + $fakeHomePath = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FakeHomePath'; |
| 75 | + putenv("HOME=$fakeHomePath"); |
| 76 | + |
| 77 | + }//end setHomePath() |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Verify that a sniff reference with the magic "home path" placeholder gets expanded correctly |
| 82 | + * and finds sniffs if the path exists underneath the "home path". |
| 83 | + * |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testHomePathRefGetsExpandedAndFindsSniff() |
| 87 | + { |
| 88 | + // Set up the ruleset. |
| 89 | + $standard = __DIR__.'/ExpandRulesetReferenceHomePathTest.xml'; |
| 90 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 91 | + $ruleset = new Ruleset($config); |
| 92 | + |
| 93 | + $expected = ['MyStandard.Category.Valid' => 'FakeHomePath\\MyStandard\\Sniffs\\Category\\ValidSniff']; |
| 94 | + |
| 95 | + $this->assertSame($expected, $ruleset->sniffCodes); |
| 96 | + |
| 97 | + }//end testHomePathRefGetsExpandedAndFindsSniff() |
| 98 | + |
| 99 | + |
| 100 | + /** |
| 101 | + * Verify that a sniff reference with the magic "home path" placeholder gets expanded correctly |
| 102 | + * and still fails to find sniffs if the path doesn't exists underneath the "home path". |
| 103 | + * |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + public function testHomePathRefGetsExpandedAndThrowsExceptionWhenPathIsInvalid() |
| 107 | + { |
| 108 | + // Set up the ruleset. |
| 109 | + $standard = __DIR__.'/ExpandRulesetReferenceHomePathFailTest.xml'; |
| 110 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 111 | + |
| 112 | + $exceptionMessage = 'Referenced sniff "~/src/MyStandard/Sniffs/DoesntExist/" does not exist'; |
| 113 | + $this->expectRuntimeExceptionMessage($exceptionMessage); |
| 114 | + |
| 115 | + new Ruleset($config); |
| 116 | + |
| 117 | + }//end testHomePathRefGetsExpandedAndThrowsExceptionWhenPathIsInvalid() |
| 118 | + |
| 119 | + |
| 120 | +}//end class |
0 commit comments