CacheStorageTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\TaxRegistry;
  7. use Magento\Framework\App\Cache\StateInterface;
  8. use Magento\Framework\Cache\FrontendInterface;
  9. use Vertex\Tax\Model\Cache\Serializer;
  10. use Vertex\Tax\Model\TaxRegistry\CacheStorage;
  11. use Vertex\Tax\Test\Unit\TestCase;
  12. /**
  13. * Test Vertex tax registry cache storage.
  14. */
  15. class CacheStorageTest extends TestCase
  16. {
  17. /** @var \PHPUnit_Framework_MockObject_MockObject|FrontendInterface */
  18. private $cacheFrontendMock;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject|StateInterface */
  20. private $cacheStateMock;
  21. /** @var CacheStorage */
  22. private $cacheStorage;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject|Serializer */
  24. private $serializerMock;
  25. /**
  26. * Perform test setup.
  27. */
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->cacheFrontendMock = $this->createMock(FrontendInterface::class);
  32. $this->cacheStateMock = $this->createMock(StateInterface::class);
  33. $this->serializerMock = $this->createMock(Serializer::class);
  34. $this->cacheStateMock->method('isEnabled')
  35. ->willReturn(true);
  36. $this->cacheStorage = $this->getObject(
  37. CacheStorage::class,
  38. [
  39. 'cache' => $this->cacheFrontendMock,
  40. 'cacheState' => $this->cacheStateMock,
  41. 'serializer' => $this->serializerMock,
  42. ]
  43. );
  44. }
  45. /**
  46. * Test that the absence of cached data can be handled.
  47. *
  48. * @covers \Vertex\Tax\Model\TaxRegistry\CacheStorage::get()
  49. */
  50. public function testMissingCacheEntryRetrievalResolvesToNull()
  51. {
  52. $this->cacheFrontendMock->method('load')
  53. ->willReturn(false);
  54. $this->serializerMock->expects($this->never())
  55. ->method('unserialize');
  56. $this->assertNull(
  57. $this->cacheStorage->get('non_existent_key')
  58. );
  59. }
  60. /**
  61. * Test that the absence of cached data will return a specified default value.
  62. *
  63. * @covers \Vertex\Tax\Model\TaxRegistry\CacheStorage::get()
  64. */
  65. public function testMissingCacheEntryRetrievalResolvesToFallback()
  66. {
  67. $expectedValue = 'default_value';
  68. $this->cacheFrontendMock->method('load')
  69. ->willReturn(false);
  70. $actualValue = $this->cacheStorage->get('non_existent_key', $expectedValue);
  71. $this->assertEquals($expectedValue, $actualValue);
  72. }
  73. /**
  74. * Test that scalar data can be retrieved from storage with type integrity.
  75. *
  76. * @param array $data
  77. * @dataProvider provideScalarData
  78. * @covers \Vertex\Tax\Model\TaxRegistry\CacheStorage::get()
  79. */
  80. public function testGetScalarData(array $data)
  81. {
  82. $expectedValues = array_values($data);
  83. $this->cacheFrontendMock->expects($this->exactly(count($expectedValues)))
  84. ->method('load');
  85. $this->serializerMock->expects($this->exactly(count($expectedValues)))
  86. ->method('unserialize')
  87. ->willReturnOnConsecutiveCalls(...$expectedValues);
  88. foreach ($data as $type => $expectedValue) {
  89. $storageKey = 'test_type_' . $type;
  90. $actualResult = $this->cacheStorage->get($storageKey);
  91. $this->assertEquals(gettype($actualResult), $type);
  92. $this->assertEquals($expectedValue, $actualResult);
  93. }
  94. }
  95. /**
  96. * Test that array data can be retrieved from storage with type integrity.
  97. *
  98. * @param array $data
  99. * @dataProvider provideScalarData
  100. * @covers \Vertex\Tax\Model\TaxRegistry\CacheStorage::get()
  101. */
  102. public function testGetArrayData(array $data)
  103. {
  104. $this->cacheFrontendMock->method('load')
  105. ->willReturn($data);
  106. $this->serializerMock->expects($this->once())
  107. ->method('unserialize')
  108. ->willReturn($data);
  109. $storageKey = 'test_type_array';
  110. $actualResult = $this->cacheStorage->get($storageKey);
  111. $this->assertEquals(gettype($actualResult), 'array');
  112. $this->assertEquals(serialize($data), serialize($actualResult));
  113. }
  114. /**
  115. * Test that scalar data can be written to storage with type integrity.
  116. *
  117. * @param array $data
  118. * @dataProvider provideScalarData
  119. * @covers \Vertex\Tax\Model\TaxRegistry\CacheStorage::set()
  120. */
  121. public function testSetScalarData(array $data)
  122. {
  123. $expectedValues = array_values($data);
  124. $this->cacheFrontendMock->method('save')
  125. ->willReturn(true);
  126. $this->serializerMock->expects($this->exactly(count($expectedValues)))
  127. ->method('serialize');
  128. foreach ($data as $type => $expectedValue) {
  129. $storageKey = 'test_type_' . $type;
  130. $this->assertTrue(
  131. $this->cacheStorage->set($storageKey, $expectedValue)
  132. );
  133. }
  134. }
  135. /**
  136. * Test that array data can be written to storage with type integrity.
  137. *
  138. * @param array $data
  139. * @dataProvider provideScalarData
  140. * @covers \Vertex\Tax\Model\TaxRegistry\CacheStorage::set()
  141. */
  142. public function testSetArrayData(array $data)
  143. {
  144. $this->cacheFrontendMock->method('save')
  145. ->willReturn(true);
  146. $this->serializerMock->expects($this->once())
  147. ->method('serialize')
  148. ->willReturn(serialize($data));
  149. $storageKey = 'test_type_array';
  150. $this->assertTrue(
  151. $this->cacheStorage->set($storageKey, $data)
  152. );
  153. }
  154. /**
  155. * Data provider of scalar data for tax registry storage tests.
  156. *
  157. * @return array
  158. */
  159. public function provideScalarData()
  160. {
  161. return [
  162. 'data' => [
  163. [
  164. 'boolean' => false,
  165. 'integer' => 255,
  166. 'double' => 515.11,
  167. 'string' => 'test string type',
  168. ],
  169. ],
  170. ];
  171. }
  172. /**
  173. * Data provider of array data for tax registry storage tests.
  174. *
  175. * @return array
  176. */
  177. public function provideArrayData()
  178. {
  179. return [
  180. 'data' => [
  181. [
  182. 'id' => 918,
  183. 'key' => 'identifier',
  184. 'value' => 'test value',
  185. ],
  186. ],
  187. ];
  188. }
  189. }