Skip to content

Commit

Permalink
All PSR-4 autoload key to be an array
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Oct 30, 2023
1 parent 09ebec1 commit b32fe49
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Composer/ComposerPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ public function getPackageAbsolutePath(): ?string
/**
*
* e.g. ['psr-4' => [ 'BrianHenryIE\Project' => 'src' ]]
* e.g. ['psr-4' => [ 'BrianHenryIE\Project' => ['src','lib] ]]
*
* @return array<string, array<int|string, string>>
* @return array<string, array<int|string, string|array<string>>>
*/
public function getAutoload(): array
{
Expand Down
13 changes: 8 additions & 5 deletions src/Composer/Extra/StraussConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,14 @@ public function __construct(Composer $composer)
// Check each autoloader.
foreach ($composer->getPackage()->getAutoload() as $autoload) {
// To see if one of its paths.
foreach ($autoload as $path) {
// Matches the target directory.
if (trim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR === $this->getTargetDirectory()) {
$this->classmapOutput = false;
break 2;
foreach ($autoload as $entry) {
$paths = (array) $entry;
foreach ($paths as $path) {
// Matches the target directory.
if (trim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR === $this->getTargetDirectory()) {
$this->classmapOutput = false;
break 3;
}
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions tests/Issues/StraussIssue76Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Test PSR-4 array of autoload values.
*
* @see https://github.com/BrianHenryIE/strauss/issues/76
*/

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 StraussIssue76Test extends \BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase
{

/**
*/
public function test_psr4_array()
{

$composerJsonString = <<<'EOD'
{
"autoload": {
"psr-4": {
"FakerPress\\": [
"src/FakerPress/",
"src/functions/"
],
"FakerPress\\Dev\\": "dev/src/"
}
},
"extra": {
"strauss": {
"target_directory": "vendor-prefixed",
"namespace_prefix": "FakerPress\\ThirdParty\\",
"classmap_prefix": "FakerPress_ThirdParty_",
"constant_prefix": "FAKERPRESS__"
}
}
}
EOD;

chdir($this->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);
}
}
35 changes: 35 additions & 0 deletions tests/Unit/Composer/ComposerPackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace BrianHenryIE\Strauss\Tests\Unit\Composer;

use BrianHenryIE\Strauss\Composer\ComposerPackage;
use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
use Composer\Factory;
use Composer\IO\NullIO;
use PHPUnit\Framework\TestCase;

class ComposerPackageTest extends TestCase
Expand Down Expand Up @@ -126,6 +129,38 @@ public function testAutoloadFiles()
$this->assertIsArray($autoload['files']);
}

public function testPsr4Array()
{

$composerJson = <<<'EOD'
{
"autoload": {
"psr-4": { "Monolog\\": ["src/", "lib/"] }
}
}
EOD;
$tmpfname = tempnam(sys_get_temp_dir(), 'strauss-test-');
file_put_contents($tmpfname, $composerJson);

$composer = Factory::create(new NullIO(), $tmpfname);

$sut = new ComposerPackage($composer);

$autoload = $sut->getAutoload();

$this->assertArrayHasKey('psr-4', $autoload);

$psr4Autoload = $autoload['psr-4'];

$this->assertArrayHasKey('Monolog\\', $psr4Autoload);

$monologAutoload = $psr4Autoload['Monolog\\'];

$this->assertContains('src/', $monologAutoload);
$this->assertContains('lib/', $monologAutoload);
}

public function testOverrideAutoload()
{
$this->markTestIncomplete();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/LicenserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testFindLicenceFilesPathsAreRelative()

$dependency = $this->createStub(ComposerPackage::class);
$dependency->method('getRelativePath')->willReturn('developer-name/project-name/');
$dependency->method('getPackageAbsolutePath')->willReturn(__DIR__.'/vendor/developer-name/project-name/');
$dependencies[] = $dependency;

$sut = new Licenser($config, $workingDir, $dependencies, 'BrianHenryIE');
Expand Down

0 comments on commit b32fe49

Please sign in to comment.