Example #1
0
 /**
  * @param Uuid $id
  * @return User
  * @throws ObjectNotFoundException
  */
 public function getUser(Uuid $id)
 {
     $user = $this->storage->find($id->getValue());
     if ($user == null) {
         throw new ObjectNotFoundException('User', $id);
     }
     return $user;
 }
Example #2
0
 /**
  * @param Uuid $id
  * @return Book
  * @throws ObjectNotFoundException
  */
 public function getBook(Uuid $id)
 {
     $book = $this->storage->find($id->getValue());
     if ($book === null) {
         throw new ObjectNotFoundException('Book', $id);
     }
     return $book;
 }
Example #3
0
 public function testSerialization()
 {
     $uuid = Uuid::createNew();
     $serialized = $uuid->serialize();
     $deserialized = Uuid::deserialize($serialized);
     self::assertEquals($uuid->getValue(), $deserialized->getValue());
 }
Example #4
0
 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);
 }
Example #5
0
 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);
 }
Example #6
0
 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 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 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);
 }
Example #12
0
 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);
 }
Example #14
0
 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());
 }
Example #15
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());
 }
Example #16
0
 /**
  * @param array $data
  * @return User
  */
 public static function deserialize(array $data)
 {
     assert(array_key_exists('id', $data));
     assert(array_key_exists('userName', $data));
     assert(array_key_exists('emailAddress', $data));
     assert(array_key_exists('fullName', $data));
     assert(array_key_exists('version', $data));
     return new self(Uuid::deserialize($data['id']), $data['userName'], $data['emailAddress'], $data['fullName'], $data['version']);
 }
Example #17
0
 /**
  * @param array $data
  * @return Book
  */
 public static function deserialize(array $data)
 {
     assert(array_key_exists('id', $data));
     assert(array_key_exists('authors', $data));
     assert(array_key_exists('title', $data));
     assert(array_key_exists('isbn', $data));
     assert(array_key_exists('available', $data));
     assert(array_key_exists('version', $data));
     return new self(Uuid::deserialize($data['id']), Authors::deserialize($data['authors']), $data['title'], $data['isbn'], $data['available'], $data['version']);
 }
 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());
     }
 }
 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());
 }
 /**
  * @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();
 }
 /**
  * @param Uuid $bookId
  */
 public function __construct(Uuid $bookId)
 {
     parent::__construct(sprintf(static::$messageTemplate, $bookId->getValue()));
 }
 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);
 }
 public function getId()
 {
     return Uuid::createFromValue(1);
 }
Example #24
0
 /**
  * @param Request $request
  * @param ParamConfiguration $configuration
  * @return Uuid
  * @throws BadRequestHttpException
  */
 protected function convertUuid(Request $request, ParamConfiguration $configuration)
 {
     $uuid = $request->attributes->get($configuration->getName());
     if (empty($uuid)) {
         throw new BadRequestHttpException("Uuid value of '{$configuration->getName()}' must not be empty.");
     }
     return Uuid::createFromValue($uuid);
 }
 /**
  * @Rest\Put("/{id}",
  *  condition="request.headers.get('content-type') matches '/domain-model=checkout/i'",
  *  requirements={
  *      "id"="[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}"
  *  },
  *  defaults={
  *      "id"=null
  *  }
  * )
  * @Rest\View(statusCode=204)
  *
  * @ParamConverter("id",
  *  class="AppBundle\EventSourcing\EventStore\Uuid",
  *  converter="param_converter"
  * )
  * @ParamConverter("version",
  *  class="AppBundle\Domain\ReadModel\Book",
  *  options={
  *      "id": "id"
  *  },
  *  converter="param_converter"
  * )
  * @Rest\QueryParam(
  *  name="user_id",
  *  key=null,
  *  requirements="[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}",
  *  default=null,
  *  description="The user ID of the user that checks out the book.",
  *  strict=true,
  *  array=false,
  *  nullable=false
  * )
  *
  * @param Uuid $id
  * @param integer $version
  * @param ParamFetcherInterface $params
  * @throws HttpException
  */
 public function checkOutBookAction(Uuid $id, $version, ParamFetcherInterface $params)
 {
     $userId = Uuid::createFromValue($params->get('user_id'));
     $command = new CheckOutBook($id, $userId, $version);
     try {
         $this->commandBus->send($command);
     } catch (BookUnavailableException $e) {
         throw new PreconditionFailedHttpException($e->getMessage(), $e);
     }
 }
 /**
  * @param string $objectName
  * @param Uuid $objectId
  */
 public function __construct($objectName, Uuid $objectId)
 {
     parent::__construct(sprintf(static::$messageTemplate, $objectName, $objectId->getValue()));
 }
Example #27
0
 /**
  * @return Uuid[]
  */
 public function getAggregateIds()
 {
     return array_map(function ($identity) {
         return Uuid::createFromValue($identity);
     }, $this->storage->findIdentities());
 }
Example #28
0
 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);
 }