/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/ChosenShippingMethodEligibilityValidatorSpec.php

https://github.com/Sylius/Sylius · PHP · 212 lines · 163 code · 41 blank · 8 comment · 0 complexity · dc551ec93dc33f9cbd2f25ac0e7f6f87 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace spec\Sylius\Bundle\ApiBundle\Validator\Constraints;
  12. use PhpSpec\ObjectBehavior;
  13. use Prophecy\Argument;
  14. use Sylius\Bundle\ApiBundle\Command\Checkout\ChooseShippingMethod;
  15. use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
  16. use Sylius\Bundle\ApiBundle\Validator\Constraints\ChosenShippingMethodEligibility;
  17. use Sylius\Component\Core\Model\AddressInterface;
  18. use Sylius\Component\Core\Model\OrderInterface;
  19. use Sylius\Component\Core\Model\ShipmentInterface;
  20. use Sylius\Component\Core\Model\ShippingMethodInterface;
  21. use Sylius\Component\Core\Repository\ShipmentRepositoryInterface;
  22. use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
  23. use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
  24. use Symfony\Component\Validator\Constraint;
  25. use Symfony\Component\Validator\ConstraintValidatorInterface;
  26. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  27. final class ChosenShippingMethodEligibilityValidatorSpec extends ObjectBehavior
  28. {
  29. function let(
  30. ShipmentRepositoryInterface $shipmentRepository,
  31. ShippingMethodRepositoryInterface $shippingMethodRepository,
  32. ShippingMethodsResolverInterface $shippingMethodsResolver,
  33. ExecutionContextInterface $executionContext
  34. ): void {
  35. $this->beConstructedWith($shipmentRepository, $shippingMethodRepository, $shippingMethodsResolver);
  36. $this->initialize($executionContext);
  37. }
  38. function it_is_a_constraint_validator(): void
  39. {
  40. $this->shouldImplement(ConstraintValidatorInterface::class);
  41. }
  42. function it_throws_an_exception_if_constraint_is_not_an_instance_of_chosen_shipping_method_eligibility(): void
  43. {
  44. $this
  45. ->shouldThrow(\InvalidArgumentException::class)
  46. ->during('validate', [new ChooseShippingMethod('SHIPPING_METHOD_CODE'), new class() extends Constraint {
  47. }])
  48. ;
  49. }
  50. function it_throws_an_exception_if_value_is_not_an_instance_of_choose_shipping_method_command(): void
  51. {
  52. $this
  53. ->shouldThrow(\InvalidArgumentException::class)
  54. ->during('validate', [new CompleteOrder(), new ChosenShippingMethodEligibility()])
  55. ;
  56. }
  57. function it_adds_violation_if_chosen_shipping_method_does_not_match_supported_methods(
  58. ShipmentRepositoryInterface $shipmentRepository,
  59. ShippingMethodRepositoryInterface $shippingMethodRepository,
  60. ShippingMethodsResolverInterface $shippingMethodsResolver,
  61. ExecutionContextInterface $executionContext,
  62. ShippingMethodInterface $shippingMethod,
  63. ShippingMethodInterface $differentShippingMethod,
  64. ShipmentInterface $shipment,
  65. OrderInterface $order,
  66. AddressInterface $shippingAddress
  67. ): void {
  68. $command = new ChooseShippingMethod('SHIPPING_METHOD_CODE');
  69. $command->setOrderTokenValue('ORDER_TOKEN');
  70. $command->setSubresourceId('123');
  71. $shippingMethodRepository->findOneBy(['code' => 'SHIPPING_METHOD_CODE'])->willReturn($shippingMethod);
  72. $shippingMethod->getName()->willReturn('DHL');
  73. $shipmentRepository->find('123')->willReturn($shipment);
  74. $shipment->getOrder()->willReturn($order);
  75. $order->getShippingAddress()->willReturn($shippingAddress);
  76. $shippingMethodsResolver->getSupportedMethods($shipment)->willReturn([$differentShippingMethod]);
  77. $executionContext
  78. ->addViolation('sylius.shipping_method.not_available', ['%name%' => 'DHL'])
  79. ->shouldBeCalled()
  80. ;
  81. $this->validate($command, new ChosenShippingMethodEligibility());
  82. }
  83. function it_does_not_add_violation_if_chosen_shipping_method_matches_supported_methods(
  84. ShipmentRepositoryInterface $shipmentRepository,
  85. ShippingMethodRepositoryInterface $shippingMethodRepository,
  86. ShippingMethodsResolverInterface $shippingMethodsResolver,
  87. ExecutionContextInterface $executionContext,
  88. ShippingMethodInterface $shippingMethod,
  89. ShipmentInterface $shipment,
  90. OrderInterface $order,
  91. AddressInterface $shippingAddress
  92. ): void {
  93. $command = new ChooseShippingMethod('SHIPPING_METHOD_CODE');
  94. $command->setOrderTokenValue('ORDER_TOKEN');
  95. $command->setSubresourceId('123');
  96. $shippingMethodRepository->findOneBy(['code' => 'SHIPPING_METHOD_CODE'])->willReturn($shippingMethod);
  97. $shipmentRepository->find('123')->willReturn($shipment);
  98. $shipment->getOrder()->willReturn($order);
  99. $order->getShippingAddress()->willReturn($shippingAddress);
  100. $shippingMethodsResolver->getSupportedMethods($shipment)->willReturn([$shippingMethod]);
  101. $executionContext
  102. ->addViolation('sylius.shipping_method.not_available', Argument::any())
  103. ->shouldNotBeCalled()
  104. ;
  105. $this->validate($command, new ChosenShippingMethodEligibility());
  106. }
  107. function it_adds_a_violation_if_given_shipping_method_is_null(
  108. ShipmentRepositoryInterface $shipmentRepository,
  109. ShippingMethodRepositoryInterface $shippingMethodRepository,
  110. ExecutionContextInterface $executionContext
  111. ): void {
  112. $command = new ChooseShippingMethod('SHIPPING_METHOD_CODE');
  113. $command->setOrderTokenValue('ORDER_TOKEN');
  114. $command->setSubresourceId('123');
  115. $shippingMethodRepository->findOneBy(['code' => 'SHIPPING_METHOD_CODE'])->willReturn(null);
  116. $shipmentRepository->find('123')->shouldNotBeCalled();
  117. $executionContext
  118. ->addViolation('sylius.shipping_method.not_available', Argument::any())
  119. ->shouldNotBeCalled()
  120. ;
  121. $executionContext
  122. ->addViolation('sylius.shipping_method.not_found', ['%code%' => 'SHIPPING_METHOD_CODE'])
  123. ->shouldBeCalled()
  124. ;
  125. $this->validate($command, new ChosenShippingMethodEligibility());
  126. }
  127. function it_adds_violation_if_order_is_not_addressed(
  128. ShipmentRepositoryInterface $shipmentRepository,
  129. ShippingMethodRepositoryInterface $shippingMethodRepository,
  130. ShippingMethodsResolverInterface $shippingMethodsResolver,
  131. ExecutionContextInterface $executionContext,
  132. ShippingMethodInterface $shippingMethod,
  133. ShipmentInterface $shipment,
  134. OrderInterface $order,
  135. AddressInterface $shippingAddress
  136. ): void {
  137. $command = new ChooseShippingMethod('SHIPPING_METHOD_CODE');
  138. $command->setOrderTokenValue('ORDER_TOKEN');
  139. $command->setSubresourceId('123');
  140. $shippingMethodRepository->findOneBy(['code' => 'SHIPPING_METHOD_CODE'])->willReturn($shippingMethod);
  141. $shipmentRepository->find('123')->willReturn($shipment);
  142. $shipment->getOrder()->willReturn($order);
  143. $order->getShippingAddress()->willReturn(null);
  144. $shippingMethodsResolver->getSupportedMethods($shipment)->willReturn([$shippingMethod]);
  145. $executionContext
  146. ->addViolation('sylius.shipping_method.shipping_address_not_found')
  147. ->shouldBeCalled()
  148. ;
  149. $this->validate($command, new ChosenShippingMethodEligibility());
  150. }
  151. function it_throws_an_exception_if_given_shipmnent_is_null(
  152. ShipmentRepositoryInterface $shipmentRepository,
  153. ShippingMethodRepositoryInterface $shippingMethodRepository,
  154. ExecutionContextInterface $executionContext,
  155. ShippingMethodInterface $shippingMethod
  156. ): void {
  157. $command = new ChooseShippingMethod('SHIPPING_METHOD_CODE');
  158. $command->setOrderTokenValue('ORDER_TOKEN');
  159. $command->setSubresourceId('123');
  160. $shippingMethodRepository->findOneBy(['code' => 'SHIPPING_METHOD_CODE'])->willReturn($shippingMethod);
  161. $shipmentRepository->find('123')->willReturn(null);
  162. $executionContext
  163. ->addViolation('sylius.shipping_method.not_available', Argument::any())
  164. ->shouldNotBeCalled()
  165. ;
  166. $this
  167. ->shouldThrow(\InvalidArgumentException::class)
  168. ->during('validate', [$command, new ChosenShippingMethodEligibility()])
  169. ;
  170. }
  171. }