Skip to content

Commit

Permalink
Merge pull request #12 from naxvog/wildcard-ns
Browse files Browse the repository at this point in the history
Allow usage of namespace selection using wildcards
  • Loading branch information
nunomaduro authored Sep 29, 2024
2 parents 0a27e55 + f2a135d commit be0551f
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 7 deletions.
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
Expand Down
4 changes: 3 additions & 1 deletion src/Factories/LayerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function make(LayerOptions $options, string $name, bool $onlyUserDefinedU
return $object;
}, $this->objectsStorage->allByNamespace($name, $onlyUserDefinedUses));

$layer = Layer::fromBase($objects)->leaveByNameStart($name);
$layer = str_contains($name, '*')
? Layer::fromBase($objects)->leaveByNameRegex('/'.strtr(preg_quote($name, '/'), ['\\*' => '[^\\\\]+']).'/')
: Layer::fromBase($objects)->leaveByNameStart($name);

foreach ($options->exclude as $exclude) {
$layer = $layer->excludeByNameStart($exclude);
Expand Down
4 changes: 2 additions & 2 deletions src/Layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

use IteratorAggregate;
use PHPUnit\Architecture\Elements\Layer\Layer as BaseLayer;
use PHPUnit\Architecture\Elements\Layer\LayerLeave;
use PHPUnit\Architecture\Elements\ObjectDescription;
use Traversable;

/**
* @method Layer assertDoesNotDependOn(string ...$objects)
* @method Layer excludeByNameStart(string $name)
* @method Layer exclude(callable $callback)
* @method Layer leaveByNameStart(string $name)
* @mixin LayerLeave
*
* @implements IteratorAggregate<int, ObjectDescription>
*/
Expand Down
8 changes: 5 additions & 3 deletions src/Repositories/ObjectsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function allByNamespace(string $namespace, bool $onlyUserDefinedUses = tr

$objectsPerPrefix = array_values(array_filter(array_reduce($directories, fn (array $files, string $fileOrDirectory): array => array_merge($files, array_values(array_map(
static fn (SplFileInfo $file): ?ObjectDescription => ObjectDescriptionFactory::make($file->getPathname(), $onlyUserDefinedUses),
is_dir($fileOrDirectory) ? iterator_to_array(Finder::create()->files()->in($fileOrDirectory)->name('*.php')) : [new SplFileInfo($fileOrDirectory)],
is_dir($fileOrDirectory) || str_contains($fileOrDirectory, '*') ? iterator_to_array(Finder::create()->files()->in($fileOrDirectory)->name('*.php')) : [new SplFileInfo($fileOrDirectory)],
))), [])));

$objects = [...$objects, ...$this->cachedObjectsPerPrefix[$prefix][(int) $onlyUserDefinedUses] = $objectsPerPrefix];
Expand Down Expand Up @@ -151,13 +151,15 @@ private function directoriesByNamespace(string $name): array

$directoriesByNamespace[$name] = [...$directoriesByNamespace[$name] ?? [], ...array_values(array_filter(array_map(static function (string $directory) use ($prefix): string {
$fileOrDirectory = $directory.DIRECTORY_SEPARATOR.$prefix;

if (is_dir($fileOrDirectory)) {
return $fileOrDirectory;
}
if (str_contains($fileOrDirectory, '*')) {
return $fileOrDirectory;
}

return $fileOrDirectory.'.php';
}, $directories), static fn (string $fileOrDirectory): bool => is_dir($fileOrDirectory) || file_exists($fileOrDirectory)))];
}, $directories), static fn (string $fileOrDirectory): bool => is_dir($fileOrDirectory) || str_contains($fileOrDirectory, '*') || file_exists($fileOrDirectory)))];
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/Domains/A/Contracts/Models/Bazable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\A\Contracts\Models;

interface Bazable
{
// ...
}
12 changes: 12 additions & 0 deletions tests/Fixtures/Domains/A/Models/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\A\Models;

use Tests\Fixtures\Domains\A\Contracts\Models\Bazable;

final class Article implements Bazable
{
// ...
}
10 changes: 10 additions & 0 deletions tests/Fixtures/Domains/B/Contracts/Models/Bazable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\B\Contracts\Models;

interface Bazable
{
// ...
}
12 changes: 12 additions & 0 deletions tests/Fixtures/Domains/B/Models/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\B\Models;

use Tests\Fixtures\Domains\B\Contracts\Models\Bazable;

final class Article implements Bazable
{
// ...
}
22 changes: 22 additions & 0 deletions tests/Layer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Tests\Fixtures\Domains\A\Contracts\Models\Bazable as BazableDomainA;
use Tests\Fixtures\Domains\A\Models\Article as ArticleDomainA;
use Tests\Fixtures\Domains\B\Contracts\Models\Bazable as BazableDomainB;
use Tests\Fixtures\Domains\B\Models\Article as ArticleDomainB;
use Tests\Fixtures\Enums\Color;
use Tests\Fixtures\Enums\ColorThatDependsOnColor;
use Tests\Fixtures\Misc\DependOnGlobalFunctions;
Expand All @@ -23,6 +27,24 @@
->toOnlyUse('Pest\Support\Str');
});

it('loads namespaces', function () {
expect('Tests\Fixtures\Domains\A\Models')
->getTargets()
->toBe([ArticleDomainA::class]);
});

it('loads namespaces using wildcards', function () {
expect('Tests\Fixtures\Domains\*\Models')
->getTargets()
->toBe([ArticleDomainA::class, ArticleDomainB::class]);
});

it('loads namespaces using multiple wildcards', function () {
expect('Tests\Fixtures\Domains\*\*\Models')
->getTargets()
->toBe([BazableDomainA::class, BazableDomainB::class]);
});

it('does support enums', function () {
expect(Color::class)->toUseNothing()
->and(ColorThatDependsOnColor::class)->toUse([Color::class]);
Expand Down
19 changes: 19 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Pest\Arch\Exceptions\ArchExpectationFailedException;
use Pest\Arch\Expectations\Targeted;
use Pest\Arch\Objects\ObjectDescription;

uses()->beforeEach(function () {
$this->arch()->ignore([
Expand All @@ -23,3 +25,20 @@
->toBe($line);
});
});

expect()->extend('getTargets', function () {
$classes = [];
Targeted::make(
$this,
function (ObjectDescription $object) use (&$classes): bool {
$classes[] = $object->name;

return true;
},
'',
fn ($path) => 0,
);
$this->value = $classes;

return $this;
});

0 comments on commit be0551f

Please sign in to comment.