public function testSerialization() { $uuid = Uuid::createNew(); $serialized = $uuid->serialize(); $deserialized = Uuid::deserialize($serialized); self::assertEquals($uuid->getValue(), $deserialized->getValue()); }
public function testSerialization() { $id = Uuid::createNew(); $user = new User($id, 'foo', 'bar', 'name', 1); $serialized = $user->serialize(); $deserialized = User::deserialize($serialized); self::assertEquals($user, $deserialized); }
public function testSerialization() { $id = Uuid::createNew(); $book = new Book($id, new Authors(), 'title', 'isbn', false, 1); $serialized = $book->serialize(); $deserialized = Book::deserialize($serialized); self::assertEquals($book, $deserialized); }
public function testCheckInIsIdempotent() { $id = Uuid::createNew(); $book = new Book($id); $book->checkIn(); $events = $book->getUncommittedChanges()->getIterator()->getArrayCopy(); self::assertCount(0, $events); }
public function testGetByUserNameActionThrowsNotFoundExceptionIfNotFound() { self::setExpectedException(NotFoundHttpException::class); $id = Uuid::createNew(); $user = new User($id, 'user', 'email', 'name', 0); $this->service->expects(self::once())->method('getUserByUserName')->with('user')->will(self::returnValue(null)); $fetcher = $this->getMockBuilder(ParamFetcherInterface::class)->getMockForAbstractClass(); $fetcher->expects(self::once())->method('get')->with('user_name')->will(self::returnValue('user')); $controller = new UsersController($this->viewBuilder, $this->service, $this->commandBus); $view = $controller->getByUserNameAction($fetcher); }
public function testGetUserByEmailAddressReturnsFoundUser() { $id = Uuid::createNew(); $user = new User($id, 'user', 'foo', 'name', 0); $storage = $this->getMockBuilder(Storage::class)->disableOriginalConstructor()->getMock(); $storage->expects(self::once())->method('findBy')->with(['userName' => 'user'], 0, 1)->will(self::returnValue([$user])); $service = new UserService($storage); $actualUser = $service->getUserByUserName('user'); self::assertInstanceOf(User::class, $actualUser); self::assertEquals($user->getId(), $actualUser->getId()); }
public function testAddBookHandlerWillCallStoreOnRepository() { $id = Uuid::createNew(); $title = 'foo'; $authors = [new AddAuthor($id, 'first', 'last', -1)]; $isbn = 'isbn'; $command = new AddBook($id, $authors, $title, $isbn); $book = Book::add($command->getId(), $command->getAuthors(), $command->getTitle(), $command->getISBN()); $repository = $this->getMockBuilder(Repository::class)->disableOriginalConstructor()->getMock(); $repository->expects(self::once())->method('store')->with($book); $handler = new AddBookHandler($repository); $handler->handle($command); }
public function testCreateUserHandlerWillCallStoreOnRepository() { $id = Uuid::createNew(); $userName = '******'; $emailAddress = 'bar'; $fullName = 'baz boo'; $command = new CreateUser($id, $userName, $emailAddress, $fullName); $user = User::create($command->getId(), $command->getUserName(), $command->getEmailAddress(), $command->getFullName()); $repository = $this->getMockBuilder(Repository::class)->disableOriginalConstructor()->getMock(); $repository->expects(self::once())->method('store')->with($user); $handler = new CreateUserHandler($repository); $handler->handle($command); }
public function testReturnBookHandlerWillCallStoreOnRepository() { $bookId = Uuid::createNew(); $command = new ReturnBook($bookId, 0); $book = Book::add($bookId, [], 'title', 'isbn'); $repository = $this->getMockBuilder(Repository::class)->disableOriginalConstructor()->getMock(); $repository->expects(self::once())->method('findById')->with($bookId)->will(self::returnValue($book)); $repository->expects(self::once())->method('store')->will(self::returnCallback(function (Book $actual) use($book) { self::assertEquals($book->getId(), $actual->getId()); })); $handler = new ReturnBookHandler($repository); $handler->handle($command); }
public function testFindUserByIdLoadsUserFromHistory() { $userId = Uuid::createNew(); $userName = '******'; $emailAddress = 'bar'; $fullName = 'baz boo'; $expectedUser = User::create($userId, $userName, $emailAddress, $fullName); $events = new Events([new UserCreated($userId, $userName, $emailAddress, $fullName)]); $storage = $this->getMockBuilder(EventStore::class)->disableOriginalConstructor()->getMock(); $storage->expects(self::once())->method('getEventsForAggregate')->with($userId)->will(self::returnValue($events)); $repository = new Users($storage); $actualUser = $repository->findById($userId); self::assertEquals($expectedUser->getId(), $actualUser->getId()); }
public function testExecuteRetrievesIdentitiesFromEventStoreAndInsertsDocumentsIntoReadStore() { $eventStore = $this->getMockBuilder(EventStore::class)->disableOriginalConstructor()->getMock(); $readStore = $this->getMockBuilder(Storage::class)->disableOriginalConstructor()->getMock(); $identities = [Uuid::createFromValue(1), Uuid::createFromValue(2), Uuid::createFromValue(3), Uuid::createFromValue(4), Uuid::createFromValue(5)]; $eventStore->expects(self::once())->method('getAggregateIds')->will(self::returnValue($identities)); $eventStore->expects(self::exactly(count($identities)))->method('getEventsForAggregate')->will(self::returnCallback(function (Uuid $id) { return new Events([new BookAdded($id, [new AuthorAdded($id, 'first', 'last')], 'title', 'isbn'), new AuthorAdded($id, 'first', 'last'), new BookCheckedOut($id, Uuid::createNew())]); })); $readStore->expects(self::exactly(count($identities)))->method('upsert'); $in = $this->getMockBuilder(InputInterface::class)->getMockForAbstractClass(); $out = $this->getMockBuilder(OutputInterface::class)->getMockForAbstractClass(); $reload = new ReloadBookReadStore($eventStore, $readStore); $reload->run($in, $out); }
public function testCreateFromReadModelMapsDocumentToResource() { $id = Uuid::createNew(); $authors = new Authors(); $title = 'title'; $isbn = 'isbn'; $available = true; $version = 1; $document = new BookDocument($id, $authors, $title, $isbn, $available, $version); $resource = BookResource::createFromReadModel($document); self::assertEquals($id, $resource->getId()); self::assertEquals($title, $resource->getTitle()); self::assertEquals($isbn, $resource->getISBN()); self::assertEquals($available, $resource->isAvailable()); }
public function testFindBookByIdLoadsBookFromHistory() { $bookId = Uuid::createNew(); $title = 'foo'; $isbn = 'isbn'; $authors = []; $authorFirstName = 'first'; $authorLastName = 'last'; $expectedBook = Book::add($bookId, $authors, $title, $isbn); $expectedBook->addAuthor($authorFirstName, $authorLastName); $events = new Events([new BookAdded($bookId, $authors, $title, $isbn), new AuthorAdded($bookId, $authorFirstName, $authorLastName)]); $storage = $this->getMockBuilder(EventStore::class)->disableOriginalConstructor()->getMock(); $storage->expects(self::once())->method('getEventsForAggregate')->with($bookId)->will(self::returnValue($events)); $repository = new Books($storage); $actualBook = $repository->findById($bookId); self::assertEquals($expectedBook->getId(), $actualBook->getId()); }
/** * @Rest\Post("", * condition="request.headers.get('content-type') matches '/domain-model=add-book/i'" * ) * @Rest\View(statusCode=201) * * @ParamConverter("bookResource", * class="AppBundle\Controller\Resource\Book", * converter="fos_rest.request_body" * ) * * @param BookResource $bookResource * @return View * @throws HttpException */ public function addBookAction(BookResource $bookResource) { $id = Uuid::createNew(); $authors = array_map(function (AuthorResource $author) use($id) { return new AddAuthor($id, $author->getFirstName(), $author->getLastName(), -1); }, $bookResource->getAuthors()); $command = new AddBook($id, $authors, $bookResource->getTitle(), $bookResource->getISBN()); $this->commandBus->send($command); $book = $this->bookService->getBook($id); return $this->viewBuilder->setDocument($book)->setVersion()->setLocation(static::BASE_ROUTE . $book->getId())->build(); }
public function testApplyConverterOnUserCreatesUser() { $id = Uuid::createNew(); $user = new UserReadModel($id, 'user', 'email', 'name', 1); $request = Request::createFromGlobals(); $request->attributes->set('id', $id); $this->userService->expects(self::once())->method('getUser')->with($id)->will(self::returnValue($user)); $this->configuration->expects(self::once())->method('getOptions')->will(self::returnValue(['id' => 'id'])); $this->configuration->expects(self::atLeastOnce())->method('getClass')->will(self::returnValue(UserResource::class)); $this->configuration->expects(self::atLeastOnce())->method('getName')->will(self::returnValue('user')); $converter = new ParamConverter($this->bookService, $this->userService); $converter->apply($request, $this->configuration); self::assertEquals($user->getId()->getValue(), $request->attributes->get('user')->getId()->getValue()); }
public function testBookIsAvailableAfterBookAdded() { $bookId = Uuid::createNew(); $title = 'foo'; $isbn = 'bar'; $firstName = 'bar'; $lastName = 'baz'; $authors = [new AuthorAdded($bookId, $firstName, $lastName)]; $storage = new MemoryStorage(); $service = new BookService($storage); $service->on(new BookAdded($bookId, $authors, $title, $isbn)); $book = $service->getBook($bookId); self::assertEquals($bookId, $book->getId()); self::assertEquals($title, $book->getTitle()); self::assertTrue($book->isAvailable()); foreach ($book->getAuthors()->getIterator() as $author) { /* @var $author Author */ self::assertEquals($firstName, $author->getFirstName()); self::assertEquals($lastName, $author->getLastName()); } }
/** * @Rest\Put("", * condition="request.headers.get('content-type') matches '/domain-model=create-user/i'" * ) * @Rest\View() * * @ParamConverter("userResource", * class="AppBundle\Controller\Resource\User", * converter="fos_rest.request_body" * ) * * @param UserResource $userResource * @return View * @throws HttpException */ public function createAction(UserResource $userResource) { $user = $this->userService->getUserByUserName($userResource->getUserName()); if ($user != null) { return $this->viewBuilder->setStatus(204)->setVersion($user)->setLocation(static::BASE_ROUTE . $user->getId())->build(); } $id = Uuid::createNew(); $command = new CreateUser($id, $userResource->getUserName(), $userResource->getEmailAddress(), $userResource->getFullName()); $this->commandBus->send($command); $user = $this->userService->getUser($id); return $this->viewBuilder->setDocument($user)->setVersion()->setLocation(static::BASE_ROUTE . $user->getId())->setStatus(201)->build(); }
public function testAppendEventForExistingAggregateWithWrongPlayheadThrowsException() { self::setExpectedException(ConcurrencyException::class); $aggregateId = Uuid::createNew(); $events = new Events([new FirstEvent()]); $eventBus = $this->getEventBus(); $storage = new MemoryEventStorage(); $serializer = $this->getSerializer(); $map = $this->getEventClassMap(); $store = new EventStore($eventBus, $storage, $serializer, $map); $store->save($aggregateId, $events, -1); $store->save($aggregateId, new Events([new SecondEvent()]), 1); }
public function testReturnBookSendsReturnBookCommand() { $this->commandBus->expects(self::once())->method('send')->will(self::returnCallback(function (Command $command) { self::assertInstanceOf(ReturnBook::class, $command); })); $id = Uuid::createNew(); $controller = new BooksController($this->viewBuilder, $this->service, $this->commandBus); $controller->returnBookAction($id, 0); }