Beispiel #1
0
 /**
  * @param Uuid $id
  * @return AggregateRoot
  * @throws AggregateNotFoundException
  */
 public function findById(Uuid $id)
 {
     $events = $this->storage->getEventsForAggregate($id);
     $book = new Book($id);
     $book->loadFromHistory($events);
     return $book;
 }
Beispiel #2
0
 public function testCheckInIsIdempotent()
 {
     $id = Uuid::createNew();
     $book = new Book($id);
     $book->checkIn();
     $events = $book->getUncommittedChanges()->getIterator()->getArrayCopy();
     self::assertCount(0, $events);
 }
 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 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);
 }
Beispiel #5
0
 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);
 }
Beispiel #7
0
 /**
  * @param BookCheckedOut $event
  */
 protected function applyBookCheckedOut(BookCheckedOut $event)
 {
     parent::applyBookCheckedOut($event);
     $this->available = false;
 }