customers = new InMemoryCustomerRepository; $transactions = new FakeTransactionManager; $this->registerCustomer = new RegisterCustomer($this->customers, $transactions, new FakeEventDispatcher); $this->updateCustomer = new UpdateCustomer($this->customers, $transactions); $this->customerId = $this->registerCustomer->execute( new CustomerDetailsData('Maija Meikäläinen', CustomerType::Consumer, personalIdentityCode: '131052-308t'), ); }); it('stores the normalised code of a consumer', function () { expect($this->customers->findById($this->customerId)?->personalIdentityCode()?->value)->toBe('131052-308T'); }); it('rejects a code that belongs to another customer', function (string $useCase) { match ($useCase) { 'register' => $this->registerCustomer->execute(new CustomerDetailsData('Toinen', CustomerType::Consumer, personalIdentityCode: '131052-308T')), 'update' => $this->updateCustomer->execute( $this->registerCustomer->execute(new CustomerDetailsData('Toinen', CustomerType::Consumer)), new CustomerDetailsData('Toinen', CustomerType::Consumer, personalIdentityCode: '131052-308T'), ), }; })->throws(PersonalIdentityCodeAlreadyInUse::class)->with(['register', 'update']); it('keeps the stored code when the form leaves it empty, and lets the customer keep its own code', function () { $this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija M.', CustomerType::Consumer)); $this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija M.', CustomerType::Consumer, personalIdentityCode: '131052-308T')); expect($this->customers->findById($this->customerId)?->personalIdentityCode()?->value)->toBe('131052-308T'); }); it('removes the code when asked, unless a new code is given', function () { $this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija', CustomerType::Consumer, personalIdentityCode: '010594Y9032', removePersonalIdentityCode: true)); expect($this->customers->findById($this->customerId)?->personalIdentityCode()?->value)->toBe('010594Y9032'); $this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija', CustomerType::Consumer, removePersonalIdentityCode: true)); expect($this->customers->findById($this->customerId)?->personalIdentityCode())->toBeNull(); });