Files
Toiminnanohjaus/tests/Feature/Modules/Customers/EloquentCustomerRepositoryTest.php
T
petriandClaude Opus 5.5 70e107f6e2 Add customer jobs with numbering, members, tags, links and history
Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 18:57:18 +02:00

58 lines
2.7 KiB
PHP

<?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()->where('is_internal', false)->count())->toBe(1)
->and(CustomerModel::query()->where('is_internal', false)->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(4) // and the internal customer
->and($queries->search('100%'))->toBe([]);
});