Skip to content

Commit

Permalink
Fix #27
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Aug 2, 2021
1 parent 03a148c commit 9a93339
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Prefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
67 changes: 67 additions & 0 deletions tests/Issues/StraussIssue27Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Problem with too many replacements due to common class, domain, namespace names, "Normalizer".
*
* @see https://github.com/BrianHenryIE/strauss/issues/27
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

use BrianHenryIE\Strauss\Console\Commands\Compose;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @package BrianHenryIE\Strauss\Tests\Issues
* @coversNothing
*/
class StraussIssue27Test extends \BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase
{

/**
*/
public function test_virtual_package()
{

$composerJsonString = <<<'EOD'
{
"require": {
"symfony/polyfill-intl-normalizer": "1.23"
},
"extra": {
"strauss": {
"namespace_prefix": "Normalizer_Test\\",
"classmap_prefix": "Normalizer_Test_"
}
}
}
EOD;

file_put_contents($this->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);
}
}
40 changes: 37 additions & 3 deletions tests/Unit/PrefixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 9a93339

Please sign in to comment.