TaxRegistryTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\DataObjectFactory;
  9. use Vertex\Data\LineItem;
  10. use Vertex\Data\Tax;
  11. use Vertex\Tax\Model\TaxQuote\TaxQuoteResponse;
  12. use Vertex\Tax\Model\TaxRegistry;
  13. use Vertex\Tax\Model\TaxRegistry\StorageInterface;
  14. use Vertex\Tax\Test\Unit\TestCase;
  15. /**
  16. * Test Vertex tax registry functions.
  17. */
  18. class TaxRegistryTest extends TestCase
  19. {
  20. /** @var \PHPUnit_Framework_MockObject_MockObject|DataObjectFactory */
  21. private $dataObjectFactoryMock;
  22. /** @var \PHPUnit_Framework_MockObject_MockObject|StorageInterface */
  23. private $storageInterfaceMock;
  24. /** @var TaxQuoteResponse */
  25. private $taxDataObject;
  26. /** @var TaxRegistry */
  27. private $taxRegistry;
  28. /**
  29. * Perform test setup.
  30. */
  31. protected function setUp()
  32. {
  33. parent::setUp();
  34. $this->storageInterfaceMock = $this->createMock(StorageInterface::class);
  35. $dataObject = $this->getObject(DataObject::class);
  36. $this->dataObjectFactoryMock = $this->createMock(DataObjectFactory::class);
  37. $this->dataObjectFactoryMock->method('create')
  38. ->willReturn($dataObject);
  39. $this->taxDataObject = $this->getObject(
  40. TaxQuoteResponse::class,
  41. ['dataObjectFactory' => $this->dataObjectFactoryMock]
  42. );
  43. $this->taxRegistry = $this->getObject(
  44. TaxRegistry::class,
  45. [
  46. 'dataObjectFactory' => $this->dataObjectFactoryMock,
  47. 'storage' => $this->storageInterfaceMock,
  48. ]
  49. );
  50. }
  51. /**
  52. * Test that tax storage can hold and return any scalar type with type integrity.
  53. *
  54. * @param array $data
  55. * @dataProvider provideScalarDataForRegistration
  56. * @covers \Vertex\Tax\Model\TaxRegistry::register()
  57. * @covers \Vertex\Tax\Model\TaxRegistry::lookup()
  58. */
  59. public function testScalarDataRegistration(array $data)
  60. {
  61. $expectedValues = array_values($data);
  62. $this->storageInterfaceMock->method('get')
  63. ->willReturnOnConsecutiveCalls(...$expectedValues);
  64. foreach ($data as $type => $expectedValue) {
  65. $storageKey = 'test_type_' . $type;
  66. $this->taxRegistry->register($storageKey, $expectedValue);
  67. $actualResult = $this->taxRegistry->lookup($storageKey);
  68. $this->assertEquals(gettype($actualResult), $type);
  69. $this->assertEquals($expectedValue, $actualResult);
  70. }
  71. }
  72. /**
  73. * Test that tax storage can hold and return array data with type integrity.
  74. *
  75. * @param array $data
  76. * @dataProvider provideArrayDataForRegistration
  77. * @covers \Vertex\Tax\Model\TaxRegistry::register()
  78. * @covers \Vertex\Tax\Model\TaxRegistry::lookup()
  79. */
  80. public function testArrayDataRegistration(array $data)
  81. {
  82. $this->storageInterfaceMock->method('get')
  83. ->willReturn($data);
  84. $storageKey = 'test_type_array';
  85. $this->taxRegistry->register($storageKey, $data);
  86. $actualResult = $this->taxRegistry->lookup($storageKey);
  87. $this->assertEquals(gettype($actualResult), 'array');
  88. $this->assertEquals(serialize($data), serialize($actualResult));
  89. }
  90. /**
  91. * Test that presence detection of calculated tax data in the registry responds correctly.
  92. *
  93. * @param array $data
  94. * @dataProvider provideTaxDataForRegistration
  95. * @covers \Vertex\Tax\Model\TaxRegistry::hasTaxes()
  96. */
  97. public function testTaxDataPresenceCheck(array $data)
  98. {
  99. // Calculated tax lookup should use internal object storage, not StorageInterface
  100. $this->storageInterfaceMock->expects($this->never())
  101. ->method('get');
  102. $this->taxDataObject->prepareQuoteTaxedItems($data);
  103. $this->taxRegistry->registerTaxes($this->taxDataObject);
  104. $this->assertTrue($this->taxRegistry->hasTaxes());
  105. }
  106. /**
  107. * Test that the lookupTaxes interface can hold and return calculated tax data with integrity.
  108. *
  109. * @param array $data
  110. * @dataProvider provideTaxDataForRegistration
  111. * @covers \Vertex\Tax\Model\TaxRegistry::lookupTaxes()
  112. */
  113. public function testTaxDataLookupInterface(array $data)
  114. {
  115. // Calculated tax lookup should use internal object storage, not StorageInterface
  116. $this->storageInterfaceMock->expects($this->never())
  117. ->method('get');
  118. $this->taxDataObject->prepareQuoteTaxedItems($data);
  119. $this->taxRegistry->registerTaxes($this->taxDataObject);
  120. $actualResult = $this->taxRegistry->lookupTaxes();
  121. $this->assertEquals(gettype($actualResult), 'array');
  122. foreach ($actualResult as $resultItem) {
  123. $this->assertInstanceOf(DataObject::class, $resultItem);
  124. }
  125. }
  126. /**
  127. * Test that the registerTaxes interface can hold and return calculated tax data with integrity.
  128. *
  129. * @param array $data
  130. * @dataProvider provideTaxDataForRegistration
  131. * @covers \Vertex\Tax\Model\TaxRegistry::lookupTaxes()
  132. */
  133. public function testTaxDataRegistrationInterface(array $data)
  134. {
  135. // Calculated tax lookup should use internal object storage, not StorageInterface
  136. $this->storageInterfaceMock->expects($this->never())
  137. ->method('set');
  138. $this->taxDataObject->prepareQuoteTaxedItems($data);
  139. $actualResult = $this->taxRegistry->registerTaxes($this->taxDataObject);
  140. $this->assertTrue($actualResult);
  141. $this->taxDataObject->prepareQuoteTaxedItems([]);
  142. $actualResult = $this->taxRegistry->registerTaxes($this->taxDataObject);
  143. $this->assertFalse($actualResult);
  144. }
  145. /**
  146. * Test that the registerError interface can hold and return data with integrity.
  147. *
  148. * @covers \Vertex\Tax\Model\TaxRegistry::registerError()
  149. */
  150. public function testErrorRegistrationInterface()
  151. {
  152. $error = 'Unable to calculate taxes. This could be caused by an invalid address provided in checkout.';
  153. $this->storageInterfaceMock->method('get')
  154. ->with(TaxRegistry::KEY_ERROR_GENERIC)
  155. ->willReturn($error);
  156. $this->taxRegistry->registerError($error, TaxRegistry::KEY_ERROR_GENERIC);
  157. $actualResult = $this->taxRegistry->lookupError(TaxRegistry::KEY_ERROR_GENERIC);
  158. $this->assertEquals($error, $actualResult);
  159. $this->taxRegistry->registerError($error, 'other_error_type');
  160. $actualResult = $this->taxRegistry->lookupError('other_error_type');
  161. $this->assertEquals($error, $actualResult);
  162. $this->taxRegistry->registerError('unexpected error message', 'another_error_type');
  163. $actualResult = $this->taxRegistry->lookupError('another_error_type');
  164. $this->assertNotNull($actualResult);
  165. $this->assertNotEquals($error, $actualResult);
  166. }
  167. /**
  168. * Test that data can be removed from registry.
  169. *
  170. * @covers \Vertex\Tax\Model\TaxRegistry::unregister()
  171. */
  172. public function testDataDeregistration()
  173. {
  174. $expectedResult = true;
  175. $storageKey = 'test_key';
  176. $testValue = 'test';
  177. $this->storageInterfaceMock->method('unsetData')
  178. ->willReturn($expectedResult);
  179. $this->taxRegistry->register($storageKey, $testValue);
  180. $actualResult = $this->taxRegistry->unregister($storageKey);
  181. $this->assertEquals($expectedResult, $actualResult);
  182. }
  183. /**
  184. * Test that the unregisterTaxes interface can remove calculated tax data from the registry.
  185. *
  186. * @param array $data
  187. * @dataProvider provideTaxDataForRegistration
  188. * @covers \Vertex\Tax\Model\TaxRegistry::unregister()
  189. */
  190. public function testTaxDataDeregistrationInterface(array $data)
  191. {
  192. $this->taxDataObject->prepareQuoteTaxedItems($data);
  193. $this->taxRegistry->registerTaxes($this->taxDataObject);
  194. $actualResult = $this->taxRegistry->unregisterTaxes();
  195. $this->assertTrue($actualResult);
  196. }
  197. /**
  198. * Data provider of scalar data for tax registry storage tests.
  199. *
  200. * @return array
  201. */
  202. public function provideScalarDataForRegistration()
  203. {
  204. return [
  205. 'data' => [
  206. [
  207. 'boolean' => true,
  208. 'integer' => 99,
  209. 'double' => 110.50,
  210. 'string' => 'sample data',
  211. ],
  212. ],
  213. ];
  214. }
  215. /**
  216. * Data provider of array data for tax registry storage tests.
  217. *
  218. * @return array
  219. */
  220. public function provideArrayDataForRegistration()
  221. {
  222. return [
  223. 'data' => [
  224. [
  225. 'id' => 1,
  226. 'value' => 'test',
  227. ],
  228. ],
  229. ];
  230. }
  231. /**
  232. * Data provider of mock tax response for tax registry lookup tests.
  233. */
  234. public function provideTaxDataForRegistration()
  235. {
  236. $lineItem1 = new LineItem();
  237. $lineItem1->setLineItemId(1);
  238. $lineItem1->setProductCode('PRODUCT-LINE-1');
  239. $lineItem1->setProductClass('Taxable Goods');
  240. $lineItem1->setQuantity(1);
  241. $lineItem1->setUnitPrice(2.50);
  242. $lineItem1->setTotalTax(2.50);
  243. $lineItem1Tax1 = new Tax();
  244. $lineItem1Tax1->setEffectiveRate(1.0);
  245. $lineItem1Tax2 = new Tax();
  246. $lineItem1Tax2->setEffectiveRate(2.0);
  247. $lineItem1->setTaxes([$lineItem1Tax1, $lineItem1Tax2]);
  248. $lineItem2 = new LineItem();
  249. $lineItem2->setTaxes([$lineItem1Tax1]);
  250. $lineItem2->setLineItemId(2);
  251. $lineItem2->setProductCode('PRODUCT-LINE-2');
  252. $lineItem2->setProductClass('None');
  253. $lineItem2->setQuantity(2);
  254. $lineItem2->setUnitPrice(1.50);
  255. $lineItem2->setTotalTax(1.50);
  256. return [
  257. [
  258. 'data' => [
  259. $lineItem1,
  260. $lineItem2
  261. ],
  262. ],
  263. ];
  264. }
  265. }