Skip to content

Commit

Permalink
Fix 108
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Oct 25, 2024
1 parent ef17e02 commit d041941
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/FileEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ protected function addFile(ComposerPackage $dependency, string $packageRelativeP
$projectRelativePath = $this->vendorDir . $outputRelativeFilepath;
$isOutsideProjectDir = 0 !== strpos($sourceAbsoluteFilepath, $this->workingDir);

$f = $this->discoveredFiles[$outputRelativeFilepath]
?? new File($dependency, $packageRelativePath, $sourceAbsoluteFilepath);
$f = $this->discoveredFiles->getFiles()[$outputRelativeFilepath]
?? new File($dependency, $packageRelativePath, $sourceAbsoluteFilepath);

$f->addAutoloader($autoloaderType);
$f->setDoDelete($isOutsideProjectDir);
Expand Down
78 changes: 78 additions & 0 deletions tests/Issues/StraussIssue108Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* `use GlobalClass as Alias;` should be replaced with `use Prefixed_GlobalClass as Alias;`.
*
*
*
* @see https://github.com/BrianHenryIE/strauss/issues/108
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

use BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase;

/**
* @package BrianHenryIE\Strauss\Tests\Issues
* @coversNothing
*/
class StraussIssue108Test extends IntegrationTestCase
{
public function test_correct_directory_permission()
{
$composerJsonString = <<<'EOD'
{
"name": "strauss/issue108",
"require": {
"erusev/parsedown": "1.7.4"
},
"autoload": {
"classmap": [
"src/"
]
},
"extra": {
"strauss": {
"namespace_prefix": "Strauss\\Issue108\\",
"classmap_prefix": "Prefixed_",
"override_autoload": {
"erusev/parsedown": {
"classmap": [
"."
]
}
}
}
}
}
EOD;

$replacementfile = <<<'EOD'
<?php
use Parsedown as MarkdownParser;
class MyClass {
public function myFunction() {
$parsedown = new MarkdownParser();
}
}
EOD;

chdir($this->testsWorkingDir);

file_put_contents($this->testsWorkingDir . '/composer.json', $composerJsonString);

@mkdir($this->testsWorkingDir . 'src');
$replacementfilePath = $this->testsWorkingDir . '/src/file.php';
file_put_contents($replacementfilePath, $replacementfile);

exec('composer install');

$this->runStrauss();

$php_string = file_get_contents($replacementfilePath);

self::assertStringNotContainsString("use Parsedown as MarkdownParser;", $php_string);
self::assertStringContainsString("use Prefixed_Parsedown as MarkdownParser;", $php_string);
}
}
28 changes: 28 additions & 0 deletions tests/Unit/PrefixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1939,4 +1939,32 @@ public static function ini(

self::assertEqualsRN($expected, $result);
}
public function testPrefixesAliasedGlobalClass(): void
{
$contents = <<<'EOD'
<?php
use GlobalClass as Alias;
class MyClass {
}
EOD;
$expected = <<<'EOD'
<?php
use Prefixed_GlobalClass as Alias;
class MyClass {
}
EOD;

$config = $this->createMock(StraussConfig::class);

$replacer = new Prefixer($config, __DIR__);
$result = $replacer->replaceClassname($contents, 'GlobalClass', 'Prefixed_');

$this->assertEqualsRN($expected, $result);
}
}

0 comments on commit d041941

Please sign in to comment.