customers = new InMemoryCustomerRepository; $this->contacts = new InMemoryCustomerContactRepository; $this->billingUnits = new InMemoryBillingUnitRepository; $this->references = new FakeCustomerReferences; $this->removal = new CustomerRemoval($this->customers, $this->contacts, $this->billingUnits, $this->references); $this->removeCustomer = new RemoveCustomer($this->customers, $this->removal, new FakeTransactionManager); $this->customerId = $this->customers->save(Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company))->id(); }); it('removes a customer that has nothing but its name', function () { expect($this->removal->canRemove($this->customerId))->toBeTrue(); $this->removeCustomer->execute($this->customerId); expect($this->customers->findById($this->customerId))->toBeNull() ->and($this->removal->canRemove($this->customerId))->toBeFalse(); }); it('does not remove an unknown customer', function () { $this->removeCustomer->execute(999); })->throws(CustomerNotFound::class); it('does not remove the internal customer', function () { $internalId = $this->customers->save(Customer::reconstitute(50, new CustomerName('Oma Oy'), CustomerType::Company, null, null, true))->id(); expect($this->removal->canRemove($internalId))->toBeFalse() ->and(fn () => $this->removeCustomer->execute($internalId))->toThrow(InternalCustomerCannotBeRemoved::class) ->and($this->customers->findById($internalId))->not->toBeNull(); }); it('does not remove a customer that has a contact or a billing unit', function (string $row) { match ($row) { 'contact' => $this->contacts->save(CustomerContact::create($this->customerId, new ContactName('Maija'))), 'billing unit' => $this->billingUnits->save(BillingUnit::create($this->customerId, new BillingUnitName('Mainos Oy'))), }; expect($this->removal->canRemove($this->customerId))->toBeFalse() ->and(fn () => $this->removeCustomer->execute($this->customerId))->toThrow(CustomerInUse::class) ->and($this->customers->findById($this->customerId))->not->toBeNull(); })->with(['contact', 'billing unit']); it('does not remove a customer that other modules refer to', function () { $this->references->referencedIds = [$this->customerId]; expect($this->removal->canRemove($this->customerId))->toBeFalse() ->and(fn () => $this->removeCustomer->execute($this->customerId))->toThrow(CustomerInUse::class) ->and($this->customers->findById($this->customerId))->not->toBeNull(); });