TaxQuoteRequestTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\TaxQuote;
  7. use Vertex\Data\ConfigurationInterface;
  8. use Vertex\Services\Quote\Request;
  9. use Vertex\Services\Quote\Response;
  10. use Vertex\Services\Quote\ResponseInterface;
  11. use Vertex\Tax\Api\QuoteInterface;
  12. use Vertex\Tax\Model\Api\ConfigBuilder;
  13. use Vertex\Tax\Model\Api\Utility\MapperFactoryProxy;
  14. use Vertex\Tax\Model\TaxQuote\CacheKeyGenerator;
  15. use Vertex\Tax\Model\TaxQuote\TaxQuoteRequest;
  16. use Vertex\Tax\Model\TaxRegistry;
  17. use Vertex\Tax\Test\Unit\TestCase;
  18. use Vertex\Utility\VersionDeterminer;
  19. /**
  20. * Test tax quote request functionality.
  21. */
  22. class TaxQuoteRequestTest extends TestCase
  23. {
  24. /** @var \PHPUnit_Framework_MockObject_MockObject|QuoteInterface */
  25. private $quoteMock;
  26. /** @var TaxQuoteRequest */
  27. private $taxQuoteRequest;
  28. /** @var \PHPUnit_Framework_MockObject_MockObject|TaxRegistry */
  29. private $taxRegistryMock;
  30. /**
  31. * Perform test setup.
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $versionDeterminerMock = $this->createMock(VersionDeterminer::class);
  37. $versionDeterminerMock->method('execute')
  38. ->willReturn('60');
  39. $configBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
  40. ->setMethods(['setScopeType','setScopeCode', 'build'])
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $configBuilderMock->method('setScopeType')
  44. ->willReturnSelf();
  45. $configBuilderMock->method('setScopeCode')
  46. ->willReturnSelf();
  47. $configBuilderMock->method('build')
  48. ->willReturn($this->createMock(ConfigurationInterface::class));
  49. $realMapperFactory = new \Vertex\Mapper\MapperFactory();
  50. $mapperFactory = $this->getObject(
  51. MapperFactoryProxy::class,
  52. [
  53. 'versionDeterminer' => $versionDeterminerMock,
  54. 'configBuilder' => $configBuilderMock,
  55. 'factory' => $realMapperFactory,
  56. ]
  57. );
  58. $this->quoteMock = $this->createMock(QuoteInterface::class);
  59. $this->taxRegistryMock = $this->createMock(TaxRegistry::class);
  60. $this->taxQuoteRequest = $this->getObject(
  61. TaxQuoteRequest::class,
  62. [
  63. 'quote' => $this->quoteMock,
  64. 'cacheKeyGenerator' => $this->getObject(
  65. CacheKeyGenerator::class,
  66. ['mapperFactory' => $mapperFactory]
  67. ),
  68. 'taxRegistry' => $this->taxRegistryMock,
  69. 'mapperFactory' => $mapperFactory
  70. ]
  71. );
  72. }
  73. /**
  74. * Test cached tax response handling.
  75. *
  76. * @covers \Vertex\Tax\Model\TaxQuote\TaxQuoteRequest::taxQuote()
  77. */
  78. public function testTaxQuoteRequestCachedResponse()
  79. {
  80. $request = new Request();
  81. $response = new Response();
  82. $this->quoteMock->expects($this->once())
  83. ->method('request')
  84. ->willReturn($response);
  85. $rawResponse = new \stdClass();
  86. $rawResponse->QuotationResponse = new \stdClass();
  87. $this->taxRegistryMock->expects($this->exactly(2))
  88. ->method('lookup')
  89. ->willReturnOnConsecutiveCalls(null, $rawResponse);
  90. // Vertex sendApiRequest should be called once, testing uncached
  91. $result = $this->taxQuoteRequest->taxQuote($request);
  92. $this->assertEquals($response, $result);
  93. // It should not be called again, testing cached
  94. $result = $this->taxQuoteRequest->taxQuote($request);
  95. $this->assertInstanceOf(ResponseInterface::class, $result);
  96. }
  97. }