CalculatorTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @author Mediotype Developement <diveinto@mediotype.com>
  4. * @copyright 2018 Mediotype. All rights reserved.
  5. */
  6. namespace Vertex\Tax\Test\Integration\Model;
  7. use Magento\Checkout\Api\Data\TotalsInformationInterface;
  8. use Magento\Checkout\Api\TotalsInformationManagementInterface;
  9. use Magento\Framework\Message\Collection;
  10. use Magento\Framework\Message\ManagerInterface;
  11. use Magento\Framework\Message\MessageInterface;
  12. use Vertex\Tax\Test\Integration\Scenarios\QuoteWithPaymentAndInvalidAddress;
  13. use Vertex\Tax\Test\Integration\TestCase;
  14. /**
  15. * Ensure that Calculator works properly
  16. */
  17. class CalculatorTest extends TestCase
  18. {
  19. /** @var QuoteWithPaymentAndInvalidAddress */
  20. private $quoteWithPaymentAndInvalidAddress;
  21. /** @inheritdoc */
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $this->quoteWithPaymentAndInvalidAddress = $this->getObject(
  26. QuoteWithPaymentAndInvalidAddress::class
  27. );
  28. }
  29. /**
  30. * Test if vertex error message is shown on frontend
  31. *
  32. * @magentoAppArea frontend
  33. * @magentoDbIsolation enabled
  34. * @magentoAppIsolation enabled
  35. * @magentoCache all disabled
  36. * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
  37. * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
  38. */
  39. public function testShowErrorMessageToCustomer()
  40. {
  41. $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
  42. $soapClient->expects($this->atLeastOnce())
  43. ->method('CalculateTax70')
  44. ->with(
  45. $this->callback(
  46. function (\stdClass $request) {
  47. $destination = $request->QuotationRequest->Customer->Destination;
  48. if ($destination->PostalCode !== QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE) {
  49. $this->fail(
  50. 'Post code is valid, please use \''
  51. . QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE . '\''
  52. );
  53. return false;
  54. }
  55. $this->assertEquals(
  56. QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE,
  57. $destination->PostalCode
  58. );
  59. return true;
  60. }
  61. )
  62. )
  63. ->willReturn(new \stdClass());
  64. $this->getSoapFactory()->setSoapClient($soapClient);
  65. $cart = $this->quoteWithPaymentAndInvalidAddress->create('vertex_cart_with_invalid_address');
  66. /** @var TotalsInformationInterface $totalsInfo */
  67. $totalsInfo = $this->createObject(TotalsInformationInterface::class);
  68. $totalsInfo->setAddress($cart->getBillingAddress());
  69. $totalsInfo->setShippingCarrierCode('flatrate');
  70. $totalsInfo->setShippingMethodCode('flatrate');
  71. /** @var TotalsInformationManagementInterface $totalsManagement */
  72. $totalsManagement = $this->getObject(TotalsInformationManagementInterface::class);
  73. $totals = $totalsManagement->calculate($cart->getId(), $totalsInfo);
  74. $messages = $totals->getTotalSegments()['tax']->getExtensionAttributes()->getVertexTaxCalculationMessages();
  75. $this->assertInternalType('array', $messages);
  76. $this->assertContains(
  77. 'Unable to calculate taxes. This could be caused by an invalid address provided in checkout.',
  78. $messages
  79. );
  80. }
  81. /**
  82. * Test if vertex error message is shown on admin
  83. *
  84. * @magentoAppArea adminhtml
  85. * @magentoDbIsolation enabled
  86. * @magentoAppIsolation enabled
  87. * @magentoCache all disabled
  88. * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
  89. * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
  90. */
  91. public function testShowErrorMessageToAdminUser()
  92. {
  93. $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
  94. $soapClient->expects($this->atLeastOnce())
  95. ->method('CalculateTax70')
  96. ->with(
  97. $this->callback(
  98. function (\stdClass $request) {
  99. $destination = $request->QuotationRequest->Customer->Destination;
  100. if ($destination->PostalCode !== QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE) {
  101. $this->fail(
  102. 'Post code is valid, please use \''
  103. . QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE . '\''
  104. );
  105. return false;
  106. }
  107. $this->assertEquals(
  108. QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE,
  109. $destination->PostalCode
  110. );
  111. return true;
  112. }
  113. )
  114. )
  115. ->willReturn(new \stdClass());
  116. $this->getSoapFactory()->setSoapClient($soapClient);
  117. $cart = $this->quoteWithPaymentAndInvalidAddress->create('vertex_cart_with_invalid_address');
  118. /** @var TotalsInformationInterface $totalsInfo */
  119. $totalsInfo = $this->createObject(TotalsInformationInterface::class);
  120. $totalsInfo->setAddress($cart->getBillingAddress());
  121. $totalsInfo->setShippingCarrierCode('flatrate');
  122. $totalsInfo->setShippingMethodCode('flatrate');
  123. /** @var TotalsInformationManagementInterface $totalsManagement */
  124. $totalsManagement = $this->getObject(TotalsInformationManagementInterface::class);
  125. $totalsManagement->calculate($cart->getId(), $totalsInfo);
  126. /** @var ManagerInterface $messageManager */
  127. $messageManager = $this->getObject(ManagerInterface::class);
  128. $messages = $messageManager->getMessages(true);
  129. $this->assertInstanceOf(Collection::class, $messages);
  130. $this->assertContainsOnlyInstancesOf(MessageInterface::class, $messages->getItems());
  131. $this->assertEquals(1, $messages->getCount());
  132. /** @var MessageInterface $message */
  133. $message = current($messages->getItems());
  134. $this->assertContains(
  135. 'Unable to calculate taxes. This could be caused by an invalid address provided in checkout.',
  136. $message->getText()
  137. );
  138. }
  139. }