customers = new InMemoryCustomerRepository; $this->transactions = new FakeTransactionManager; $this->events = new FakeEventDispatcher; $this->registerCustomer = new RegisterCustomer($this->customers, $this->transactions, $this->events); }); it('stores the customer in a transaction and announces it', function () { $customerId = $this->registerCustomer->execute(new CustomerDetailsData('Mainos Oy', CustomerType::Company, '0737546-2')); $stored = $this->customers->findById($customerId); expect($stored?->name()->value)->toBe('Mainos Oy') ->and($stored?->businessId()?->value)->toBe('0737546-2') ->and($this->transactions->transactionCount)->toBe(1) ->and($this->events->dispatched)->toEqual([new CustomerRegistered($customerId)]); }); it('rejects a business id that another customer has', function () { $this->customers->save(Customer::register(new CustomerName('Vanha Oy'), CustomerType::Company, new BusinessId('0737546-2'))); expect(fn () => $this->registerCustomer->execute(new CustomerDetailsData('Uusi Oy', CustomerType::Company, '737546-2'))) ->toThrow(BusinessIdAlreadyInUse::class) ->and($this->customers->customers)->toHaveCount(1) ->and($this->events->dispatched)->toBeEmpty(); }); it('validates input before opening a transaction', function () { expect(fn () => $this->registerCustomer->execute(new CustomerDetailsData('Mainos Oy', CustomerType::Company, '0737546-3'))) ->toThrow(InvalidBusinessId::class) ->and($this->transactions->transactionCount)->toBe(0); });