Skip to content

Commit

Permalink
Fix: incorrect order of str_starts_with()
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Feb 15, 2024
1 parent fad97d4 commit ccac6af
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ChangeEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getDiscoveredNamespaces(?string $namespacePrefix = ''): array
// When running subsequent times, try to discover the original namespaces.
// This is naive: it will not work where namespace replacement patterns have been used.
foreach ($this->discoveredNamespaces as $key => $value) {
$unprefixed = str_starts_with($this->namespacePrefix, $key)
$unprefixed = str_starts_with($key, $this->namespacePrefix)
? ltrim(substr($key, strlen($this->namespacePrefix)), '\\')
: $key;
$discoveredNamespaceReplacements[ $unprefixed ] = $value;
Expand Down
81 changes: 81 additions & 0 deletions tests/Issues/StraussIssue34Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Don't double prefix when updating project code on repeated runs.
*
* @see https://github.com/BrianHenryIE/strauss/issues/34
*/

namespace BrianHenryIE\Strauss\Tests\Issues;

use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
use BrianHenryIE\Strauss\Console\Commands\Compose;
use BrianHenryIE\Strauss\Prefixer;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

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

public function test_no_double_prefix_after_second_run()
{
$composerJsonString = <<<'EOD'
{
"name": "brianhenryie/strauss-34",
"minimum-stability": "dev",
"autoload": {
"classmap": [
"src/"
]
},
"require": {
"psr/log": "*"
},
"extra": {
"strauss": {
"namespace_prefix": "BrianHenryIE\\Strauss\\",
"classmap_prefix": "BH_Strauss_",
"target_directory": "vendor",
"update_call_sites": true
}
}
}
EOD;
$phpFileJsonString = <<<'EOD'
<?php
namespace My_Namespace\My_Project;
use Psr\Log\LoggerInterface;
EOD;

file_put_contents($this->testsWorkingDir . 'composer.json', $composerJsonString);
@mkdir($this->testsWorkingDir . 'src');
file_put_contents($this->testsWorkingDir . 'src/library.php', $phpFileJsonString);

chdir($this->testsWorkingDir);

exec('composer install');

$inputInterfaceMock = $this->createMock(InputInterface::class);
$outputInterfaceMock = $this->createMock(OutputInterface::class);

$strauss = new Compose();

// Run TWICE!
$strauss->run($inputInterfaceMock, $outputInterfaceMock);
$result = $strauss->run($inputInterfaceMock, $outputInterfaceMock);
$this->assertNotEquals(1, $result);

$project_file_php_string = file_get_contents($this->testsWorkingDir . 'src/library.php');
self::assertStringNotContainsString('use Psr\Log\LoggerInterface', $project_file_php_string);
self::assertStringContainsString('use BrianHenryIE\Strauss\Psr\Log\LoggerInterface', $project_file_php_string);

$project_file_php_string = file_get_contents($this->testsWorkingDir . 'vendor/psr/log/src/LoggerInterface.php');
self::assertStringNotContainsString('namespace Psr\Log;', $project_file_php_string);
self::assertStringContainsString('namespace BrianHenryIE\Strauss\Psr\Log;', $project_file_php_string);
}
}

0 comments on commit ccac6af

Please sign in to comment.