CountryRestrictionTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Integration\CountryRestriction;
  7. use Magento\Checkout\Api\Data\TotalsInformationInterface;
  8. use Magento\Checkout\Api\Data\TotalsInformationInterfaceFactory;
  9. use Magento\Checkout\Api\TotalsInformationManagementInterface;
  10. use Magento\Quote\Api\Data\AddressInterface;
  11. use Magento\Quote\Api\Data\AddressInterfaceFactory;
  12. use Vertex\Tax\Test\Integration\Builder\CartBuilder;
  13. use Vertex\Tax\Test\Integration\Builder\CustomerBuilder;
  14. use Vertex\Tax\Test\Integration\Builder\ProductBuilder;
  15. use Vertex\Tax\Test\Integration\TestCase;
  16. /**
  17. * Ensure that the "use for countries shipping to" configuration setting is respected
  18. */
  19. class CountryRestrictionTest extends TestCase
  20. {
  21. /** @var AddressInterfaceFactory */
  22. private $addressFactory;
  23. /** @var CartBuilder */
  24. private $cartBuilder;
  25. /** @var CustomerBuilder */
  26. private $customerBuilder;
  27. /** @var ProductBuilder */
  28. private $productBuilder;
  29. /** @var TotalsInformationManagementInterface */
  30. private $totalManager;
  31. /** @var TotalsInformationInterfaceFactory */
  32. private $totalsInformationFactory;
  33. /**
  34. * Fetch objects necessary for running our test
  35. */
  36. protected function setUp()
  37. {
  38. parent::setUp();
  39. $this->totalManager = $this->getObject(TotalsInformationManagementInterface::class);
  40. $this->totalsInformationFactory = $this->getObject(TotalsInformationInterfaceFactory::class);
  41. $this->addressFactory = $this->getObject(AddressInterfaceFactory::class);
  42. $this->customerBuilder = $this->getObject(CustomerBuilder::class);
  43. $this->productBuilder = $this->getObject(ProductBuilder::class);
  44. $this->cartBuilder = $this->getObject(CartBuilder::class);
  45. }
  46. /**
  47. * Ensure that the "use for countries shipping to" configuration setting is respected
  48. *
  49. * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
  50. * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
  51. * @magentoConfigFixture default_store tax/vertex_settings/allowed_countries MX
  52. * @magentoDbIsolation enabled
  53. *
  54. * @return void
  55. * @throws \Magento\Framework\Exception\CouldNotSaveException
  56. * @throws \Magento\Framework\Exception\InputException
  57. * @throws \Magento\Framework\Exception\NoSuchEntityException
  58. * @throws \Magento\Framework\Exception\StateException
  59. * @throws \Magento\Framework\Exception\LocalizedException
  60. */
  61. public function testUsesVertexWhenCountryIsEnabled()
  62. {
  63. $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
  64. $soapClient->expects($this->atLeastOnce())
  65. ->method('CalculateTax70')
  66. ->willReturn(new \stdClass());
  67. $this->getSoapFactory()->setSoapClient($soapClient);
  68. $this->runTotalsWithMexicoAddress();
  69. }
  70. /**
  71. * Ensure that the "use for countries shipping to" configuration setting is respected
  72. *
  73. * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
  74. * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
  75. * @magentoConfigFixture default_store tax/vertex_settings/allowed_countries GB
  76. * @magentoDbIsolation enabled
  77. *
  78. * @return void
  79. * @throws \Magento\Framework\Exception\CouldNotSaveException
  80. * @throws \Magento\Framework\Exception\InputException
  81. * @throws \Magento\Framework\Exception\NoSuchEntityException
  82. * @throws \Magento\Framework\Exception\StateException
  83. * @throws \Magento\Framework\Exception\LocalizedException
  84. */
  85. public function testDoesNotUseVertexWhenCountryIsDisabled()
  86. {
  87. $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
  88. $soapClient->expects($this->never())
  89. ->method('CalculateTax70')
  90. ->willReturn(new \stdClass());
  91. $this->getSoapFactory()->setSoapClient($soapClient);
  92. $this->runTotalsWithMexicoAddress();
  93. }
  94. /**
  95. * Create a cart using a Mexico address and call totals against it
  96. *
  97. * Shared functionality for tests
  98. *
  99. * @return void
  100. * @throws \Magento\Framework\Exception\CouldNotSaveException
  101. * @throws \Magento\Framework\Exception\InputException
  102. * @throws \Magento\Framework\Exception\LocalizedException
  103. * @throws \Magento\Framework\Exception\NoSuchEntityException
  104. * @throws \Magento\Framework\Exception\StateException
  105. * @throws \Magento\Framework\Exception\State\InputMismatchException
  106. */
  107. private function runTotalsWithMexicoAddress()
  108. {
  109. $product = $this->productBuilder->createExampleProduct();
  110. $customer = $this->customerBuilder->createExampleCustomer();
  111. $cart = $this->cartBuilder->setItems()
  112. ->addItem($product)
  113. ->create($customer->getId());
  114. $address = $this->createAddress($customer);
  115. /** @var TotalsInformationInterface $totalsInfo */
  116. $totalsInfo = $this->totalsInformationFactory->create();
  117. $totalsInfo->setAddress($address);
  118. $totalsInfo->setShippingCarrierCode('flatrate');
  119. $totalsInfo->setShippingMethodCode('flatrate');
  120. $this->totalManager->calculate($cart->getId(), $totalsInfo);
  121. }
  122. /**
  123. * Create Mexico Customer Address
  124. *
  125. * @param int $customerId
  126. * @return AddressInterface
  127. */
  128. private function createAddress($customerId)
  129. {
  130. /** @var AddressInterface $address */
  131. $address = $this->addressFactory->create();
  132. $address->setCustomerId($customerId);
  133. $address->setStreet(['José María Pino Suárez 30', 'Centro Histórico, Centro']);
  134. $address->setCity('Ciudad de México');
  135. $address->setRegionCode('CDMX');
  136. $address->setRegion('Ciudad de México');
  137. $address->setPostcode('06060');
  138. $address->setCountryId('MX');
  139. $address->setFirstname(CustomerBuilder::EXAMPLE_CUSTOMER_FIRSTNAME);
  140. $address->setLastname(CustomerBuilder::EXAMPLE_CUSTOMER_LASTNAME);
  141. return $address;
  142. }
  143. }