createMock(CustomerRepository::class); } if ($dispatcher === null) { $dispatcher = $this->createMock(EventDispatcherInterface::class); $dispatcher->method('dispatch')->willReturnCallback(function ($event) { return $event; }); } if ($validator === null) { $validator = $this->createMock(ValidatorInterface::class); $validator->method('validate')->willReturn(new ConstraintViolationList()); } if ($configuration === null) { $configuration = SystemConfigurationFactory::createStub([ 'defaults' => [ 'customer' => [ 'timezone' => 'Europe/Vienna', 'country' => 'IN', 'currency' => 'RUB', ] ] ]); } return new CustomerService($repository, $configuration, $validator, $dispatcher); } public function testCannotSavePersistedCustomerAsNew(): void { $Customer = $this->createMock(Customer::class); $Customer->expects($this->once())->method('getId')->willReturn(1); $sut = $this->getSut(); $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot create customer, already persisted'); $sut->saveNewCustomer($Customer); } public function testSaveNewCustomerHasValidationError(): void { $constraints = new ConstraintViolationList(); $constraints->add(new ConstraintViolation('toooo many tests', 'abc.def', [], '$root', 'begin', 4, null, null, null, '$cause')); $validator = $this->createMock(ValidatorInterface::class); $validator->method('validate')->willReturn($constraints); $sut = $this->getSut(null, $validator); $this->expectException(ValidationFailedException::class); $this->expectExceptionMessage('Validation Failed'); $sut->saveNewCustomer(new Customer('foo')); } public function testUpdateDispatchesEvents(): void { $Customer = $this->createMock(Customer::class); $Customer->method('getId')->willReturn(1); $dispatcher = $this->createMock(EventDispatcherInterface::class); $dispatcher->expects($this->exactly(2))->method('dispatch')->willReturnCallback(function ($event) use ($Customer) { if ($event instanceof CustomerUpdatePostEvent) { self::assertSame($Customer, $event->getCustomer()); } elseif ($event instanceof CustomerUpdatePreEvent) { self::assertSame($Customer, $event->getCustomer()); } else { $this->fail('Invalid event received'); } return $event; }); $sut = $this->getSut($dispatcher); $sut->updateCustomer($Customer); } public function testCreateNewCustomerDispatchesEvents(): void { $dispatcher = $this->createMock(EventDispatcherInterface::class); $dispatcher->expects($this->exactly(2))->method('dispatch')->willReturnCallback(function ($event) { if ($event instanceof CustomerMetaDefinitionEvent) { self::assertInstanceOf(Customer::class, $event->getEntity()); } elseif ($event instanceof CustomerCreateEvent) { self::assertInstanceOf(Customer::class, $event->getCustomer()); } else { $this->fail('Invalid event received'); } return $event; }); $sut = $this->getSut($dispatcher); $customer = $sut->createNewCustomer(''); self::assertInstanceOf(Customer::class, $customer); self::assertEquals('Europe/Vienna', $customer->getTimezone()); self::assertEquals('IN', $customer->getCountry()); self::assertEquals('RUB', $customer->getCurrency()); } public function testSaveNewCustomerDispatchesEvents(): void { $dispatcher = $this->createMock(EventDispatcherInterface::class); $dispatcher->expects($this->exactly(2))->method('dispatch')->willReturnCallback(function ($event) { if ($event instanceof CustomerCreatePreEvent) { self::assertInstanceOf(Customer::class, $event->getCustomer()); } elseif ($event instanceof CustomerCreatePostEvent) { self::assertInstanceOf(Customer::class, $event->getCustomer()); } else { $this->fail('Invalid event received'); } return $event; }); $sut = $this->getSut($dispatcher); $Customer = new Customer('foo'); $sut->saveNewCustomer($Customer); } }