130 lines
4.1 KiB
PHP
130 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Architecture;
|
|
|
|
use App\Exceptions\GlobalHandler;
|
|
use App\Services\ArcadiaEmployee;
|
|
use App\Services\EmployeeUpsertServiceResult;
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
// 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('dtos have Dto suffix')
|
|
->expect("App\Dto")
|
|
->toHaveSuffix('Dto');
|
|
|
|
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\\'],
|
|
]);
|