Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function it_creates_cart(
- ValidatorInterface $validator,
- ConstraintViolationListInterface $violationList,
- ConstraintViolationInterface $constraintViolation,
- MessageBusInterface $bus,
- GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
- ViewHandlerInterface $viewHandler
- ): void
- {
- $request = new Request([
- 'token' => 'token',
- 'cartId' => '12345'
- ]);
- $createCartRequest = new CreateCartRequest($request);
- $validator->validate($createCartRequest)->willReturn($violationList);
- $envelope = new Envelope($createCartRequest->getCommand(), []);
- $bus->dispatch($createCartRequest->getCommand())->willReturn($envelope);
- $genericSuccessViewFactory->create('12345')->willReturn(new GenericSuccessView());
- $viewHandler->handle(Argument::any(), Argument::any())->willReturn(new Response());
- $this->__invoke($request);
- }
- function it_returns_validation_error(
- ValidatorInterface $validator,
- ConstraintViolationInterface $constraintViolation,
- MessageBusInterface $bus,
- ValidationErrorViewFactoryInterface $validationErrorViewFactory,
- ViewHandlerInterface $viewHandler
- ): void
- {
- $request = new Request([
- 'token' => 'token',
- 'cartId' => '12345'
- ]);
- $createCartRequest = new CreateCartRequest($request);
- $violationList = new ConstraintViolationList([$constraintViolation->getWrappedObject()]);
- $validator->validate($createCartRequest)->willReturn($violationList);
- $envelope = new Envelope($createCartRequest->getCommand(), []);
- $bus->dispatch($createCartRequest->getCommand())->willReturn($envelope);
- $validationErrorViewFactory->create($violationList)->willReturn(new ValidationErrorView());
- $viewHandler->handle(Argument::any(), Argument::any())->willReturn(new Response());
- $this->__invoke($request);
- }
- //***********************************************************************************************************
- public function __invoke(Request $request): Response
- {
- $createCartRequest = CreateCartRequest::fromHttpRequest($request);
- $validationResults = $this->validator->validate($createCartRequest);
- if (0 !== count($validationResults)) {
- return $this->viewHandler->handle(View::create(
- $this->validationErrorViewFactory->create($validationResults),
- Response::HTTP_BAD_REQUEST
- ));
- }
- $createCartCommand = $createCartRequest->getCommand();
- $this->bus->dispatch($createCartCommand);
- return $this->viewHandler->handle(View::create(
- $this->genericSuccessViewFactory->create($createCartCommand->cartId())
- ));
- }
- //***********************************************************************************************************
- final class CreateCartRequest
- {
- /** @var string|null */
- private $token;
- /** @var string */
- private $cartId;
- public function __construct(Request $request)
- {
- $this->token = $request->query->get('token');
- $this->cartId = Uuid::uuid4()->toString();
- }
- public static function fromHttpRequest(Request $request): self
- {
- return new self($request);
- }
- public function getCommand(): CreateCart
- {
- return new CreateCart($this->token, $this->cartId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement