diff --git a/src/Prefixer.php b/src/Prefixer.php index a03a95b3..30ed6346 100644 --- a/src/Prefixer.php +++ b/src/Prefixer.php @@ -189,6 +189,7 @@ public function replaceNamespace(string $contents, string $originalNamespace, st |implements\s+ |extends\s+ # when the class being extended is namespaced inline |return\s+ + |instanceof\s+ # when checking the class type of an object in a conditional |\(\s* # inside a function declaration as the first parameters type |,\s* # inside a function declaration as a subsequent parameter type |\.\s* # as part of a concatenated string diff --git a/tests/Issues/StraussIssue83Test.php b/tests/Issues/StraussIssue83Test.php new file mode 100644 index 00000000..c56a9c37 --- /dev/null +++ b/tests/Issues/StraussIssue83Test.php @@ -0,0 +1,63 @@ +testsWorkingDir); + + file_put_contents($this->testsWorkingDir . '/composer.json', $composerJsonString); + + exec('composer install'); + + $inputInterfaceMock = $this->createMock(InputInterface::class); + $outputInterfaceMock = $this->createMock(OutputInterface::class); + + $strauss = new Compose(); + + $result = $strauss->run($inputInterfaceMock, $outputInterfaceMock); + + self::assertEquals(0, $result); + + $php_string = file_get_contents($this->testsWorkingDir . '/vendor-prefixed/aws/aws-sdk-php/src/ClientResolver.php'); + + self::assertStringNotContainsString('$value instanceof \Aws\EndpointV2\EndpointProviderV2', $php_string); + self::assertStringContainsString('$value instanceof \Company\Project\Aws\EndpointV2\EndpointProviderV2', $php_string); + } +} diff --git a/tests/Unit/PrefixerTest.php b/tests/Unit/PrefixerTest.php index 695837ea..7d9c34ca 100644 --- a/tests/Unit/PrefixerTest.php +++ b/tests/Unit/PrefixerTest.php @@ -14,6 +14,7 @@ use Composer\Composer; use Composer\Config; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; /** * Class ReplacerTest @@ -43,7 +44,9 @@ protected function aaasetUp(): void $composer = new Composer(); $composer->setConfig($composerConfig); - $this->config = new StraussConfig($composer); + $input = $this->createMock(InputInterface::class); + + $this->config = new StraussConfig($composer, $input); } public function testNamespaceReplacer() @@ -1784,4 +1787,52 @@ public function get_google_client() { $this->assertEquals($expected, $result); } + + /** + * @see https://github.com/BrianHenryIE/strauss/issues/83 + * @see vendor-prefixed/aws/aws-sdk-php/src/ClientResolver.php:955 + */ + public function testPrefixesFullNamespaceInInstanceOf(): void + { + $contents = <<<'EOD' +isEnabled()) + ) { + + } + } +} +EOD; + + $expected = <<<'EOD' +isEnabled()) + ) { + + } + } +} +EOD; + + $config = $this->createMock(StraussConfig::class); + + $replacer = new Prefixer($config, __DIR__); + + $result = $replacer->replaceNamespace($contents, 'Aws\\EndpointDiscovery', 'Company\\Project\\Aws\\EndpointDiscovery'); + $result = $replacer->replaceNamespace($result, 'Aws', 'Company\\Project\\Aws'); + + $this->assertEquals($expected, $result); + } }