Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1.     function it_creates_cart(
  2.         ValidatorInterface $validator,
  3.         ConstraintViolationListInterface $violationList,
  4.         ConstraintViolationInterface $constraintViolation,
  5.         MessageBusInterface $bus,
  6.         GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
  7.         ViewHandlerInterface $viewHandler
  8.     ): void
  9.     {
  10.         $request = new Request([
  11.             'token' => 'token',
  12.             'cartId' => '12345'
  13.         ]);
  14.  
  15.         $createCartRequest = new CreateCartRequest($request);
  16.  
  17.         $validator->validate($createCartRequest)->willReturn($violationList);
  18.  
  19.         $envelope = new Envelope($createCartRequest->getCommand(), []);
  20.         $bus->dispatch($createCartRequest->getCommand())->willReturn($envelope);
  21.  
  22.         $genericSuccessViewFactory->create('12345')->willReturn(new GenericSuccessView());
  23.  
  24.         $viewHandler->handle(Argument::any(), Argument::any())->willReturn(new Response());
  25.  
  26.         $this->__invoke($request);
  27.     }
  28.  
  29.     function it_returns_validation_error(
  30.         ValidatorInterface $validator,
  31.         ConstraintViolationInterface $constraintViolation,
  32.         MessageBusInterface $bus,
  33.         ValidationErrorViewFactoryInterface $validationErrorViewFactory,
  34.         ViewHandlerInterface $viewHandler
  35.     ): void
  36.     {
  37.         $request = new Request([
  38.             'token' => 'token',
  39.             'cartId' => '12345'
  40.         ]);
  41.  
  42.         $createCartRequest = new CreateCartRequest($request);
  43.  
  44.         $violationList = new ConstraintViolationList([$constraintViolation->getWrappedObject()]);
  45.  
  46.         $validator->validate($createCartRequest)->willReturn($violationList);
  47.  
  48.         $envelope = new Envelope($createCartRequest->getCommand(), []);
  49.         $bus->dispatch($createCartRequest->getCommand())->willReturn($envelope);
  50.  
  51.         $validationErrorViewFactory->create($violationList)->willReturn(new ValidationErrorView());
  52.  
  53.         $viewHandler->handle(Argument::any(), Argument::any())->willReturn(new Response());
  54.  
  55.         $this->__invoke($request);
  56.     }
  57.  
  58. //***********************************************************************************************************
  59.  
  60.     public function __invoke(Request $request): Response
  61.     {
  62.         $createCartRequest = CreateCartRequest::fromHttpRequest($request);
  63.  
  64.         $validationResults = $this->validator->validate($createCartRequest);
  65.  
  66.         if (0 !== count($validationResults)) {
  67.             return $this->viewHandler->handle(View::create(
  68.                 $this->validationErrorViewFactory->create($validationResults),
  69.                 Response::HTTP_BAD_REQUEST
  70.             ));
  71.         }
  72.  
  73.         $createCartCommand = $createCartRequest->getCommand();
  74.  
  75.         $this->bus->dispatch($createCartCommand);
  76.  
  77.         return $this->viewHandler->handle(View::create(
  78.             $this->genericSuccessViewFactory->create($createCartCommand->cartId())
  79.         ));
  80.     }
  81.  
  82. //***********************************************************************************************************
  83.  
  84. final class CreateCartRequest
  85. {
  86.     /** @var string|null */
  87.     private $token;
  88.  
  89.     /** @var string */
  90.     private $cartId;
  91.  
  92.     public function __construct(Request $request)
  93.     {
  94.         $this->token = $request->query->get('token');
  95.         $this->cartId = Uuid::uuid4()->toString();
  96.     }
  97.  
  98.     public static function fromHttpRequest(Request $request): self
  99.     {
  100.         return new self($request);
  101.     }
  102.  
  103.     public function getCommand(): CreateCart
  104.     {
  105.         return new CreateCart($this->token, $this->cartId);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement