ConfigTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model;
  7. use Magento\Eav\Model\Cache\Type as Cache;
  8. use Magento\Eav\Model\Config;
  9. use Magento\Eav\Model\Entity\Attribute;
  10. use Magento\Eav\Model\Entity\Type;
  11. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
  12. use Magento\Framework\DataObject;
  13. use Magento\Framework\Serialize\SerializerInterface;
  14. class ConfigTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Eav\Model\Config
  18. */
  19. protected $config;
  20. /**
  21. * @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $cacheMock;
  24. /**
  25. * @var \Magento\Eav\Model\Entity\TypeFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $typeFactoryMock;
  28. /**
  29. * @var \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $collectionFactoryMock;
  32. /**
  33. * @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $cacheStateMock;
  36. /**
  37. * @var \Magento\Framework\Validator\UniversalFactory|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $universalFactoryMock;
  40. /**
  41. * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $serializerMock;
  44. /**
  45. * @var Type|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $typeMock;
  48. protected function setUp()
  49. {
  50. $this->cacheMock = $this->createMock(\Magento\Framework\App\CacheInterface::class);
  51. $this->typeFactoryMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\TypeFactory::class)
  52. ->setMethods(['create'])
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->collectionFactoryMock =
  56. $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory::class)
  57. ->setMethods(['create'])
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->cacheStateMock = $this->createMock(\Magento\Framework\App\Cache\StateInterface::class);
  61. $this->universalFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\UniversalFactory::class)
  62. ->setMethods(['create'])
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->serializerMock = $this->createMock(SerializerInterface::class);
  66. $this->typeMock = $this->createMock(Type::class);
  67. $this->config = new Config(
  68. $this->cacheMock,
  69. $this->typeFactoryMock,
  70. $this->collectionFactoryMock,
  71. $this->cacheStateMock,
  72. $this->universalFactoryMock,
  73. $this->serializerMock
  74. );
  75. }
  76. public function testGetAttributeCache()
  77. {
  78. $attributeData = [
  79. 'attribute_code' => 'attribute_code_1',
  80. 'attribute_id' => 1
  81. ];
  82. $attributeCollectionMock = $this->getMockBuilder(
  83. Collection::class
  84. )->disableOriginalConstructor()
  85. ->setMethods(['getData', 'setEntityTypeFilter'])
  86. ->getMock();
  87. $attributeCollectionMock->expects($this->any())
  88. ->method('setEntityTypeFilter')
  89. ->will($this->returnSelf());
  90. $attributeCollectionMock->expects($this->any())
  91. ->method('getData')
  92. ->willReturn([$attributeData]);
  93. $entityAttributeMock = $this->getMockBuilder(Attribute::class)
  94. ->setMethods(['setData', 'loadByCode', 'toArray'])
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $entityAttributeMock->expects($this->atLeastOnce())->method('setData')
  98. ->willReturnSelf();
  99. $entityAttributeMock->expects($this->atLeastOnce())->method('loadByCode')
  100. ->willReturnSelf();
  101. $factoryCalls = [
  102. [
  103. Collection::class,
  104. [],
  105. $attributeCollectionMock
  106. ],
  107. [
  108. Attribute::class,
  109. [],
  110. $entityAttributeMock
  111. ],
  112. ];
  113. $entityTypeData = [
  114. 'entity_type_id' => 'entity_type_id',
  115. 'entity_type_code' => 'entity_type_code'
  116. ];
  117. $collectionStub = new DataObject([$entityTypeData]);
  118. $this->collectionFactoryMock
  119. ->expects($this->any())
  120. ->method('create')
  121. ->willReturn($collectionStub);
  122. $entityType = $this->getMockBuilder(Type::class)
  123. ->setMethods(['getEntity', 'setData', 'getData', 'getEntityTypeCode', 'getId'])
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $entityType->method('getEntityTypeCode')
  127. ->willReturn('entity_type_code');
  128. $entityType->method('getId')
  129. ->willReturn(101);
  130. $this->typeFactoryMock
  131. ->expects($this->any())
  132. ->method('create')
  133. ->willReturn($entityType);
  134. $this->universalFactoryMock
  135. ->expects($this->atLeastOnce())
  136. ->method('create')
  137. ->will($this->returnValueMap($factoryCalls));
  138. $this->assertInstanceOf(Attribute::class, $this->config->getAttribute($entityType, 'attribute_code_1'));
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function getAttributeCacheDataProvider()
  144. {
  145. return [
  146. 'cache-disabled' => [
  147. false,
  148. 0,
  149. 0,
  150. false,
  151. ],
  152. 'cache-miss' => [
  153. true,
  154. 1,
  155. 0,
  156. false,
  157. ],
  158. 'cached' => [
  159. true,
  160. 1,
  161. 1,
  162. 'attribute serialzied data',
  163. ],
  164. ];
  165. }
  166. /**
  167. * @param boolean $cacheEnabled
  168. * @param int $loadCalls
  169. * @param int $cachedValue
  170. * @param int $unserializeCalls
  171. * @dataProvider getAttributeCacheDataProvider
  172. * @return void
  173. */
  174. public function testGetAttributes($cacheEnabled)
  175. {
  176. $attributeData = [
  177. 'attribute_code' => 'attribute_code_1',
  178. 'attribute_id' => 1
  179. ];
  180. $attributeCollectionMock = $this->getMockBuilder(
  181. Collection::class
  182. )->disableOriginalConstructor()
  183. ->setMethods(['getData', 'setEntityTypeFilter'])
  184. ->getMock();
  185. $attributeCollectionMock
  186. ->expects($this->any())
  187. ->method('setEntityTypeFilter')
  188. ->will($this->returnSelf());
  189. $attributeCollectionMock
  190. ->expects($this->any())
  191. ->method('getData')
  192. ->willReturn([$attributeData]);
  193. $entityAttributeMock = $this->getMockBuilder(Attribute::class)
  194. ->setMethods(['setData', 'load', 'toArray'])
  195. ->disableOriginalConstructor()
  196. ->getMock();
  197. $entityAttributeMock->method('setData')
  198. ->willReturnSelf();
  199. $entityAttributeMock->method('load')
  200. ->willReturnSelf();
  201. $entityAttributeMock->method('toArray')
  202. ->willReturn($attributeData);
  203. $factoryCalls = [
  204. [
  205. Collection::class,
  206. [],
  207. $attributeCollectionMock
  208. ],
  209. [
  210. Attribute::class,
  211. [],
  212. $entityAttributeMock
  213. ],
  214. ];
  215. $this->cacheStateMock
  216. ->expects($this->atLeastOnce())
  217. ->method('isEnabled')
  218. ->with(Cache::TYPE_IDENTIFIER)
  219. ->willReturn($cacheEnabled);
  220. $entityTypeData = [
  221. 'entity_type_id' => 'entity_type_id',
  222. 'entity_type_code' => 'entity_type_code'
  223. ];
  224. $collectionStub = new DataObject([$entityTypeData]);
  225. $this->collectionFactoryMock
  226. ->expects($this->any())
  227. ->method('create')
  228. ->willReturn($collectionStub);
  229. $entityType = $this->getMockBuilder(Type::class)
  230. ->setMethods(['getEntity', 'setData', 'getData', 'getEntityTypeCode', 'getId'])
  231. ->disableOriginalConstructor()
  232. ->getMock();
  233. $entityType->method('getEntityTypeCode')
  234. ->willReturn('entity_type_code');
  235. $entityType->method('getId')
  236. ->willReturn(101);
  237. $this->typeFactoryMock
  238. ->expects($this->any())
  239. ->method('create')
  240. ->willReturn($entityType);
  241. $this->universalFactoryMock
  242. ->expects($this->atLeastOnce())
  243. ->method('create')
  244. ->will($this->returnValueMap($factoryCalls));
  245. $this->assertEquals(['attribute_code_1' => $entityAttributeMock], $this->config->getAttributes($entityType));
  246. }
  247. public function testClear()
  248. {
  249. $this->cacheMock->expects($this->once())
  250. ->method('clean')
  251. ->with(
  252. $this->equalTo(
  253. [
  254. Cache::CACHE_TAG,
  255. Attribute::CACHE_TAG,
  256. ]
  257. )
  258. );
  259. $this->config->clear();
  260. }
  261. public function testGetEntityTypeInstanceOfTypePassed()
  262. {
  263. $this->assertEquals(
  264. $this->typeMock,
  265. $this->config->getEntityType($this->typeMock)
  266. );
  267. }
  268. public function testGetEntityTypeCacheExists()
  269. {
  270. $entityTypeCode = 'catalog_product';
  271. $data = [
  272. $entityTypeCode => [
  273. 'entity_type_id' => 1
  274. ]
  275. ];
  276. $serializedData = 'serialized data';
  277. $this->cacheStateMock->expects($this->once())
  278. ->method('isEnabled')
  279. ->with(Cache::TYPE_IDENTIFIER)
  280. ->willReturn(true);
  281. $this->cacheMock->expects($this->once())
  282. ->method('load')
  283. ->with(Config::ENTITIES_CACHE_ID)
  284. ->willReturn($serializedData);
  285. $this->serializerMock->expects($this->once())
  286. ->method('unserialize')
  287. ->with($serializedData)
  288. ->willReturn($data);
  289. $this->typeMock->expects($this->exactly(2))
  290. ->method('getId')
  291. ->willReturn($data[$entityTypeCode]['entity_type_id']);
  292. $this->typeMock->expects($this->once())
  293. ->method('getEntityTypeCode')
  294. ->willReturn($entityTypeCode);
  295. $this->typeFactoryMock->expects($this->once())
  296. ->method('create')
  297. ->with(['data' => $data[$entityTypeCode]])
  298. ->willReturn($this->typeMock);
  299. $this->assertInstanceOf(
  300. Type::class,
  301. $this->config->getEntityType($entityTypeCode)
  302. );
  303. }
  304. public function testGetEntityTypeCacheDoesNotExist()
  305. {
  306. $entityTypeCode = 'catalog_product';
  307. $collectionData = [
  308. [
  309. 'entity_type_id' => 1,
  310. 'entity_type_code' => $entityTypeCode
  311. ]
  312. ];
  313. $data = [
  314. $entityTypeCode => [
  315. 'entity_type_id' => 1,
  316. 'entity_type_code' => $entityTypeCode,
  317. 'attribute_model' => Attribute::class
  318. ]
  319. ];
  320. $serializedData = 'serialized data';
  321. $this->cacheStateMock->expects($this->once())
  322. ->method('isEnabled')
  323. ->with(Cache::TYPE_IDENTIFIER)
  324. ->willReturn(true);
  325. $this->cacheMock->expects($this->once())
  326. ->method('load')
  327. ->with(Config::ENTITIES_CACHE_ID)
  328. ->willReturn(false);
  329. $this->serializerMock->expects($this->never())
  330. ->method('unserialize');
  331. $attributeCollectionMock = $this->createMock(Collection::class);
  332. $this->collectionFactoryMock->expects($this->once())
  333. ->method('create')
  334. ->willReturn($attributeCollectionMock);
  335. $attributeCollectionMock->expects($this->once())
  336. ->method('getData')
  337. ->willReturn($collectionData);
  338. $this->serializerMock->expects($this->once())
  339. ->method('serialize')
  340. ->with($data)
  341. ->willReturn($serializedData);
  342. $this->cacheMock->expects($this->once())
  343. ->method('save')
  344. ->with(
  345. $serializedData,
  346. Config::ENTITIES_CACHE_ID,
  347. [
  348. Cache::CACHE_TAG,
  349. Attribute::CACHE_TAG
  350. ]
  351. );
  352. $this->typeMock->expects($this->exactly(2))
  353. ->method('getId')
  354. ->willReturn($data[$entityTypeCode]['entity_type_id']);
  355. $this->typeMock->expects($this->once())
  356. ->method('getEntityTypeCode')
  357. ->willReturn($entityTypeCode);
  358. $this->typeFactoryMock->expects($this->once())
  359. ->method('create')
  360. ->with(['data' => $data[$entityTypeCode]])
  361. ->willReturn($this->typeMock);
  362. $this->assertInstanceOf(
  363. Type::class,
  364. $this->config->getEntityType($entityTypeCode)
  365. );
  366. }
  367. }