AttributeMetadataCacheTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Metadata;
  7. use Magento\Config\App\Config\Type\System;
  8. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  9. use Magento\Customer\Model\Metadata\AttributeMetadataCache;
  10. use Magento\Customer\Model\Metadata\AttributeMetadataHydrator;
  11. use Magento\Eav\Model\Cache\Type;
  12. use Magento\Eav\Model\Entity\Attribute;
  13. use Magento\Framework\App\Cache\StateInterface;
  14. use Magento\Framework\App\CacheInterface;
  15. use Magento\Framework\Serialize\SerializerInterface;
  16. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  17. use Magento\Store\Api\Data\StoreInterface;
  18. use Magento\Store\Model\StoreManagerInterface;
  19. /**
  20. * AttributeMetadataCache Test
  21. *
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class AttributeMetadataCacheTest extends \PHPUnit\Framework\TestCase
  25. {
  26. /**
  27. * @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $cacheMock;
  30. /**
  31. * @var StateInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $stateMock;
  34. /**
  35. * @var AttributeMetadataHydrator|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $attributeMetadataHydratorMock;
  38. /**
  39. * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $serializerMock;
  42. /**
  43. * @var AttributeMetadataCache|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $attributeMetadataCache;
  46. /**
  47. * @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $storeMock;
  50. /**
  51. * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $storeManagerMock;
  54. protected function setUp()
  55. {
  56. $objectManager = new ObjectManager($this);
  57. $this->cacheMock = $this->createMock(CacheInterface::class);
  58. $this->stateMock = $this->createMock(StateInterface::class);
  59. $this->serializerMock = $this->createMock(SerializerInterface::class);
  60. $this->attributeMetadataHydratorMock = $this->createMock(AttributeMetadataHydrator::class);
  61. $this->storeMock = $this->createMock(StoreInterface::class);
  62. $this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
  63. $this->storeManagerMock->method('getStore')->willReturn($this->storeMock);
  64. $this->storeMock->method('getId')->willReturn(1);
  65. $this->attributeMetadataCache = $objectManager->getObject(
  66. AttributeMetadataCache::class,
  67. [
  68. 'cache' => $this->cacheMock,
  69. 'state' => $this->stateMock,
  70. 'serializer' => $this->serializerMock,
  71. 'attributeMetadataHydrator' => $this->attributeMetadataHydratorMock,
  72. 'storeManager' => $this->storeManagerMock
  73. ]
  74. );
  75. }
  76. public function testLoadCacheDisabled()
  77. {
  78. $entityType = 'EntityType';
  79. $suffix = 'none';
  80. $this->stateMock->expects($this->once())
  81. ->method('isEnabled')
  82. ->with(Type::TYPE_IDENTIFIER)
  83. ->willReturn(false);
  84. $this->cacheMock->expects($this->never())
  85. ->method('load');
  86. $this->assertFalse($this->attributeMetadataCache->load($entityType, $suffix));
  87. // Make sure isEnabled called once
  88. $this->attributeMetadataCache->load($entityType, $suffix);
  89. }
  90. public function testLoadNoCache()
  91. {
  92. $entityType = 'EntityType';
  93. $suffix = 'none';
  94. $storeId = 1;
  95. $cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
  96. $this->stateMock->expects($this->once())
  97. ->method('isEnabled')
  98. ->with(Type::TYPE_IDENTIFIER)
  99. ->willReturn(true);
  100. $this->cacheMock->expects($this->once())
  101. ->method('load')
  102. ->with($cacheKey)
  103. ->willReturn(false);
  104. $this->assertFalse($this->attributeMetadataCache->load($entityType, $suffix));
  105. }
  106. public function testLoad()
  107. {
  108. $entityType = 'EntityType';
  109. $suffix = 'none';
  110. $storeId = 1;
  111. $cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
  112. $serializedString = 'serialized string';
  113. $attributeMetadataOneData = [
  114. 'attribute_code' => 'attribute_code',
  115. 'frontend_input' => 'hidden',
  116. ];
  117. $attributesMetadataData = [$attributeMetadataOneData];
  118. $this->stateMock->expects($this->once())
  119. ->method('isEnabled')
  120. ->with(Type::TYPE_IDENTIFIER)
  121. ->willReturn(true);
  122. $this->cacheMock->expects($this->once())
  123. ->method('load')
  124. ->with($cacheKey)
  125. ->willReturn($serializedString);
  126. $this->serializerMock->expects($this->once())
  127. ->method('unserialize')
  128. ->with($serializedString)
  129. ->willReturn($attributesMetadataData);
  130. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $attributeMetadataMock */
  131. $attributeMetadataMock = $this->createMock(AttributeMetadataInterface::class);
  132. $this->attributeMetadataHydratorMock->expects($this->at(0))
  133. ->method('hydrate')
  134. ->with($attributeMetadataOneData)
  135. ->willReturn($attributeMetadataMock);
  136. $attributesMetadata = $this->attributeMetadataCache->load($entityType, $suffix);
  137. $this->assertInternalType(
  138. \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
  139. $attributesMetadata
  140. );
  141. $this->assertArrayHasKey(
  142. 0,
  143. $attributesMetadata
  144. );
  145. $this->assertInstanceOf(
  146. AttributeMetadataInterface::class,
  147. $attributesMetadata[0]
  148. );
  149. }
  150. public function testSaveCacheDisabled()
  151. {
  152. $entityType = 'EntityType';
  153. $suffix = 'none';
  154. $attributes = [['foo'], ['bar']];
  155. $this->stateMock->expects($this->once())
  156. ->method('isEnabled')
  157. ->with(Type::TYPE_IDENTIFIER)
  158. ->willReturn(false);
  159. $this->attributeMetadataCache->save($entityType, $attributes, $suffix);
  160. $this->assertEquals(
  161. $attributes,
  162. $this->attributeMetadataCache->load($entityType, $suffix)
  163. );
  164. }
  165. public function testSave()
  166. {
  167. $entityType = 'EntityType';
  168. $suffix = 'none';
  169. $storeId = 1;
  170. $cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
  171. $serializedString = 'serialized string';
  172. $attributeMetadataOneData = [
  173. 'attribute_code' => 'attribute_code',
  174. 'frontend_input' => 'hidden',
  175. ];
  176. $this->stateMock->expects($this->once())
  177. ->method('isEnabled')
  178. ->with(Type::TYPE_IDENTIFIER)
  179. ->willReturn(true);
  180. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $attributeMetadataMock */
  181. $attributeMetadataMock = $this->createMock(AttributeMetadataInterface::class);
  182. $attributesMetadata = [$attributeMetadataMock];
  183. $this->attributeMetadataHydratorMock->expects($this->once())
  184. ->method('extract')
  185. ->with($attributeMetadataMock)
  186. ->willReturn($attributeMetadataOneData);
  187. $this->serializerMock->expects($this->once())
  188. ->method('serialize')
  189. ->with([$attributeMetadataOneData])
  190. ->willReturn($serializedString);
  191. $this->cacheMock->expects($this->once())
  192. ->method('save')
  193. ->with(
  194. $serializedString,
  195. $cacheKey,
  196. [
  197. Type::CACHE_TAG,
  198. Attribute::CACHE_TAG,
  199. System::CACHE_TAG
  200. ]
  201. );
  202. $this->attributeMetadataCache->save($entityType, $attributesMetadata, $suffix);
  203. $this->assertSame(
  204. $attributesMetadata,
  205. $this->attributeMetadataCache->load($entityType, $suffix)
  206. );
  207. }
  208. public function testCleanCacheDisabled()
  209. {
  210. $this->stateMock->expects($this->once())
  211. ->method('isEnabled')
  212. ->with(Type::TYPE_IDENTIFIER)
  213. ->willReturn(false);
  214. $this->cacheMock->expects($this->never())
  215. ->method('clean');
  216. $this->attributeMetadataCache->clean();
  217. }
  218. public function testClean()
  219. {
  220. $this->stateMock->expects($this->once())
  221. ->method('isEnabled')
  222. ->with(Type::TYPE_IDENTIFIER)
  223. ->willReturn(true);
  224. $this->cacheMock->expects($this->once())
  225. ->method('clean');
  226. $this->attributeMetadataCache->clean();
  227. }
  228. }