|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Tests\Bootstrap; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Rector\Bootstrap\AutoloadFileParameterResolver; |
| 10 | +use Rector\Caching\Config\FileHashComputer; |
| 11 | +use Rector\Configuration\Option; |
| 12 | +use Rector\Configuration\Parameter\SimpleParameterProvider; |
| 13 | + |
| 14 | +final class AutoloadFileParameterResolverTest extends TestCase |
| 15 | +{ |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + SimpleParameterProvider::setParameter(Option::AUTOLOAD_FILE, ''); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * all spellings must resolve to the same value, or the main process and |
| 23 | + * parallel workers, which receive the space-separated form, would compute |
| 24 | + * different configuration hashes |
| 25 | + * |
| 26 | + * @param array<int, string> $argv |
| 27 | + */ |
| 28 | + #[DataProvider('provideArgvSpellings')] |
| 29 | + public function testEverySpellingResolvesToSameRealPath(array $argv): void |
| 30 | + { |
| 31 | + AutoloadFileParameterResolver::resolveFromArgv($argv); |
| 32 | + |
| 33 | + $this->assertSame( |
| 34 | + (string) realpath(__FILE__), |
| 35 | + SimpleParameterProvider::provideStringParameter(Option::AUTOLOAD_FILE) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return iterable<string, array{array<int, string>}> |
| 41 | + */ |
| 42 | + public static function provideArgvSpellings(): iterable |
| 43 | + { |
| 44 | + $relativePath = 'tests/Bootstrap/' . basename(__FILE__); |
| 45 | + |
| 46 | + yield 'long with space' => [['bin/rector', 'process', '--autoload-file', __FILE__]]; |
| 47 | + yield 'long with equals' => [['bin/rector', 'process', '--autoload-file=' . __FILE__]]; |
| 48 | + yield 'short with space' => [['bin/rector', 'process', '-a', __FILE__]]; |
| 49 | + yield 'relative path is normalized' => [['bin/rector', 'process', '-a', $relativePath]]; |
| 50 | + } |
| 51 | + |
| 52 | + public function testWithoutOptionParameterStaysUntouched(): void |
| 53 | + { |
| 54 | + AutoloadFileParameterResolver::resolveFromArgv(['bin/rector', 'process', '--dry-run']); |
| 55 | + |
| 56 | + $this->assertSame('', SimpleParameterProvider::provideStringParameter(Option::AUTOLOAD_FILE)); |
| 57 | + } |
| 58 | + |
| 59 | + public function testResolvedAutoloadFileChangesConfigurationHash(): void |
| 60 | + { |
| 61 | + $fileHashComputer = new FileHashComputer(); |
| 62 | + $configFilePath = __DIR__ . '/config/some_config.php'; |
| 63 | + |
| 64 | + $hashWithout = $fileHashComputer->compute($configFilePath); |
| 65 | + |
| 66 | + AutoloadFileParameterResolver::resolveFromArgv(['bin/rector', 'process', '-a', __FILE__]); |
| 67 | + |
| 68 | + $this->assertNotSame($hashWithout, $fileHashComputer->compute($configFilePath)); |
| 69 | + } |
| 70 | +} |
0 commit comments