Add modular layered architecture with Customers module

Introduce a modular monolith under app/Modules with Domain, Application,
Infrastructure and Presentation layers.

- Shared module: DomainException base, TransactionManager and an
  EventDispatcher that publishes events after commit.
- Customers module: Customer entity, CustomerName and BusinessId
  (Y-tunnus checksum) value objects, RegisterCustomer/UpdateCustomer
  use cases, CustomerQueries read contract, Eloquent repository, and
  Livewire list and form pages with Finnish translations.
- Pest arch tests enforce layer and module dependency rules for every
  module; Larastan (level 6) added as a dev dependency.
- Raise Flux error text contrast to meet WCAG 2.2 AA.
- Document the architecture in docs/architecture.md and CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-22 10:23:33 +02:00
co-authored by Claude Opus 5.5
parent d403a86443
commit a3fb09bbf6
55 changed files with 2270 additions and 3 deletions
@@ -0,0 +1,103 @@
<?php
use App\Models\User;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->actingAs(User::factory()->create());
});
it('lists customers and filters them by search', function () {
CustomerModel::factory()->create(['name' => 'Alfa Media Oy']);
CustomerModel::factory()->create(['name' => 'Kahvila Kulma']);
$this->get(route('customers.index'))
->assertOk()
->assertSee('Alfa Media Oy')
->assertSee('Kahvila Kulma');
Livewire::test('customers::index')
->set('search', 'media')
->assertSee('Alfa Media Oy')
->assertDontSee('Kahvila Kulma')
->assertSee('1 asiakas');
});
it('registers a company customer', function () {
$this->get(route('customers.create'))->assertOk()->assertSee('Uusi asiakas');
Livewire::test('customers::form')
->set('form.name', ' Mainos Oy ')
->set('form.business_id', '0737546-2')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('customers.index'));
$customer = CustomerModel::query()->sole();
expect($customer->name)->toBe('Mainos Oy')
->and($customer->type)->toBe(CustomerType::Company)
->and($customer->business_id)->toBe('0737546-2');
});
it('ignores a business id typed before switching to consumer', function () {
Livewire::test('customers::form')
->set('form.name', 'Maija Meikäläinen')
->set('form.business_id', '0737546-2')
->set('form.type', CustomerType::Consumer->value)
->call('save')
->assertHasNoErrors();
expect(CustomerModel::query()->sole()->business_id)->toBeNull();
});
it('shows validation and business rule errors on the fields', function (array $input, string $field, string $message) {
CustomerModel::factory()->company('1572860-0')->create();
Livewire::test('customers::form')
->set($input)
->call('save')
->assertHasErrors([$field])
->assertSee($message);
expect(CustomerModel::query()->count())->toBe(1);
})->with([
'missing name' => [['form.name' => ''], 'form.name', 'Anna asiakkaan nimi.'],
'bad check digit' => [['form.name' => 'Mainos Oy', 'form.business_id' => '0737546-3'], 'form.business_id', 'Y-tunnus ei ole kelvollinen.'],
'business id in use' => [['form.name' => 'Mainos Oy', 'form.business_id' => '1572860-0'], 'form.business_id', 'Tällä Y-tunnuksella on jo asiakas.'],
]);
it('edits a customer', function () {
$customer = CustomerModel::factory()->company('0737546-2')->create(['name' => 'Mainos Oy']);
$this->get(route('customers.edit', $customer->id))
->assertOk()
->assertSee('Muokkaa asiakasta')
->assertSee('0737546-2');
Livewire::test('customers::form', ['customer' => $customer->id])
->assertSet('form.name', 'Mainos Oy')
->set('form.name', 'Mainos Oyj')
->call('save')
->assertHasNoErrors()
->assertRedirect(route('customers.index'));
expect($customer->fresh()->name)->toBe('Mainos Oyj');
});
it('keeps the customer its own business id when editing', function () {
$customer = CustomerModel::factory()->company('0737546-2')->create();
Livewire::test('customers::form', ['customer' => $customer->id])
->call('save')
->assertHasNoErrors();
});
it('returns 404 for an unknown customer', function () {
$this->get(route('customers.edit', 999))->assertNotFound();
});
@@ -0,0 +1,57 @@
<?php
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Domain\Contracts\CustomerRepository;
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('stores a new customer and reads it back as an entity', function () {
$repository = app(CustomerRepository::class);
$saved = $repository->save(Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company, new BusinessId('0737546-2')));
$found = $repository->findById((int) $saved->id());
expect($saved->id())->toBeInt()
->and($found?->name()->value)->toBe('Mainos Oy')
->and($found?->type())->toBe(CustomerType::Company)
->and($found?->businessId()?->value)->toBe('0737546-2');
});
it('updates a stored customer', function () {
$repository = app(CustomerRepository::class);
$customer = $repository->save(Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company));
$customer->changeDetails(new CustomerName('Maija Meikäläinen'), CustomerType::Consumer, null);
$repository->save($customer);
expect(CustomerModel::query()->count())->toBe(1)
->and(CustomerModel::query()->sole()->type)->toBe(CustomerType::Consumer);
});
it('tells whether another customer has the business id', function () {
$repository = app(CustomerRepository::class);
$customer = CustomerModel::factory()->company('0737546-2')->create();
expect($repository->isBusinessIdTaken(new BusinessId('0737546-2')))->toBeTrue()
->and($repository->isBusinessIdTaken(new BusinessId('0737546-2'), $customer->id))->toBeFalse()
->and($repository->isBusinessIdTaken(new BusinessId('1572860-0')))->toBeFalse();
});
it('returns summaries filtered by name or business id, ordered by name', function () {
CustomerModel::factory()->create(['name' => 'Bravo Media Oy']);
CustomerModel::factory()->company('0737546-2')->create(['name' => 'Alfa Media Oy']);
CustomerModel::factory()->create(['name' => 'Kahvila Kulma']);
$queries = app(CustomerQueries::class);
expect(array_map(fn ($summary) => $summary->name, $queries->search('media')))->toBe(['Alfa Media Oy', 'Bravo Media Oy'])
->and($queries->paginate('0737546')->total())->toBe(1)
->and($queries->paginate(null)->total())->toBe(3)
->and($queries->search('100%'))->toBe([]);
});
@@ -0,0 +1,55 @@
<?php
use App\Modules\Customers\Application\DTOs\CustomerDetailsData;
use App\Modules\Customers\Application\UseCases\RegisterCustomer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Events\CustomerRegistered;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Shared\Application\Contracts\EventDispatcher;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
uses(RefreshDatabase::class);
it('rolls back everything when the work fails', function () {
expect(fn () => app(TransactionManager::class)->run(function (): void {
CustomerModel::factory()->create();
throw new RuntimeException('fail');
}))->toThrow(RuntimeException::class);
expect(CustomerModel::query()->count())->toBe(0);
});
it('publishes events only after the transaction commits', function () {
Event::fake([CustomerRegistered::class]);
app(TransactionManager::class)->run(function (): void {
app(EventDispatcher::class)->dispatch(new CustomerRegistered(1));
Event::assertNotDispatched(CustomerRegistered::class);
});
Event::assertDispatched(CustomerRegistered::class);
});
it('drops events of a rolled back transaction', function () {
Event::fake([CustomerRegistered::class]);
rescue(fn () => app(TransactionManager::class)->run(function (): void {
app(EventDispatcher::class)->dispatch(new CustomerRegistered(1));
throw new RuntimeException('fail');
}), report: false);
Event::assertNotDispatched(CustomerRegistered::class);
});
it('announces a registered customer to listeners', function () {
Event::fake([CustomerRegistered::class]);
$customerId = app(RegisterCustomer::class)->execute(new CustomerDetailsData('Mainos Oy', CustomerType::Company));
Event::assertDispatched(CustomerRegistered::class, fn (CustomerRegistered $event) => $event->customerId === $customerId);
});