BaseCurrencyCodeSentToVertexTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\ApiValidation;
  7. use Magento\Checkout\Api\Data\TotalsInformationInterface;
  8. use Magento\Checkout\Api\Data\TotalsInformationInterfaceFactory;
  9. use Magento\Checkout\Api\TotalsInformationManagementInterface;
  10. use Magento\Customer\Api\Data\GroupInterfaceFactory;
  11. use Magento\Customer\Api\GroupRepositoryInterface;
  12. use Magento\Quote\Api\Data\AddressInterface;
  13. use Magento\Quote\Api\Data\AddressInterfaceFactory;
  14. use Vertex\Tax\Test\Integration\Builder\CartBuilder;
  15. use Vertex\Tax\Test\Integration\Builder\CartBuilderFactory;
  16. use Vertex\Tax\Test\Integration\Builder\CustomerBuilder;
  17. use Vertex\Tax\Test\Integration\Builder\ProductBuilder;
  18. use Vertex\Tax\Test\Integration\Builder\ProductBuilderFactory;
  19. use Vertex\Tax\Test\Integration\Builder\TaxClassBuilder;
  20. use Vertex\Tax\Test\Integration\TestCase;
  21. /**
  22. * Ensure that when totals are collected the correct currency is sent to Vertex
  23. *
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class BaseCurrencyCodeSentToVertexTest extends TestCase
  27. {
  28. const TAX_CLASS_NAME = 'Testable Tax Class';
  29. /** @var AddressInterfaceFactory */
  30. private $addressFactory;
  31. /** @var CartBuilder */
  32. private $cartBuilder;
  33. /** @var CustomerBuilder */
  34. private $customerBuilder;
  35. /** @var GroupInterfaceFactory */
  36. private $customerGroupFactory;
  37. /** @var GroupRepositoryInterface */
  38. private $customerGroupRepository;
  39. /** @var ProductBuilder */
  40. private $productBuilder;
  41. /** @var TaxClassBuilder */
  42. private $taxClassBuilder;
  43. /** @var TotalsInformationManagementInterface */
  44. private $totalManager;
  45. /** @var TotalsInformationInterfaceFactory */
  46. private $totalsInformationFactory;
  47. /**
  48. * Fetch objects necessary for running our test
  49. */
  50. protected function setUp()
  51. {
  52. parent::setUp();
  53. $this->totalManager = $this->getObject(TotalsInformationManagementInterface::class);
  54. $this->totalsInformationFactory = $this->getObject(TotalsInformationInterfaceFactory::class);
  55. $this->addressFactory = $this->getObject(AddressInterfaceFactory::class);
  56. $this->customerGroupFactory = $this->getObject(GroupInterfaceFactory::class);
  57. $this->customerGroupRepository = $this->getObject(GroupRepositoryInterface::class);
  58. $this->customerBuilder = $this->getObject(CustomerBuilder::class);
  59. $this->productBuilder = $this->getObject(ProductBuilderFactory::class)->create();
  60. $this->taxClassBuilder = $this->getObject(TaxClassBuilder::class);
  61. $this->cartBuilder = $this->getObject(CartBuilderFactory::class)->create();
  62. }
  63. /**
  64. * Ensure that when totals are collected our tax request being sent to Vertex sends a base currency of CNY
  65. *
  66. * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
  67. * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
  68. * @magentoConfigFixture current_store currency/options/base EUR
  69. * @magentoConfigFixture current_store currency/options/default EUR
  70. * @magentoConfigFixture current_store currency/options/allow EUR
  71. * @magentoConfigFixture current_store catalog/price/scope 1
  72. * @magentoDbIsolation enabled
  73. * @magentoCache all disabled
  74. *
  75. * @throws \Magento\Framework\Exception\CouldNotSaveException
  76. * @throws \Magento\Framework\Exception\InputException
  77. * @throws \Magento\Framework\Exception\LocalizedException
  78. * @throws \Magento\Framework\Exception\NoSuchEntityException
  79. * @throws \Magento\Framework\Exception\StateException
  80. * @return void
  81. */
  82. public function testBaseCurrencyOfEUR()
  83. {
  84. $product = $this->productBuilder->createExampleProduct();
  85. $customer = $this->customerBuilder->createExampleCustomer();
  86. $cart = $this->cartBuilder->setItems([])
  87. ->addItem($product)
  88. ->create($customer->getId());
  89. $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
  90. $soapClient->expects($this->atLeastOnce())
  91. ->method('CalculateTax70')
  92. ->with(
  93. $this->callback(
  94. function (\stdClass $request) {
  95. $currency = $request->QuotationRequest->Currency;
  96. if (empty($currency) || empty($currency->isoCurrencyCodeAlpha)) {
  97. $this->fail('Currency code not set');
  98. return false;
  99. }
  100. $this->assertEquals('EUR', $request->QuotationRequest->Currency->isoCurrencyCodeAlpha);
  101. return true;
  102. }
  103. )
  104. )
  105. ->willReturn(new \stdClass());
  106. $this->getSoapFactory()->setSoapClient($soapClient);
  107. $address = $this->createShippingAddress($customer->getId());
  108. /** @var TotalsInformationInterface $totalsInfo */
  109. $totalsInfo = $this->totalsInformationFactory->create();
  110. $totalsInfo->setAddress($address);
  111. $totalsInfo->setShippingCarrierCode('flatrate');
  112. $totalsInfo->setShippingMethodCode('flatrate');
  113. $this->totalManager->calculate($cart->getId(), $totalsInfo);
  114. }
  115. /**
  116. * Ensure that when totals are collected our tax request being sent to Vertex sends a base currency of USD
  117. *
  118. * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
  119. * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
  120. * @magentoConfigFixture current_store currency/options/base USD
  121. * @magentoConfigFixture current_store currency/options/default USD
  122. * @magentoConfigFixture current_store catalog/price/scope 1
  123. * @magentoDbIsolation enabled
  124. * @magentoCache all disabled
  125. *
  126. * @throws \Magento\Framework\Exception\CouldNotSaveException
  127. * @throws \Magento\Framework\Exception\InputException
  128. * @throws \Magento\Framework\Exception\LocalizedException
  129. * @throws \Magento\Framework\Exception\NoSuchEntityException
  130. * @throws \Magento\Framework\Exception\StateException
  131. * @return void
  132. */
  133. public function testBaseCurrencyOfUSD()
  134. {
  135. $product = $this->productBuilder->createExampleProduct();
  136. $customer = $this->customerBuilder->createExampleCustomer();
  137. $cart = $this->cartBuilder->setItems([])
  138. ->addItem($product)
  139. ->create($customer->getId());
  140. $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
  141. $soapClient->expects($this->atLeastOnce())
  142. ->method('CalculateTax70')
  143. ->with(
  144. $this->callback(
  145. function (\stdClass $request) {
  146. $currency = $request->QuotationRequest->Currency;
  147. if (empty($currency) || empty($currency->isoCurrencyCodeAlpha)) {
  148. $this->fail('Currency code not set');
  149. return false;
  150. }
  151. $this->assertEquals('USD', $request->QuotationRequest->Currency->isoCurrencyCodeAlpha);
  152. return true;
  153. }
  154. )
  155. )
  156. ->willReturn(new \stdClass());
  157. $this->getSoapFactory()->setSoapClient($soapClient);
  158. $address = $this->createShippingAddress($customer->getId());
  159. /** @var TotalsInformationInterface $totalsInfo */
  160. $totalsInfo = $this->totalsInformationFactory->create();
  161. $totalsInfo->setAddress($address);
  162. $totalsInfo->setShippingCarrierCode('flatrate');
  163. $totalsInfo->setShippingMethodCode('flatrate');
  164. $this->totalManager->calculate($cart->getId(), $totalsInfo);
  165. }
  166. /**
  167. * Create a shipping address for our order
  168. *
  169. * @param int|null $customerId
  170. * @return AddressInterface
  171. */
  172. private function createShippingAddress($customerId = null)
  173. {
  174. /** @var AddressInterface $address */
  175. $address = $this->addressFactory->create();
  176. $address->setCustomerId($customerId);
  177. $address->setCity('West Chester');
  178. $address->setCountryId('US');
  179. $address->setFirstname('John');
  180. $address->setLastname('Doe');
  181. $address->setPostcode('19382');
  182. $address->setRegion('Pennsylvania');
  183. $address->setRegionCode('PA');
  184. $address->setRegionId(51);
  185. $address->setStreet(['233 West Gay St']);
  186. $address->setTelephone('1234567890');
  187. return $address;
  188. }
  189. }