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 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 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()); }
/** * @param AddBook $command */ private function handleAddBook(AddBook $command) { $book = Book::add($command->getId(), $command->getAuthors(), $command->getTitle(), $command->getISBN()); $this->repository->store($book); }