coolify/tests/ArchTest.php
peaklabs-dev 93cc12a308
test: improve arch test
- add attribute test
- order all test cases and their assertions
- remove unused test case
2025-12-01 23:11:33 +01:00

223 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests;
use Throwable;
arch()->preset()->php();
arch()->preset()->security();
arch()->preset()->strict();
arch('App')
->expect('App')
->not->toBeEnums()
->ignoring('App\Enums')
->not->toExtend(\Illuminate\Database\Eloquent\Model::class)
->ignoring('App\Models')
->not->toExtend(\Illuminate\Foundation\Http\FormRequest::class)
->ignoring('App\Http\Requests')
->not->toExtend(\Illuminate\Http\Resources\Json\JsonResource::class)
->ignoring('App\Http\Resources')
->not->toExtend(\Illuminate\Http\Resources\Json\ResourceCollection::class)
->ignoring('App\Http\Resources')
->not->toExtend(\Illuminate\Console\Command::class)
->ignoring('App\Console\Commands')
->not->toExtend(\Illuminate\Mail\Mailable::class)
->ignoring('App\Mail')
->not->toExtend(\Illuminate\Notifications\Notification::class)
->ignoring('App\Notifications')
->not->toExtend(\Illuminate\Support\ServiceProvider::class)
->ignoring('App\Providers')
->not->toImplement(Throwable::class)
->ignoring('App\Exceptions')
->not->toImplement(\Illuminate\Contracts\Queue\ShouldQueue::class)
->ignoring('App\Jobs')
->not->toUseTrait(\Illuminate\Foundation\Bus\Dispatchable::class)
->ignoring('App\Jobs')
->not->toUseTrait(\Illuminate\Queue\SerializesModels::class)
->ignoring('App\Events')
->not->toHaveSuffix('Controller')
->ignoring('App\Http\Controllers')
->not->toHaveSuffix('ServiceProvider')
->ignoring('App\Providers');
arch('Contracts')
->expect('App\Contracts')
->toBeInterfaces()
->toExtendNothing()
->toImplementNothing()
->toHaveLineCountLessThan(100);
arch('Attributes')
->expect('App\Attributes')
->toBeClasses()
->toHaveMethod(method: 'resolve')
->toHaveAttribute('Attribute')
->toImplement(\Illuminate\Contracts\Container\ContextualAttribute::class)
->toHaveLineCountLessThan(100);
arch('Concerns')
->expect('App\Concerns')
->toBeTraits()
->toExtendNothing()
->toImplementNothing()
->toHaveLineCountLessThan(100)
->toHavePrefix('Has');
arch('Traits')
->expect('App\Traits')
->toBeTraits()
->toExtendNothing()
->toImplementNothing()
->toHaveLineCountLessThan(100)
->toHavePrefix('Has');
arch('Enums')
->expect('App\Enums')
->toBeEnums()
->ignoring('App\Enums\Concerns')
->toExtendNothing()
->toImplementNothing()
->toHaveLineCountLessThan(80);
arch('Exceptions')
->expect('App\Exceptions')
->toBeClasses()
->toImplement('Throwable')
->ignoring('App\Exceptions\Handler')
->toHaveLineCountLessThan(150)
->toHaveSuffix('Exception');
arch('Http')
->expect('App\Http')
->toBeClasses()
->toOnlyBeUsedIn('App\Http');
arch('Middleware')
->expect('App\Http\Middleware')
->toBeClasses()
->toHaveMethod('handle')
->toHaveLineCountLessThan(150);
arch('Controllers')
->expect('App\Http\Controllers')
->toBeClasses()
->not->toHavePublicMethodsBesides(['__construct', '__invoke', 'index', 'show', 'create', 'store', 'edit', 'update', 'destroy', 'middleware'])
->toOnlyBeUsedIn('App\Http\Controllers')
->toHaveLineCountLessThan(250)
->ignoring('App\Http\Controllers\Api')
->toHaveSuffix('Controller');
arch('Requests')
->expect('App\Http\Requests')
->toBeClasses()
->toHaveMethod(method: 'rules')
->toExtend(\Illuminate\Foundation\Http\FormRequest::class)
->toOnlyBeUsedIn('App\Http\Controllers')
->toHaveLineCountLessThan(150)
->toHaveSuffix('Request');
arch('Resources')
->expect('App\Http\Resources')
->toBeClasses()
->toExtend(\Illuminate\Http\Resources\Json\JsonResource::class)
->toOnlyBeUsedIn('App\Http\Controllers')
->toHaveLineCountLessThan(150)
->toHaveSuffix('Resource');
arch('Actions')
->expect('App\Actions')
->toBeClasses()
->not->toHavePublicMethodsBesides(['handle'])
->toExtendNothing()
->toImplementNothing()
->toHaveLineCountLessThan(250)
->not->toHaveSuffix('Action');
arch('Services')
->expect('App\Services')
->toBeClasses()
->toHaveLineCountLessThan(250)
->toHaveSuffix('Service');
arch('Events')
->expect('App\Events')
->toBeClasses()
->toExtendNothing()
->toUseTrait(\Illuminate\Queue\SerializesModels::class)
->toHaveLineCountLessThan(100)
->not->toHaveSuffix('Event');
arch('Listeners')
->expect('App\Listeners')
->toBeClasses()
->not->toHavePublicMethodsBesides(['__construct', 'handle'])
->toHaveLineCountLessThan(100);
arch('Commands')
->expect('App\Console\Commands')
->toBeClasses()
->not->toHavePublicMethodsBesides(['handle'])
->toExtend(\Illuminate\Console\Command::class)
->toImplementNothing()
->toHaveLineCountLessThan(150)
->toHaveSuffix('Command');
arch('Jobs')
->expect('App\Jobs')
->toBeClasses()
->not->toHavePublicMethodsBesides(['handle'])
->toImplement(\Illuminate\Contracts\Queue\ShouldQueue::class)
->toUseTrait(\Illuminate\Foundation\Bus\Dispatchable::class)
->toHaveLineCountLessThan(250)
->toHaveSuffix('Job');
arch('Mail')
->expect('App\Mail')
->toBeClasses()
->toExtend(\Illuminate\Mail\Mailable::class)
->toImplement(\Illuminate\Contracts\Queue\ShouldQueue::class)
->toHaveLineCountLessThan(150);
arch('Notifications')
->expect('App\Notifications')
->toBeClasses()
->toExtend(\Illuminate\Notifications\Notification::class)
->toHaveLineCountLessThan(200)
->toHaveSuffix('Notification');
arch('Models')
->expect('App\Models')
->toBeClasses()
->toExtend(\Illuminate\Database\Eloquent\Model::class)
->not->toUseTrait(\Illuminate\Database\Eloquent\SoftDeletes::class)
->toOnlyUse('Illuminate\Database')
->toHaveLineCountLessThan(250)
->not->toHaveSuffix('Model');
arch('Queries')
->expect('App\Queries')
->toBeClasses()
->toExtend(\Illuminate\Database\Eloquent\Builder::class)
->not->toHavePublicMethodsBesides(['__construct', 'builder'])
->toHaveLineCountLessThan(150);
arch('Policies')
->expect('App\Policies')
->toBeClasses()
->toHaveLineCountLessThan(150)
->toHaveSuffix('Policy');
arch('Providers')
->expect('App\Providers')
->toBeClasses()
->toExtend(\Illuminate\Support\ServiceProvider::class)
->not->toBeUsed()
->toHaveLineCountLessThan(250)
->toHaveSuffix('ServiceProvider');
arch('Functions')
->expect(['dd', 'ddd', 'dump', 'env', 'exit', 'ray'])
->not->toBeUsed();