Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Rule/Rules/Composer/Psr4DirectoryExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

public function evaluateProject(string $basePath, Architecture $architecture, array $skipPaths = []): ?RuleViolation
{
$composerFile = rtrim($basePath, '/') . '/composer.json';
$composerFile = Path::normalise(rtrim($basePath, '/') . '/composer.json', canonicalise: true);

if (! file_exists($composerFile)) {
return $this->violation(
Expand Down
10 changes: 3 additions & 7 deletions src/Rule/Rules/Composer/Psr4EmptyNamespacePrefixRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Boundwize\StructArmed\Rule\ComposerJsonRuleInterface;
use Boundwize\StructArmed\Rule\MultipleProjectRuleViolationInterface;
use Boundwize\StructArmed\Rule\RuleViolation;
use Boundwize\StructArmed\Util\Path;

use function array_keys;
use function file_exists;
use function is_array;
use function is_string;
use function rtrim;
Expand All @@ -38,18 +38,14 @@ public function evaluateProject(string $basePath, Architecture $architecture, ar
*/
public function evaluateProjectAll(string $basePath, Architecture $architecture, array $skipPaths = []): array
{
$composerFile = rtrim($basePath, '/') . '/composer.json';

if (! file_exists($composerFile)) {
return [];
}

$composer = $this->psr4PathResolver->composerConfig($basePath);

if ($composer === null) {
return [];
}

$composerFile = Path::normalise(rtrim($basePath, '/') . '/composer.json', canonicalise: true);

$violations = [];

foreach (['autoload', 'autoload-dev'] as $section) {
Expand Down
9 changes: 2 additions & 7 deletions src/Rule/Rules/Composer/Psr4RootPathRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Boundwize\StructArmed\Rule\RuleViolation;
use Boundwize\StructArmed\Util\Path;

use function file_exists;
use function is_array;
use function is_string;
use function rtrim;
Expand All @@ -36,18 +35,14 @@ public function evaluateProject(string $basePath, Architecture $architecture, ar
*/
public function evaluateProjectAll(string $basePath, Architecture $architecture, array $skipPaths = []): array
{
$composerFile = rtrim($basePath, '/') . '/composer.json';

if (! file_exists($composerFile)) {
return [];
}

$composer = $this->psr4PathResolver->composerConfig($basePath);

if ($composer === null) {
return [];
}

$composerFile = Path::normalise(rtrim($basePath, '/') . '/composer.json', canonicalise: true);

$violations = [];
$normalisedBasePath = Path::normalise($basePath, canonicalise: true);

Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Rules/Composer/Psr4SourcePathsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(

public function evaluateProject(string $basePath, Architecture $architecture, array $skipPaths = []): ?RuleViolation
{
$composerFile = rtrim($basePath, '/') . '/composer.json';
$composerFile = Path::normalise(rtrim($basePath, '/') . '/composer.json', canonicalise: true);

if (! file_exists($composerFile)) {
return $this->violation(
Expand Down
78 changes: 78 additions & 0 deletions tests/Rule/Composer/Psr4ComposerFilePathCanonicalisationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Boundwize\StructArmed\Tests\Rule\Composer;

use Boundwize\StructArmed\Architecture;
use Boundwize\StructArmed\Rule\Rules\Composer\Psr4EmptyNamespacePrefixRule;
use Boundwize\StructArmed\Rule\Rules\Composer\Psr4RootPathRule;
use Boundwize\StructArmed\Tests\Support\TemporaryDirectoryCleanupTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

use function file_put_contents;
use function realpath;
use function str_replace;

#[CoversClass(Psr4EmptyNamespacePrefixRule::class)]
#[CoversClass(Psr4RootPathRule::class)]
final class Psr4ComposerFilePathCanonicalisationTest extends TestCase
{
use TemporaryDirectoryCleanupTrait;

public function testEmptyNamespacePrefixRuleReportsCanonicalisedComposerFilePath(): void
{
$basePath = $this->makeTempProject(<<<'JSON'
{
"autoload": {
"psr-4": {
"": "src/"
}
}
}
JSON);

$violations = (new Psr4EmptyNamespacePrefixRule())->evaluateProjectAll(
$basePath . '/.',
Architecture::define()
);

$this->assertCount(1, $violations);
$this->assertSame($this->expectedComposerFilePath($basePath), $violations[0]->file);
}

public function testRootPathRuleReportsCanonicalisedComposerFilePath(): void
{
$basePath = $this->makeTempProject(<<<'JSON'
{
"autoload": {
"psr-4": {
"App\\": "."
}
}
}
JSON);

$violations = (new Psr4RootPathRule())->evaluateProjectAll(
$basePath . '/.',
Architecture::define()
);

$this->assertCount(1, $violations);
$this->assertSame($this->expectedComposerFilePath($basePath), $violations[0]->file);
}

private function expectedComposerFilePath(string $basePath): string
{
return str_replace('\\', '/', (string) realpath($basePath)) . '/composer.json';
}

private function makeTempProject(string $composerJson): string
{
$basePath = $this->makeTemporaryDirectory('structarmed-psr4-composer-file-path');
file_put_contents($basePath . '/composer.json', $composerJson);

return $basePath;
}
}
41 changes: 41 additions & 0 deletions tests/Rule/Composer/Psr4ComposerFilePathNormalisationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Boundwize\StructArmed\Tests\Rule\Composer;

use Boundwize\StructArmed\Architecture;
use Boundwize\StructArmed\Rule\Rules\Composer\Psr4DirectoryExistsRule;
use Boundwize\StructArmed\Rule\Rules\Composer\Psr4SourcePathsRule;
use Boundwize\StructArmed\Rule\RuleViolation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Psr4DirectoryExistsRule::class)]
#[CoversClass(Psr4SourcePathsRule::class)]
final class Psr4ComposerFilePathNormalisationTest extends TestCase
{
private const WINDOWS_STYLE_MISSING_BASE_PATH = 'C:\structarmed-missing-fixture\app';

public function testDirectoryExistsRuleReportsForwardSlashesForWindowsStyleBasePath(): void
{
$violation = (new Psr4DirectoryExistsRule())->evaluateProject(
self::WINDOWS_STYLE_MISSING_BASE_PATH,
Architecture::define()
);

$this->assertInstanceOf(RuleViolation::class, $violation);
$this->assertSame('C:/structarmed-missing-fixture/app/composer.json', $violation->file);
}

public function testSourcePathsRuleReportsForwardSlashesForWindowsStyleBasePath(): void
{
$violation = (new Psr4SourcePathsRule(null))->evaluateProject(
self::WINDOWS_STYLE_MISSING_BASE_PATH,
Architecture::define()
);

$this->assertInstanceOf(RuleViolation::class, $violation);
$this->assertSame('C:/structarmed-missing-fixture/app/composer.json', $violation->file);
}
}
Loading