Files
Toiminnanohjaus/tests/Unit/Modules/Customers/Application/ContactAndBillingUnitUseCasesTest.php
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

121 lines
6.4 KiB
PHP

<?php
use App\Modules\Customers\Application\DTOs\BillingUnitData;
use App\Modules\Customers\Application\DTOs\ContactData;
use App\Modules\Customers\Application\UseCases\AddBillingUnit;
use App\Modules\Customers\Application\UseCases\AddContact;
use App\Modules\Customers\Application\UseCases\RemoveBillingUnit;
use App\Modules\Customers\Application\UseCases\RemoveContact;
use App\Modules\Customers\Application\UseCases\UpdateBillingUnit;
use App\Modules\Customers\Application\UseCases\UpdateContact;
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\BillingUnitInUse;
use App\Modules\Customers\Domain\Exceptions\BillingUnitNotFound;
use App\Modules\Customers\Domain\Exceptions\ContactInUse;
use App\Modules\Customers\Domain\Exceptions\ContactNotFound;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Domain\Exceptions\EInvoiceOperatorRequired;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use Tests\Fakes\FakeCustomerRowUsage;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryBillingUnitRepository;
use Tests\Fakes\InMemoryCustomerContactRepository;
use Tests\Fakes\InMemoryCustomerRepository;
beforeEach(function () {
$this->customers = new InMemoryCustomerRepository;
$this->contacts = new InMemoryCustomerContactRepository;
$this->billingUnits = new InMemoryBillingUnitRepository;
$transactions = new FakeTransactionManager;
$this->addContact = new AddContact($this->customers, $this->contacts, $transactions);
$this->updateContact = new UpdateContact($this->contacts, $transactions);
$this->usage = new FakeCustomerRowUsage;
$this->removeContact = new RemoveContact($this->contacts, $this->usage, $transactions);
$this->addBillingUnit = new AddBillingUnit($this->customers, $this->billingUnits, $transactions);
$this->updateBillingUnit = new UpdateBillingUnit($this->billingUnits, $transactions);
$this->removeBillingUnit = new RemoveBillingUnit($this->billingUnits, $this->usage, $transactions);
$this->customerId = $this->customers->save(Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company))->id();
$this->otherCustomerId = $this->customers->save(Customer::register(new CustomerName('Toinen Oy'), CustomerType::Company))->id();
});
it('adds, changes and removes a contact', function () {
$contactId = $this->addContact->execute($this->customerId, new ContactData('Maija', 'Markkinointi', '040 123 4567', '[email protected]'));
expect($this->contacts->findForCustomer($this->customerId, $contactId)?->email()?->value)->toBe('[email protected]');
$this->updateContact->execute($this->customerId, $contactId, new ContactData('Maija Meikäläinen'));
$contact = $this->contacts->findForCustomer($this->customerId, $contactId);
expect($contact?->name()->value)->toBe('Maija Meikäläinen')
->and($contact?->email())->toBeNull();
$this->removeContact->execute($this->customerId, $contactId);
expect($this->contacts->contacts)->toBeEmpty();
});
it('rejects an invalid contact email', function () {
$this->addContact->execute($this->customerId, new ContactData('Maija', email: 'maija@'));
})->throws(InvalidEmailAddress::class);
it('adds a contact or a billing unit only to a stored customer', function (string $useCase, object $data) {
$this->{$useCase}->execute(999, $data);
})->throws(CustomerNotFound::class)->with([
'contact' => ['addContact', new ContactData('Maija')],
'billing unit' => ['addBillingUnit', new BillingUnitData('Mainos Oy')],
]);
it('does not touch a contact of another customer', function (string $action) {
$contactId = $this->addContact->execute($this->otherCustomerId, new ContactData('Toisen kontakti'));
match ($action) {
'update' => $this->updateContact->execute($this->customerId, $contactId, new ContactData('Muutettu')),
'remove' => $this->removeContact->execute($this->customerId, $contactId),
};
})->throws(ContactNotFound::class)->with(['update', 'remove']);
it('adds, changes and removes a billing unit', function () {
$billingUnitId = $this->addBillingUnit->execute($this->customerId, new BillingUnitData('Mainos Oy', "Katu 1\n00100 Helsinki"));
$this->updateBillingUnit->execute($this->customerId, $billingUnitId, new BillingUnitData('Mainos Oy, markkinointi', null, '0037 1234 5678', '003721291126', 'PO-42'));
$billingUnit = $this->billingUnits->findForCustomer($this->customerId, $billingUnitId);
expect($billingUnit?->name()->value)->toBe('Mainos Oy, markkinointi')
->and($billingUnit?->billingAddress())->toBeNull()
->and($billingUnit?->eInvoiceAddress()?->value)->toBe('003712345678')
->and($billingUnit?->reference())->toBe('PO-42');
$this->removeBillingUnit->execute($this->customerId, $billingUnitId);
expect($this->billingUnits->billingUnits)->toBeEmpty();
});
it('rejects an e-invoice address without an operator', function () {
$this->addBillingUnit->execute($this->customerId, new BillingUnitData('Mainos Oy', eInvoiceAddress: '003712345678'));
})->throws(EInvoiceOperatorRequired::class);
it('does not touch a billing unit of another customer', function (string $action) {
$billingUnitId = $this->addBillingUnit->execute($this->otherCustomerId, new BillingUnitData('Toinen Oy'));
match ($action) {
'update' => $this->updateBillingUnit->execute($this->customerId, $billingUnitId, new BillingUnitData('Muutettu')),
'remove' => $this->removeBillingUnit->execute($this->customerId, $billingUnitId),
};
})->throws(BillingUnitNotFound::class)->with(['update', 'remove']);
it('does not remove a contact or billing unit that a customer job uses', function () {
$contactId = $this->addContact->execute($this->customerId, new ContactData('Maija'));
$billingUnitId = $this->addBillingUnit->execute($this->customerId, new BillingUnitData('Mainos Oy'));
$this->usage->contactsInUse = [$contactId];
$this->usage->billingUnitsInUse = [$billingUnitId];
expect(fn () => $this->removeContact->execute($this->customerId, $contactId))->toThrow(ContactInUse::class)
->and(fn () => $this->removeBillingUnit->execute($this->customerId, $billingUnitId))->toThrow(BillingUnitInUse::class)
->and($this->contacts->contacts)->toHaveCount(1)
->and($this->billingUnits->billingUnits)->toHaveCount(1);
});