LogRequestTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Unit\Model\Vertex;
  7. use Magento\Framework\Stdlib\DateTime\DateTime;
  8. use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
  9. use Vertex\Tax\Api\Data\LogEntryInterface;
  10. use Vertex\Tax\Api\Data\LogEntryInterfaceFactory;
  11. use Vertex\Tax\Api\LogEntryRepositoryInterface;
  12. use Vertex\Tax\Model\ApiClient;
  13. use Vertex\Tax\Model\DomDocumentFactory;
  14. use Vertex\Tax\Model\RequestLogger;
  15. use Vertex\Tax\Test\Unit\TestCase;
  16. class LogRequestTest extends TestCase
  17. {
  18. const DATE = '1991-08-06 12:00:00';
  19. const OBJECT_ID = '42';
  20. const REQUEST_XML = '<request/>';
  21. const REQUEST_XML_FORMATTED = "<?xml version=\"1.0\"?>\n<request/>\n";
  22. const RESPONSE_EXCEPTIONTYPE = 'SOME EXCEPTION';
  23. const RESPONSE_LOOKUP_RESULT = 'RESULT OK';
  24. const RESPONSE_SUBTOTAL = '4.20';
  25. const RESPONSE_TOTAL = '5.25';
  26. const RESPONSE_TOTAL_TAX = '1.05';
  27. const RESPONSE_XML = '<QuotationResponse><TotalTax>1.05</TotalTax><Total>5.25</Total><SubTotal>4.20</SubTotal>'
  28. . '<Status lookupResult="RESULT OK"/></QuotationResponse>';
  29. const RESPONSE_XML_EXCEPTIONTYPE = '<response><exceptionType>SOME EXCEPTION</exceptionType></response>';
  30. const RESPONSE_XML_FORMATTED = <<<XML
  31. <?xml version="1.0"?>
  32. <QuotationResponse>
  33. <TotalTax>1.05</TotalTax>
  34. <Total>5.25</Total>
  35. <SubTotal>4.20</SubTotal>
  36. <Status lookupResult="RESULT OK"/>
  37. </QuotationResponse>\n
  38. XML;
  39. const SOURCE_PATH = 'source_path';
  40. const TAX_AREA_ID = '3.14';
  41. private $dateTimeFactory;
  42. public function setUp()
  43. {
  44. parent::setUp();
  45. $dateTimeMock = $this->createPartialMock(DateTime::class, ['date']);
  46. $dateTimeMock->expects($this->any())
  47. ->method('date')
  48. ->willReturn(static::DATE);
  49. $this->dateTimeFactory = $this->createMock(DateTimeFactory::class);
  50. $this->dateTimeFactory->expects($this->any())
  51. ->method('create')
  52. ->willReturn($dateTimeMock);
  53. }
  54. public function testHappyLogRequest()
  55. {
  56. $type = 'third_type';
  57. $logEntry = $this->createMock(LogEntryInterface::class);
  58. // Test that factory is used to generate LogEntry
  59. $logEntryFactory = $this->createMock(LogEntryInterfaceFactory::class);
  60. $logEntryFactory->expects($this->once())
  61. ->method('create')
  62. ->willReturn($logEntry);
  63. // Test that repository is used to save LogEntry
  64. $logEntryRepository = $this->createMock(LogEntryRepositoryInterface::class);
  65. $logEntryRepository->expects($this->once())
  66. ->method('save')
  67. ->with($logEntry);
  68. // Test that Type is properly set as passed in parameter
  69. $logEntry->expects($this->once())
  70. ->method('setType')
  71. ->with($type)
  72. ->willReturnSelf();
  73. // Test that Date is properly set as the date from the DateTimeFactory's DateTime
  74. $logEntry->expects($this->once())
  75. ->method('setDate')
  76. ->with(static::DATE)
  77. ->willReturnSelf();
  78. // Test that Request XML is formatted
  79. $logEntry->expects($this->once())
  80. ->method('setRequestXml')
  81. ->with(static::REQUEST_XML_FORMATTED);
  82. // Test that Response XML is formatted
  83. $logEntry->expects($this->once())
  84. ->method('setResponseXml')
  85. ->with(static::RESPONSE_XML_FORMATTED);
  86. // Test that Total Tax is set from XML
  87. $logEntry->expects($this->once())
  88. ->method('setTotalTax')
  89. ->with(static::RESPONSE_TOTAL_TAX);
  90. // Test that total is set from XML
  91. $logEntry->expects($this->once())
  92. ->method('setTotal')
  93. ->with(static::RESPONSE_TOTAL);
  94. // Test that subtotal is set from XML
  95. $logEntry->expects($this->once())
  96. ->method('setSubTotal')
  97. ->with(static::RESPONSE_SUBTOTAL);
  98. // Test that lookup result is set from XML
  99. $logEntry->expects($this->once())
  100. ->method('setLookupResult')
  101. ->with(static::RESPONSE_LOOKUP_RESULT);
  102. $requestLogger = $this->getObject(
  103. RequestLogger::class,
  104. [
  105. 'repository' => $logEntryRepository,
  106. 'logEntryFactory' => $logEntryFactory,
  107. 'dateTime' => $this->dateTimeFactory->create(),
  108. 'documentFactory' => $this->getObject(DomDocumentFactory::class),
  109. ]
  110. );
  111. $vertex = $this->getObject(
  112. ApiClient::class,
  113. [
  114. 'requestLogger' => $requestLogger,
  115. ]
  116. );
  117. $this->invokeInaccessibleMethod(
  118. $vertex,
  119. 'logRequest',
  120. $type,
  121. static::REQUEST_XML,
  122. static::RESPONSE_XML,
  123. ['_' => static::RESPONSE_TOTAL_TAX],
  124. static::TAX_AREA_ID
  125. );
  126. }
  127. }