Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigTest.php
Go to the documentation of this file.
1 <?php
7 
15 
16 class ConfigTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $config;
22 
26  protected $cacheMock;
27 
31  protected $typeFactoryMock;
32 
37 
41  protected $cacheStateMock;
42 
47 
51  private $serializerMock;
52 
56  private $typeMock;
57 
58  protected function setUp()
59  {
60  $this->cacheMock = $this->createMock(\Magento\Framework\App\CacheInterface::class);
61  $this->typeFactoryMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\TypeFactory::class)
62  ->setMethods(['create'])
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->collectionFactoryMock =
66  $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory::class)
67  ->setMethods(['create'])
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->cacheStateMock = $this->createMock(\Magento\Framework\App\Cache\StateInterface::class);
71  $this->universalFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\UniversalFactory::class)
72  ->setMethods(['create'])
73  ->disableOriginalConstructor()
74  ->getMock();
75 
76  $this->serializerMock = $this->createMock(SerializerInterface::class);
77 
78  $this->typeMock = $this->createMock(Type::class);
79 
80  $this->config = new Config(
81  $this->cacheMock,
82  $this->typeFactoryMock,
83  $this->collectionFactoryMock,
84  $this->cacheStateMock,
85  $this->universalFactoryMock,
86  $this->serializerMock
87  );
88  }
89 
90  public function testGetAttributeCache()
91  {
92  $attributeData = [
93  'attribute_code' => 'attribute_code_1',
94  'attribute_id' => 1
95  ];
96  $attributeCollectionMock = $this->getMockBuilder(
97  Collection::class
98  )->disableOriginalConstructor()
99  ->setMethods(['getData', 'setEntityTypeFilter'])
100  ->getMock();
101  $attributeCollectionMock->expects($this->any())
102  ->method('setEntityTypeFilter')
103  ->will($this->returnSelf());
104  $attributeCollectionMock->expects($this->any())
105  ->method('getData')
106  ->willReturn([$attributeData]);
107  $entityAttributeMock = $this->getMockBuilder(Attribute::class)
108  ->setMethods(['setData', 'loadByCode', 'toArray'])
109  ->disableOriginalConstructor()
110  ->getMock();
111  $entityAttributeMock->expects($this->atLeastOnce())->method('setData')
112  ->willReturnSelf();
113  $entityAttributeMock->expects($this->atLeastOnce())->method('loadByCode')
114  ->willReturnSelf();
115 
116  $factoryCalls = [
117  [
118  Collection::class,
119  [],
120  $attributeCollectionMock
121  ],
122  [
123  Attribute::class,
124  [],
125  $entityAttributeMock
126  ],
127  ];
128 
129  $entityTypeData = [
130  'entity_type_id' => 'entity_type_id',
131  'entity_type_code' => 'entity_type_code'
132  ];
133  $collectionStub = new DataObject([$entityTypeData]);
134  $this->collectionFactoryMock
135  ->expects($this->any())
136  ->method('create')
137  ->willReturn($collectionStub);
138 
139  $entityType = $this->getMockBuilder(Type::class)
140  ->setMethods(['getEntity', 'setData', 'getData', 'getEntityTypeCode', 'getId'])
141  ->disableOriginalConstructor()
142  ->getMock();
143  $entityType->method('getEntityTypeCode')
144  ->willReturn('entity_type_code');
145  $entityType->method('getId')
146  ->willReturn(101);
147 
148  $this->typeFactoryMock
149  ->expects($this->any())
150  ->method('create')
151  ->willReturn($entityType);
152 
153  $this->universalFactoryMock
154  ->expects($this->atLeastOnce())
155  ->method('create')
156  ->will($this->returnValueMap($factoryCalls));
157 
158  $this->assertInstanceOf(Attribute::class, $this->config->getAttribute($entityType, 'attribute_code_1'));
159  }
160 
165  {
166  return [
167  'cache-disabled' => [
168  false,
169  0,
170  0,
171  false,
172  ],
173  'cache-miss' => [
174  true,
175  1,
176  0,
177  false,
178  ],
179  'cached' => [
180  true,
181  1,
182  1,
183  'attribute serialzied data',
184  ],
185  ];
186  }
187 
196  public function testGetAttributes($cacheEnabled)
197  {
198  $attributeData = [
199  'attribute_code' => 'attribute_code_1',
200  'attribute_id' => 1
201  ];
202  $attributeCollectionMock = $this->getMockBuilder(
203  Collection::class
204  )->disableOriginalConstructor()
205  ->setMethods(['getData', 'setEntityTypeFilter'])
206  ->getMock();
207  $attributeCollectionMock
208  ->expects($this->any())
209  ->method('setEntityTypeFilter')
210  ->will($this->returnSelf());
211  $attributeCollectionMock
212  ->expects($this->any())
213  ->method('getData')
214  ->willReturn([$attributeData]);
215  $entityAttributeMock = $this->getMockBuilder(Attribute::class)
216  ->setMethods(['setData', 'load', 'toArray'])
217  ->disableOriginalConstructor()
218  ->getMock();
219  $entityAttributeMock->method('setData')
220  ->willReturnSelf();
221  $entityAttributeMock->method('load')
222  ->willReturnSelf();
223  $entityAttributeMock->method('toArray')
224  ->willReturn($attributeData);
225  $factoryCalls = [
226  [
227  Collection::class,
228  [],
229  $attributeCollectionMock
230  ],
231  [
232  Attribute::class,
233  [],
234  $entityAttributeMock
235  ],
236  ];
237 
238  $this->cacheStateMock
239  ->expects($this->atLeastOnce())
240  ->method('isEnabled')
241  ->with(Cache::TYPE_IDENTIFIER)
242  ->willReturn($cacheEnabled);
243 
244  $entityTypeData = [
245  'entity_type_id' => 'entity_type_id',
246  'entity_type_code' => 'entity_type_code'
247  ];
248  $collectionStub = new DataObject([$entityTypeData]);
249  $this->collectionFactoryMock
250  ->expects($this->any())
251  ->method('create')
252  ->willReturn($collectionStub);
253 
254  $entityType = $this->getMockBuilder(Type::class)
255  ->setMethods(['getEntity', 'setData', 'getData', 'getEntityTypeCode', 'getId'])
256  ->disableOriginalConstructor()
257  ->getMock();
258  $entityType->method('getEntityTypeCode')
259  ->willReturn('entity_type_code');
260  $entityType->method('getId')
261  ->willReturn(101);
262 
263  $this->typeFactoryMock
264  ->expects($this->any())
265  ->method('create')
266  ->willReturn($entityType);
267 
268  $this->universalFactoryMock
269  ->expects($this->atLeastOnce())
270  ->method('create')
271  ->will($this->returnValueMap($factoryCalls));
272 
273  $this->assertEquals(['attribute_code_1' => $entityAttributeMock], $this->config->getAttributes($entityType));
274  }
275 
276  public function testClear()
277  {
278  $this->cacheMock->expects($this->once())
279  ->method('clean')
280  ->with(
281  $this->equalTo(
282  [
283  Cache::CACHE_TAG,
285  ]
286  )
287  );
288  $this->config->clear();
289  }
290 
292  {
293  $this->assertEquals(
294  $this->typeMock,
295  $this->config->getEntityType($this->typeMock)
296  );
297  }
298 
300  {
301  $entityTypeCode = 'catalog_product';
302  $data = [
303  $entityTypeCode => [
304  'entity_type_id' => 1
305  ]
306  ];
307  $serializedData = 'serialized data';
308  $this->cacheStateMock->expects($this->once())
309  ->method('isEnabled')
310  ->with(Cache::TYPE_IDENTIFIER)
311  ->willReturn(true);
312  $this->cacheMock->expects($this->once())
313  ->method('load')
315  ->willReturn($serializedData);
316  $this->serializerMock->expects($this->once())
317  ->method('unserialize')
318  ->with($serializedData)
319  ->willReturn($data);
320  $this->typeMock->expects($this->exactly(2))
321  ->method('getId')
322  ->willReturn($data[$entityTypeCode]['entity_type_id']);
323  $this->typeMock->expects($this->once())
324  ->method('getEntityTypeCode')
325  ->willReturn($entityTypeCode);
326  $this->typeFactoryMock->expects($this->once())
327  ->method('create')
328  ->with(['data' => $data[$entityTypeCode]])
329  ->willReturn($this->typeMock);
330  $this->assertInstanceOf(
331  Type::class,
332  $this->config->getEntityType($entityTypeCode)
333  );
334  }
335 
337  {
338  $entityTypeCode = 'catalog_product';
339  $collectionData = [
340  [
341  'entity_type_id' => 1,
342  'entity_type_code' => $entityTypeCode
343  ]
344  ];
345  $data = [
346  $entityTypeCode => [
347  'entity_type_id' => 1,
348  'entity_type_code' => $entityTypeCode,
349  'attribute_model' => Attribute::class
350  ]
351  ];
352  $serializedData = 'serialized data';
353  $this->cacheStateMock->expects($this->once())
354  ->method('isEnabled')
355  ->with(Cache::TYPE_IDENTIFIER)
356  ->willReturn(true);
357  $this->cacheMock->expects($this->once())
358  ->method('load')
360  ->willReturn(false);
361  $this->serializerMock->expects($this->never())
362  ->method('unserialize');
363  $attributeCollectionMock = $this->createMock(Collection::class);
364  $this->collectionFactoryMock->expects($this->once())
365  ->method('create')
366  ->willReturn($attributeCollectionMock);
367  $attributeCollectionMock->expects($this->once())
368  ->method('getData')
369  ->willReturn($collectionData);
370  $this->serializerMock->expects($this->once())
371  ->method('serialize')
372  ->with($data)
373  ->willReturn($serializedData);
374  $this->cacheMock->expects($this->once())
375  ->method('save')
376  ->with(
377  $serializedData,
379  [
380  Cache::CACHE_TAG,
382  ]
383  );
384  $this->typeMock->expects($this->exactly(2))
385  ->method('getId')
386  ->willReturn($data[$entityTypeCode]['entity_type_id']);
387  $this->typeMock->expects($this->once())
388  ->method('getEntityTypeCode')
389  ->willReturn($entityTypeCode);
390  $this->typeFactoryMock->expects($this->once())
391  ->method('create')
392  ->with(['data' => $data[$entityTypeCode]])
393  ->willReturn($this->typeMock);
394  $this->assertInstanceOf(
395  Type::class,
396  $this->config->getEntityType($entityTypeCode)
397  );
398  }
399 }