chore(tests): upgrade to Pest 5, PHPUnit 13, pest-plugin-phpstan and phpstan-webmozart-assert

This commit is contained in:
Jérémy Munsch 2026-11-16 14:49:53 +01:00
parent 5b88fb098a
commit 49c17d61f0
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
13 changed files with 1298 additions and 938 deletions

View file

@ -7,7 +7,6 @@ namespace App\Cqs\Actions\Common;
use App\Contracts\ActionInput;
use App\Interfaces\ModelWithOrder;
use Illuminate\Database\Eloquent\Model;
use Webmozart\Assert\Assert;
final readonly class ChangeModelOrderInput implements ActionInput
{
@ -15,7 +14,5 @@ final readonly class ChangeModelOrderInput implements ActionInput
public Model & ModelWithOrder $model,
public \SortDirection $direction,
) {
Assert::isInstanceOf($this->model, Model::class);
Assert::isInstanceOf($this->model, ModelWithOrder::class);
}
}

View file

@ -15,8 +15,6 @@ final readonly class ChangeModelOrderPositionInput implements ActionInput
public Model & ModelWithOrder $model,
public int $targetOrder,
) {
Assert::isInstanceOf($this->model, Model::class);
Assert::isInstanceOf($this->model, ModelWithOrder::class);
Assert::greaterThanEq($this->targetOrder, 1);
}
}

View file

@ -7,14 +7,11 @@ namespace App\Cqs\Actions\Common;
use App\Contracts\ActionInput;
use App\Interfaces\ModelWithStar;
use Illuminate\Database\Eloquent\Model;
use Webmozart\Assert\Assert;
final readonly class ToggleModelStarInput implements ActionInput
{
public function __construct(
public Model & ModelWithStar $model,
) {
Assert::isInstanceOf($this->model, Model::class);
Assert::isInstanceOf($this->model, ModelWithStar::class);
}
}

View file

@ -7,14 +7,11 @@ namespace App\Cqs\Actions\Common;
use App\Contracts\ActionInput;
use App\Interfaces\ModelWithStatus;
use Illuminate\Database\Eloquent\Model;
use Webmozart\Assert\Assert;
final readonly class ToggleModelStatusInput implements ActionInput
{
public function __construct(
public Model & ModelWithStatus $model,
) {
Assert::isInstanceOf($this->model, Model::class);
Assert::isInstanceOf($this->model, ModelWithStatus::class);
}
}

View file

@ -44,13 +44,15 @@
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.6",
"nunomaduro/mock-final-classes": "^1.2",
"pestphp/pest": "^4.0",
"pestphp/pest-plugin-faker": "^4.0",
"pestphp/pest-plugin-laravel": "^4.0",
"pestphp/pest-plugin-type-coverage": "^4.0",
"pestphp/pest": "^5.0",
"pestphp/pest-plugin-faker": "^5.0",
"pestphp/pest-plugin-laravel": "^5.0",
"pestphp/pest-plugin-phpstan": "^5.0",
"pestphp/pest-plugin-type-coverage": "^5.0",
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpstan/phpstan-mockery": "^2.0",
"phpunit/phpunit": "^12.5",
"phpstan/phpstan-webmozart-assert": "^2.0",
"phpunit/phpunit": "^13.0",
"slevomat/coding-standard": "^8.30",
"spatie/laravel-ignition": "^2.0"
},

1541
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -118,6 +118,6 @@ final readonly class DiscountValue
$rate = $this->getPercentageRate();
return $basePrice->multiply((string) $rate, Product::ROUNDING);
return $basePrice->multiply(number_format($rate, 2, '.', ''), Product::ROUNDING);
}
}

View file

@ -1,8 +1,9 @@
includes:
- vendor/larastan/larastan/extension.neon
- vendor/nesbot/carbon/extension.neon
- vendor/pestphp/pest/phpstan-pest-extension.neon
- vendor/pestphp/pest-plugin-phpstan/extension.neon
- vendor/phpstan/phpstan-mockery/extension.neon
- vendor/phpstan/phpstan-webmozart-assert/extension.neon
parameters:
paths:

View file

@ -13,117 +13,114 @@ use App\Contracts\QueryInput;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\Finder\Finder;
describe('application coding rules', function (): void {
arch('No Domain Entities are used in Controllers')
->expect('App\Http\Controllers')
->not->toUse('Domain\Models\Entities');
// Application coding rules
arch('No Domain Entities are used in Controllers')
->expect('App\Http\Controllers')
->not->toUse('Domain\Models\Entities');
arch('No Domain Entities are used in Policies')
->expect('App\Policies')
->not->toUse('Domain\Models\Entities');
arch('No Domain Entities are used in Policies')
->expect('App\Policies')
->not->toUse('Domain\Models\Entities');
arch('App DTOs are final readonly classes')
->expect('App\Dto')
->classes()
->toBeFinal()
->toBeReadonly();
arch('App DTOs are final readonly classes')
->expect('App\Dto')
->classes()
->toBeFinal()
->toBeReadonly();
arch('App Actions implement App\Contracts\Action and Inputs implement ActionInput', function (): void {
$path = __DIR__ . '/../../app/Cqs/Actions';
$files = (new Finder())->files()->in($path)->name('*.php');
arch('App Actions implement App\Contracts\Action and Inputs implement ActionInput', function (): void {
$path = __DIR__ . '/../../app/Cqs/Actions';
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$subPath = $file->getRelativePathname();
$className = 'App\\Cqs\\Actions\\' . str_replace(['/', '.php'], ['\\', ''], $subPath);
foreach ($files as $file) {
$subPath = $file->getRelativePathname();
$className = 'App\\Cqs\\Actions\\' . str_replace(['/', '.php'], ['\\', ''], $subPath);
if (! class_exists($className) && ! interface_exists($className)) {
throw new \RuntimeException("Symbol {$className} does not exist.");
}
$reflection = new \ReflectionClass($className);
if (str_ends_with($className, 'Input')) {
expect($reflection->implementsInterface(ActionInput::class))
->toBeTrue("{$className} must implement App\Contracts\ActionInput");
} elseif (str_ends_with($className, 'Output')) {
if ($reflection->isEnum()) {
expect($reflection->isEnum())->toBeTrue();
} else {
expect($reflection->isFinal())
->toBeTrue("{$className} must be final");
expect($reflection->isReadOnly())
->toBeTrue("{$className} must be readonly");
}
} else {
expect($reflection->implementsInterface(Action::class))
->toBeTrue("{$className} must implement App\Contracts\Action");
}
if (! class_exists($className) && ! interface_exists($className)) {
throw new \RuntimeException("Symbol {$className} does not exist.");
}
});
arch('App Queries implement App\Contracts\Query and Inputs implement QueryInput', function (): void {
$path = __DIR__ . '/../../app/Cqs/Queries';
$files = (new Finder())->files()->in($path)->name('*.php');
$reflection = new \ReflectionClass($className);
foreach ($files as $file) {
$subPath = $file->getRelativePathname();
$className = 'App\\Cqs\\Queries\\' . str_replace(['/', '.php'], ['\\', ''], $subPath);
if (! class_exists($className) && ! interface_exists($className)) {
throw new \RuntimeException("Symbol {$className} does not exist.");
}
$reflection = new \ReflectionClass($className);
if (str_ends_with($className, 'Input')) {
expect($reflection->implementsInterface(QueryInput::class))
->toBeTrue("{$className} must implement App\Contracts\QueryInput");
} elseif (str_ends_with($className, 'Output')) {
expect($reflection->isFinal() && $reflection->isReadOnly())
->toBeTrue("{$className} must be final readonly");
} else {
expect($reflection->implementsInterface(Query::class))
->toBeTrue("{$className} must implement App\Contracts\Query");
if (str_ends_with($className, 'Input')) {
expect($reflection->implementsInterface(ActionInput::class))
->toBeTrue("{$className} must implement App\Contracts\ActionInput");
} elseif (str_ends_with($className, 'Output')) {
if (! $reflection->isEnum()) {
expect($reflection->isFinal())
->toBeTrue("{$className} must be final");
expect($reflection->isReadOnly())
->toBeTrue("{$className} must be readonly");
}
} else {
expect($reflection->implementsInterface(Action::class))
->toBeTrue("{$className} must implement App\Contracts\Action");
}
});
arch('Eloquent models use UtcDatetimeCast or CalendarDateCast instead of raw date or datetime casts', function (): void {
$forbiddenCasts = [
'date',
'datetime',
'immutable_date',
'immutable_datetime',
'custom_datetime',
'immutable_custom_datetime',
'timestamp',
];
$path = __DIR__ . '/../../app/Models';
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$subPath = $file->getRelativePathname();
$className = 'App\\Models\\' . str_replace(['/', '.php'], ['\\', ''], $subPath);
if (! class_exists($className)) {
throw new \RuntimeException("Model class {$className} does not exist.");
}
$reflection = new \ReflectionClass($className);
if ($reflection->isAbstract() || ! $reflection->isSubclassOf(Model::class)) {
continue;
}
/** @var Model $instance */
$instance = $reflection->newInstanceWithoutConstructor();
foreach ($instance->getCasts() as $attribute => $cast) {
$baseCast = is_string($cast) ? explode(':', $cast, 2)[0] : '';
expect(in_array($baseCast, $forbiddenCasts, true))
->toBeFalse(
"{$className}::casts()['{$attribute}'] must use "
. UtcDatetimeCast::class . ' or ' . CalendarDateCast::class . " instead of '{$cast}'."
);
}
}
});
}
});
arch('App Queries implement App\Contracts\Query and Inputs implement QueryInput', function (): void {
$path = __DIR__ . '/../../app/Cqs/Queries';
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$subPath = $file->getRelativePathname();
$className = 'App\\Cqs\\Queries\\' . str_replace(['/', '.php'], ['\\', ''], $subPath);
if (! class_exists($className) && ! interface_exists($className)) {
throw new \RuntimeException("Symbol {$className} does not exist.");
}
$reflection = new \ReflectionClass($className);
if (str_ends_with($className, 'Input')) {
expect($reflection->implementsInterface(QueryInput::class))
->toBeTrue("{$className} must implement App\Contracts\QueryInput");
} elseif (str_ends_with($className, 'Output')) {
expect($reflection->isFinal() && $reflection->isReadOnly())
->toBeTrue("{$className} must be final readonly");
} else {
expect($reflection->implementsInterface(Query::class))
->toBeTrue("{$className} must implement App\Contracts\Query");
}
}
});
arch('Eloquent models use UtcDatetimeCast or CalendarDateCast instead of raw date or datetime casts', function (): void {
$forbiddenCasts = [
'date',
'datetime',
'immutable_date',
'immutable_datetime',
'custom_datetime',
'immutable_custom_datetime',
'timestamp',
];
$path = __DIR__ . '/../../app/Models';
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$subPath = $file->getRelativePathname();
$className = 'App\\Models\\' . str_replace(['/', '.php'], ['\\', ''], $subPath);
if (! class_exists($className)) {
throw new \RuntimeException("Model class {$className} does not exist.");
}
$reflection = new \ReflectionClass($className);
if ($reflection->isAbstract() || ! $reflection->isSubclassOf(Model::class)) {
continue;
}
/** @var Model $instance */
$instance = $reflection->newInstanceWithoutConstructor();
foreach ($instance->getCasts() as $attribute => $cast) {
$baseCast = is_string($cast) ? explode(':', $cast, 2)[0] : '';
expect(in_array($baseCast, $forbiddenCasts, true))
->toBeFalse(
"{$className}::casts()['{$attribute}'] must use "
. UtcDatetimeCast::class . ' or ' . CalendarDateCast::class . " instead of '{$cast}'."
);
}
}
});

View file

@ -6,57 +6,56 @@ namespace Tests\Architecture;
use Shared\Filesystem\TemporaryFileFactory;
describe('coding standards', function (): void {
arch()->preset()->php();
arch()->preset()->security()->ignoring([
TemporaryFileFactory::class,
]);
// Coding standards
arch()->preset()->php();
arch()->preset()->security()->ignoring([
TemporaryFileFactory::class,
]);
arch('it has no shell execution functions are used')
->expect(['exec', 'shell_exec', 'system', 'passthru', 'proc_open'])
->not->toBeUsed();
arch('it has no shell execution functions are used')
->expect(['exec', 'shell_exec', 'system', 'passthru', 'proc_open'])
->not->toBeUsed();
arch('it has no eval usage')
->expect(['eval'])
->not->toBeUsed();
arch('it has no eval usage')
->expect(['eval'])
->not->toBeUsed();
arch('it has no dangerous file functions')
->expect(['file_get_contents', 'file_put_contents', 'fopen'])
->not->toBeUsed();
arch('it has no dangerous file functions')
->expect(['file_get_contents', 'file_put_contents', 'fopen'])
->not->toBeUsed();
arch('it has no globals are used')
->expect(['$GLOBALS', 'global'])
->not->toBeUsed();
arch('it has no globals are used')
->expect(['$GLOBALS', 'global'])
->not->toBeUsed();
arch('it has no singletons')
->expect(['getInstance', 'instance'])
->not->toBeUsed();
arch('it has no singletons')
->expect(['getInstance', 'instance'])
->not->toBeUsed();
arch('it has no insecure hashing')
->expect(['md5', 'sha1'])
->not->toBeUsed();
arch('it has no insecure hashing')
->expect(['md5', 'sha1'])
->not->toBeUsed();
arch('it has no sleep function are used')
->expect(['sleep', 'usleep'])
->not->toBeUsed();
arch('it has no sleep function are used')
->expect(['sleep', 'usleep'])
->not->toBeUsed();
arch('it has no debug globals are used')
->expect(['var_dump', 'die', 'print_r'])
->not->toBeUsed();
arch('it has no debug globals are used')
->expect(['var_dump', 'die', 'print_r'])
->not->toBeUsed();
arch('all app source files use strict types')
->expect('App')
->toUseStrictTypes();
arch('all app source files use strict types')
->expect('App')
->toUseStrictTypes();
arch('all domain source files use strict types')
->expect('Domain')
->toUseStrictTypes();
arch('all domain source files use strict types')
->expect('Domain')
->toUseStrictTypes();
arch('all shared source files use strict types')
->expect('Shared')
->toUseStrictTypes();
arch('all shared source files use strict types')
->expect('Shared')
->toUseStrictTypes();
arch('all infrastructure source files use strict types')
->expect('Infrastructure')
->toUseStrictTypes();
});
arch('all infrastructure source files use strict types')
->expect('Infrastructure')
->toUseStrictTypes();

View file

@ -8,83 +8,82 @@ use Shared\DDD\Directory;
use Shared\DDD\Entity;
use Shared\DDD\Repository;
describe('domain coding rules', function (): void {
arch('Entities implements Entity')
->expect("Domain\Models\Entities")
->toImplement(Entity::class);
// Domain coding rules
arch('Entities implements Entity')
->expect("Domain\Models\Entities")
->toImplement(Entity::class);
arch('Entities are final')
->expect("Domain\Models\Entities")
->toBeFinal();
arch('Entities are final')
->expect("Domain\Models\Entities")
->toBeFinal();
arch('ValueObjects are immutable (disabled due to mock-final-classes conflict)')
->expect("Domain\Models\ValueObjects")
->toBeReadonly()
->toBeFinal();
arch('ValueObjects are immutable (disabled due to mock-final-classes conflict)')
->expect("Domain\Models\ValueObjects")
->toBeReadonly()
->toBeFinal();
arch('Ports contains only interfaces')
->expect("Domain\Ports")
->classes()
->toBeInterface();
arch('Ports contains only interfaces')
->expect("Domain\Ports")
->classes()
->toBeInterface();
arch('No App classes are used in Domain')
->expect('Domain')
->not->toUse('App');
arch('No App classes are used in Domain')
->expect('Domain')
->not->toUse('App');
arch('No Infrastructure classes are used in Domain')
->expect('Domain')
->not->toUse('Infrastructure');
arch('No Infrastructure classes are used in Domain')
->expect('Domain')
->not->toUse('Infrastructure');
arch('No Illuminate classes are used in Domain')
->expect('Domain')
->not->toUse('Illuminate');
arch('No Illuminate classes are used in Domain')
->expect('Domain')
->not->toUse('Illuminate');
arch('No Symfony components are used in Domain')
->expect('Domain')
->not->toUse('Symfony');
arch('No Symfony components are used in Domain')
->expect('Domain')
->not->toUse('Symfony');
// * Deny some Laravel Functions in Domain
arch('it has no laravel helper functions')
->expect('Domain')
->not->toUse([
'abort',
'app',
'auth',
'asset',
'back',
'bcrypt',
'broadcast',
'cache',
'config',
'cookie',
'csrf_token',
'dd',
'dump',
'decrypt',
'dispatch',
'event',
'factory',
'logger',
'redirect',
'request',
'response',
'route',
'session',
'storage',
'url',
'throw_if',
'throw_unless',
'__',
'trans',
'trans_choice',
'view',
]);
// * Deny some Laravel Functions in Domain
arch('it has no laravel helper functions')
->expect('Domain')
->not->toUse([
'abort',
'app',
'auth',
'asset',
'back',
'bcrypt',
'broadcast',
'cache',
'config',
'cookie',
'csrf_token',
'dd',
'dump',
'decrypt',
'dispatch',
'event',
'factory',
'logger',
'redirect',
'request',
'response',
'route',
'session',
'storage',
'url',
'throw_if',
'throw_unless',
'__',
'trans',
'trans_choice',
'view',
]);
arch('Port Repositories extend Shared\DDD\Repository')
->expect("Domain\Ports\Repositories")
->toExtend(Repository::class);
arch('Port Repositories extend Shared\DDD\Repository')
->expect("Domain\Ports\Repositories")
->toExtend(Repository::class);
arch('Port Directories extend Shared\DDD\Directory')
->expect("Domain\Ports\Directories")
->toExtend(Directory::class);
});
arch('Port Directories extend Shared\DDD\Directory')
->expect("Domain\Ports\Directories")
->toExtend(Directory::class);

View file

@ -4,9 +4,8 @@ declare(strict_types=1);
namespace Tests\Architecture;
describe('infra coding rules', function (): void {
arch('No App classes are used in Infra')
->expect('Infrastructure')
->not->toUse('App')
->ignoring('App\Models');
});
// Infra coding rules
arch('No App classes are used in Infra')
->expect('Infrastructure')
->not->toUse('App')
->ignoring('App\Models');

View file

@ -9,125 +9,122 @@ use App\Services\ArcadiaEmployee;
use App\Services\EmployeeUpsertServiceResult;
use Symfony\Component\Finder\Finder;
describe('naming conventions in app', function (): void {
arch('casts have Cast suffix')
->expect("App\Casts")
->toHaveSuffix('Cast');
// Naming conventions in app
arch('casts have Cast suffix')
->expect("App\Casts")
->toHaveSuffix('Cast');
arch('adapters repositories have Repository suffix')
->expect("Infrastructure\Adapters\Repositories")
->toHaveSuffix('Repository');
arch('adapters repositories have Repository suffix')
->expect("Infrastructure\Adapters\Repositories")
->toHaveSuffix('Repository');
arch('dtos have Dto suffix')
->expect("App\Dto")
->toHaveSuffix('Dto');
arch('dtos have Dto suffix')
->expect("App\Dto")
->toHaveSuffix('Dto');
arch('exceptions have Exception suffix')
->expect("App\Exceptions")
->toHaveSuffix('Exception')
->ignoring([
GlobalHandler::class,
]);
arch('controllers have Controller suffix')
->expect("App\Http\Controllers")
->toHaveSuffix('Controller');
arch('requests have Request suffix')
->expect("App\Http\Requests")
->toHaveSuffix('Request');
arch('resources have Resource suffix')
->expect("App\Http\Resources")
->toHaveSuffix('Resource');
arch('policies have Policy suffix')
->expect("App\Policies")
->toHaveSuffix('Policy');
arch('providers have Provider suffix')
->expect("App\Providers")
->toHaveSuffix('Provider');
arch('services have Service suffix')
->expect("App\Services")
->toHaveSuffix('Service')
->ignoring([
ArcadiaEmployee::class,
EmployeeUpsertServiceResult::class,
]);
arch('queries have Query, QueryInput or QueryOutput suffix', function (): void {
$path = __DIR__ . '/../../app/Cqs/Queries';
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$className = $file->getBasename('.php');
expect(
str_ends_with($className, 'Query')
|| str_ends_with($className, 'QueryInput')
|| str_ends_with($className, 'QueryOutput')
)->toBeTrue("{$className} must have suffix Query, QueryInput or QueryOutput");
}
});
});
describe('naming conventions in domain', function (): void {
arch('exceptions have Exception suffix')
->expect("Domain\Exceptions")
->toHaveSuffix('Exception');
arch('policies have Policy suffix')
->expect("Domain\Policies")
->toHaveSuffix('Policy');
arch('port directories have Directory suffix')
->expect("Domain\Ports\Directories")
->toHaveSuffix('Directory');
arch('port repositories have Repository suffix')
->expect("Domain\Ports\Repositories")
->toHaveSuffix('Repository');
arch('domain services have Service suffix')
->expect("Domain\Services")
->toHaveSuffix('Service');
});
describe('naming conventions in infrastructure', function (): void {
arch('adapters serializers have Serializer suffix')
->expect("Infrastructure\Adapters\Serializers")
->toHaveSuffix('Serializer');
arch('adapters implement an interface from their domain or shared ports', function (
string $folder,
string $infraNs,
string $portNs
): void {
$path = __DIR__ . '/../../infra/' . $folder;
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$className = $infraNs . '\\' . $file->getFilenameWithoutExtension();
if (! class_exists($className) && ! interface_exists($className)) {
throw new \RuntimeException("Symbol {$className} does not exist.");
}
$interfaces = (new \ReflectionClass($className))->getInterfaceNames();
$hasPort = ! empty(array_filter(
$interfaces,
fn ($interface) => str_starts_with($interface, $portNs)
));
expect($hasPort)
->toBeTrue(message: "La classe {$className} n'implémente aucune interface de {$portNs}.");
}
})->with([
'Directories' => ['Adapters/Directories', 'Infrastructure\Adapters\Directories', 'Domain\Ports\Directories\\'],
'Events' => ['Adapters/Events', 'Infrastructure\Adapters\Events', 'Shared\DDD\\'],
'Locks' => ['Adapters/Locks', 'Infrastructure\Adapters\Locks', 'Shared\Concurrency\\'],
'Repositories' => ['Adapters/Repositories', 'Infrastructure\Adapters\Repositories', 'Domain\Ports\Repositories\\'],
'Services' => ['Adapters/Services', 'Infrastructure\Adapters\Services', 'Domain\Ports\Services\\'],
arch('app exceptions have Exception suffix')
->expect("App\Exceptions")
->toHaveSuffix('Exception')
->ignoring([
GlobalHandler::class,
]);
arch('controllers have Controller suffix')
->expect("App\Http\Controllers")
->toHaveSuffix('Controller');
arch('requests have Request suffix')
->expect("App\Http\Requests")
->toHaveSuffix('Request');
arch('resources have Resource suffix')
->expect("App\Http\Resources")
->toHaveSuffix('Resource');
arch('app policies have Policy suffix')
->expect("App\Policies")
->toHaveSuffix('Policy');
arch('providers have Provider suffix')
->expect("App\Providers")
->toHaveSuffix('Provider');
arch('services have Service suffix')
->expect("App\Services")
->toHaveSuffix('Service')
->ignoring([
ArcadiaEmployee::class,
EmployeeUpsertServiceResult::class,
]);
arch('queries have Query, QueryInput or QueryOutput suffix', function (): void {
$path = __DIR__ . '/../../app/Cqs/Queries';
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$className = $file->getBasename('.php');
expect(
str_ends_with($className, 'Query')
|| str_ends_with($className, 'QueryInput')
|| str_ends_with($className, 'QueryOutput')
)->toBeTrue("{$className} must have suffix Query, QueryInput or QueryOutput");
}
});
// Naming conventions in domain
arch('domain exceptions have Exception suffix')
->expect("Domain\Exceptions")
->toHaveSuffix('Exception');
arch('domain policies have Policy suffix')
->expect("Domain\Policies")
->toHaveSuffix('Policy');
arch('port directories have Directory suffix')
->expect("Domain\Ports\Directories")
->toHaveSuffix('Directory');
arch('port repositories have Repository suffix')
->expect("Domain\Ports\Repositories")
->toHaveSuffix('Repository');
arch('domain services have Service suffix')
->expect("Domain\Services")
->toHaveSuffix('Service');
// Naming conventions in infrastructure
arch('adapters serializers have Serializer suffix')
->expect("Infrastructure\Adapters\Serializers")
->toHaveSuffix('Serializer');
arch('adapters implement an interface from their domain or shared ports', function (
string $folder,
string $infraNs,
string $portNs
): void {
$path = __DIR__ . '/../../infra/' . $folder;
$files = (new Finder())->files()->in($path)->name('*.php');
foreach ($files as $file) {
$className = $infraNs . '\\' . $file->getFilenameWithoutExtension();
if (! class_exists($className) && ! interface_exists($className)) {
throw new \RuntimeException("Symbol {$className} does not exist.");
}
$interfaces = (new \ReflectionClass($className))->getInterfaceNames();
$hasPort = ! empty(array_filter(
$interfaces,
fn ($interface) => str_starts_with($interface, $portNs)
));
expect($hasPort)
->toBeTrue(message: "La classe {$className} n'implémente aucune interface de {$portNs}.");
}
})->with([
'Directories' => ['Adapters/Directories', 'Infrastructure\Adapters\Directories', 'Domain\Ports\Directories\\'],
'Events' => ['Adapters/Events', 'Infrastructure\Adapters\Events', 'Shared\DDD\\'],
'Locks' => ['Adapters/Locks', 'Infrastructure\Adapters\Locks', 'Shared\Concurrency\\'],
'Repositories' => ['Adapters/Repositories', 'Infrastructure\Adapters\Repositories', 'Domain\Ports\Repositories\\'],
'Services' => ['Adapters/Services', 'Infrastructure\Adapters\Services', 'Domain\Ports\Services\\'],
]);