|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Tests\Scoper; |
| 6 | + |
| 7 | +use Webmozart\Assert\Assert; |
| 8 | +use Closure; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Finder\Finder; |
| 11 | + |
| 12 | +final class ScoperPatchersTest extends TestCase |
| 13 | +{ |
| 14 | + private static bool $isFinderAliased = false; |
| 15 | + |
| 16 | + public function testKeepsPrefixedClassesInGetRuleDefinitionUnprefixed(): void |
| 17 | + { |
| 18 | + $scoperConfig = $this->provideScoperConfig(); |
| 19 | + |
| 20 | + $content = <<<'PHP' |
| 21 | +<?php |
| 22 | +
|
| 23 | +namespace Rector\Assert\Rector\ClassMethod; |
| 24 | +
|
| 25 | +use RectorPrefix202607\Webmozart\Assert\Assert; |
| 26 | +
|
| 27 | +final class AddAssertArrayFromClassMethodDocblockRector |
| 28 | +{ |
| 29 | + public function getRuleDefinition(): RuleDefinition |
| 30 | + { |
| 31 | + $metadata = 'RectorPrefix202607\Webmozart\Assert\Assert'; |
| 32 | +
|
| 33 | + new ConfiguredCodeSample( |
| 34 | + <<<'CODE_SAMPLE' |
| 35 | +<?php |
| 36 | +
|
| 37 | +namespace RectorPrefix202607; |
| 38 | +
|
| 39 | +use RectorPrefix202607\Webmozart\Assert\Assert; |
| 40 | +CODE_SAMPLE |
| 41 | + , |
| 42 | + <<<'CODE_SAMPLE' |
| 43 | +<?php |
| 44 | +
|
| 45 | +\RectorPrefix202607\Webmozart\Assert\Assert::allString($items); |
| 46 | +CODE_SAMPLE |
| 47 | + , |
| 48 | + [] |
| 49 | + ); |
| 50 | +
|
| 51 | + new CodeSample( |
| 52 | + 'RectorPrefix202607\SomeVendor\ValueObject::class', |
| 53 | + 'new RectorPrefix202607\SomeVendor\ValueObject()' |
| 54 | + ); |
| 55 | + } |
| 56 | +
|
| 57 | + public function refactor(): void |
| 58 | + { |
| 59 | + \RectorPrefix202607\SomeVendor\Runtime::class; |
| 60 | + } |
| 61 | +} |
| 62 | +PHP; |
| 63 | + |
| 64 | + foreach ($scoperConfig['patchers'] as $patcher) { |
| 65 | + $content = $patcher->__invoke( |
| 66 | + __DIR__ . '/../../rules/Assert/Rector/ClassMethod/AddAssertArrayFromClassMethodDocblockRector.php', |
| 67 | + 'RectorPrefix202607', |
| 68 | + $content |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + $this->assertStringContainsString('use Webmozart\Assert\Assert;', (string) $content); |
| 73 | + $this->assertStringContainsString('use RectorPrefix202607\Webmozart\Assert\Assert;', (string) $content); |
| 74 | + $this->assertStringContainsString(Assert::class . '::allString($items);', (string) $content); |
| 75 | + $this->assertStringContainsString("'SomeVendor\ValueObject::class'", (string) $content); |
| 76 | + $this->assertStringContainsString("'new SomeVendor\ValueObject()'", (string) $content); |
| 77 | + $this->assertStringContainsString('$metadata = \'Webmozart\Assert\Assert\';', (string) $content); |
| 78 | + $this->assertStringContainsString('\RectorPrefix202607\SomeVendor\Runtime::class;', (string) $content); |
| 79 | + $this->assertStringNotContainsString('namespace RectorPrefix202607;', (string) $content); |
| 80 | + } |
| 81 | + |
| 82 | + public function testRemovesPrefixedNamespaceInGetRuleDefinitionWithWindowsLineEndings(): void |
| 83 | + { |
| 84 | + $scoperConfig = $this->provideScoperConfig(); |
| 85 | + |
| 86 | + $content = str_replace( |
| 87 | + "\n", |
| 88 | + "\r\n", |
| 89 | + <<<'PHP' |
| 90 | +<?php |
| 91 | +
|
| 92 | +final class SomeRector |
| 93 | +{ |
| 94 | + public function getRuleDefinition(): RuleDefinition |
| 95 | + { |
| 96 | + return new CodeSample( |
| 97 | + '<?php |
| 98 | +namespace RectorPrefix202607; |
| 99 | +
|
| 100 | +RectorPrefix202607\SomeVendor\ValueObject::class;' |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
| 104 | +PHP |
| 105 | + ); |
| 106 | + |
| 107 | + foreach ($scoperConfig['patchers'] as $patcher) { |
| 108 | + $content = $patcher->__invoke( |
| 109 | + __DIR__ . '/../../rules/Some/Rector/SomeRector.php', |
| 110 | + 'RectorPrefix202607', |
| 111 | + $content |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + $this->assertStringNotContainsString('namespace RectorPrefix202607;', (string) $content); |
| 116 | + $this->assertStringContainsString('SomeVendor\ValueObject::class;', (string) $content); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return array{patchers: list<Closure(string, string, string): string>} |
| 121 | + */ |
| 122 | + private function provideScoperConfig(): array |
| 123 | + { |
| 124 | + if (! self::$isFinderAliased) { |
| 125 | + class_alias(Finder::class, 'Isolated\Symfony\Component\Finder\Finder'); |
| 126 | + self::$isFinderAliased = true; |
| 127 | + } |
| 128 | + |
| 129 | + return require __DIR__ . '/../../scoper.php'; |
| 130 | + } |
| 131 | +} |
0 commit comments