From 9a933395029d21c0599fd34758c9b73ef22f1aaa Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 1 Aug 2021 17:10:47 -0700 Subject: [PATCH] Fix #27 --- src/Prefixer.php | 2 +- tests/Issues/StraussIssue27Test.php | 67 +++++++++++++++++++++++++++++ tests/Unit/PrefixerTest.php | 40 +++++++++++++++-- 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 tests/Issues/StraussIssue27Test.php diff --git a/src/Prefixer.php b/src/Prefixer.php index 90c6007e..f2aeb378 100644 --- a/src/Prefixer.php +++ b/src/Prefixer.php @@ -219,7 +219,7 @@ public function replaceClassname($contents, $originalClassname, $classnamePrefix | ([^a-zA-Z0-9_\x7f-\xff\$\\\])('. $searchClassname . ')([^a-zA-Z0-9_\x7f-\xff\\\]) - /x'; // # x: ignore whitespace in regex. + /xs'; // # x: ignore whitespace in regex. s dot matches newline $replacingFunction = function ($matches) use ($originalClassname, $classnamePrefix) { diff --git a/tests/Issues/StraussIssue27Test.php b/tests/Issues/StraussIssue27Test.php new file mode 100644 index 00000000..d797c84c --- /dev/null +++ b/tests/Issues/StraussIssue27Test.php @@ -0,0 +1,67 @@ +testsWorkingDir . 'composer.json', $composerJsonString); + + chdir($this->testsWorkingDir); + + exec('composer install'); + + $inputInterfaceMock = $this->createMock(InputInterface::class); + $outputInterfaceMock = $this->createMock(OutputInterface::class); + + $strauss = new Compose(); + + $result = $strauss->run($inputInterfaceMock, $outputInterfaceMock); + + $php_string = file_get_contents($this->testsWorkingDir . 'strauss/symfony/polyfill-intl-normalizer/Normalizer.php'); + + $this->assertStringNotContainsString('namespace Normalizer_Test\Symfony\Polyfill\Intl\Normalizer_Test_Normalizer;', $php_string); + $this->assertStringContainsString('namespace Normalizer_Test\Symfony\Polyfill\Intl\Normalizer;', $php_string); + + $this->assertStringNotContainsString('class Normalizer_Test_Normalizer', $php_string); + $this->assertStringContainsString('class Normalizer', $php_string); + + + $php_string = file_get_contents($this->testsWorkingDir . 'strauss/symfony/polyfill-intl-normalizer/Resources/stubs/Normalizer.php'); + + $this->assertStringNotContainsString('class Normalizer_Test_Normalizer extends Normalizer_Test\Symfony\Polyfill\Intl\Normalizer_Test_Normalizer\Normalizer', $php_string); + $this->assertStringContainsString('class Normalizer_Test_Normalizer extends Normalizer_Test\Symfony\Polyfill\Intl\Normalizer\Normalizer', $php_string); + } +} diff --git a/tests/Unit/PrefixerTest.php b/tests/Unit/PrefixerTest.php index 22a9b6d8..316832d2 100644 --- a/tests/Unit/PrefixerTest.php +++ b/tests/Unit/PrefixerTest.php @@ -1287,7 +1287,7 @@ public function __construct() } /** - * A prefixed classname was being replaced inside a namespace. + * A prefixed classname was being replaced inside a namespace name. * * namespace Symfony\Polyfill\Intl\Normalizer_Test_Normalizer; * @@ -1352,8 +1352,6 @@ class Normalizer_Test_Normalizer extends Symfony\Polyfill\Intl\Normalizer\Foo $this->assertEquals($expected, $result); } - - /** * class Normalizer_Test_Normalizer extends Normalizer_Test\Symfony\Polyfill\Intl\Normalizer_Test_Normalizer\Normalizer * @@ -1387,4 +1385,40 @@ class Normalizer_Test_Normalizer extends Symfony\Polyfill\Intl\Foo\Normalizer $this->assertEquals($expected, $result); } + + + + /** + * + * + * @throws \Exception + */ + public function testItDoesNotPrefixClassDeclarationInsideNamespace(): void + { + + $contents = <<<'EOD' +namespace Symfony\Polyfill\Intl\Normalizer; + +class Normalizer +{ +EOD; + + $expected = <<<'EOD' +namespace Symfony\Polyfill\Intl\Normalizer; + +class Normalizer +{ +EOD; + + $originalClassname = 'Normalizer'; + $classnamePrefix = 'Normalizer_Test_'; + + $config = $this->createMock(StraussConfig::class); + + $replacer = new Prefixer($config, __DIR__); + + $result = $replacer->replaceClassname($contents, $originalClassname, $classnamePrefix); + + $this->assertEquals($expected, $result); + } }