Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConditionBuilderTest.php
Go to the documentation of this file.
1 <?php
11 
23 use Magento\Catalog\Model\ResourceModel\Eav\Attribute as CatalogEavAttribute;
24 
31 class ConditionBuilderTest extends \PHPUnit\Framework\TestCase
32 {
43  EntityMetadataInterface $metadata,
44  array $scopes,
45  $linkFieldValue
46  ) {
47  $storeManager = $this->getMockBuilder(StoreManager::class)
48  ->disableOriginalConstructor()
49  ->setMethods([
50  'getStore',
51  ])
52  ->getMock();
53  $storeManager->expects($this->never())
54  ->method('getStore');
55 
56  $conditionsBuilder = new ConditionBuilder($storeManager);
57  $result = $conditionsBuilder->buildExistingAttributeWebsiteScope(
58  $attribute,
59  $metadata,
60  $scopes,
61  $linkFieldValue
62  );
63 
64  $this->assertEquals([], $result);
65  }
66 
71  {
72  $attribute = $this->getMockBuilder(Attribute::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75 
76  $metadata = $this->getMockBuilder(EntityMetadataInterface::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79 
80  $scopes = [];
81 
82  $linkFieldValue = 5;
83 
84  return [
85  [
86  $attribute,
87  $metadata,
88  $scopes,
89  $linkFieldValue,
90  ],
91  ];
92  }
93 
104  EntityMetadataInterface $metadata,
105  array $scopes,
106  $linkFieldValue
107  ) {
108  $storeManager = $this->getMockBuilder(StoreManager::class)
109  ->disableOriginalConstructor()
110  ->setMethods([
111  'getStore',
112  ])
113  ->getMock();
114  $storeManager->expects($this->never())
115  ->method('getStore');
116 
117  $conditionsBuilder = new ConditionBuilder($storeManager);
118  $result = $conditionsBuilder->buildExistingAttributeWebsiteScope(
119  $attribute,
120  $metadata,
121  $scopes,
122  $linkFieldValue
123  );
124 
125  $this->assertEquals([], $result);
126  }
127 
132  {
133  $attribute = $this->getMockBuilder(CatalogEavAttribute::class)
134  ->disableOriginalConstructor()
135  ->setMethods([
136  'isScopeWebsite',
137  ])
138  ->getMock();
139 
140  $attribute->expects($this->never())
141  ->method('isScopeWebsite')
142  ->will(
143  $this->returnValue(true)
144  );
145 
146  $metadata = $this->getMockBuilder(EntityMetadataInterface::class)
147  ->disableOriginalConstructor()
148  ->getMock();
149 
150  $scopes = [];
151 
152  $linkFieldValue = 5;
153 
154  return [
155  [
156  $attribute,
157  $metadata,
158  $scopes,
159  $linkFieldValue,
160  ],
161  ];
162  }
163 
175  EntityMetadataInterface $metadata,
177  array $scopes,
178  $linkFieldValue
179  ) {
180  $storeManager = $this->getMockBuilder(StoreManager::class)
181  ->disableOriginalConstructor()
182  ->setMethods([
183  'getStore',
184  ])
185  ->getMock();
186  $storeManager->expects($this->once())
187  ->method('getStore')
188  ->will(
189  $this->returnValue($store)
190  );
191 
192  $conditionsBuilder = new ConditionBuilder($storeManager);
193  $result = $conditionsBuilder->buildExistingAttributeWebsiteScope(
194  $attribute,
195  $metadata,
196  $scopes,
197  $linkFieldValue
198  );
199 
200  $this->assertEquals([], $result);
201  }
202 
207  {
208  $attribute = $this->getMockBuilder(CatalogEavAttribute::class)
209  ->disableOriginalConstructor()
210  ->setMethods([
211  'isScopeWebsite',
212  ])
213  ->getMock();
214  $attribute->expects($this->never())
215  ->method('isScopeWebsite')
216  ->will(
217  $this->returnValue(true)
218  );
219 
220  $metadata = $this->getMockBuilder(EntityMetadataInterface::class)
221  ->disableOriginalConstructor()
222  ->getMock();
223 
224  $scope = $this->getMockBuilder(ScopeInterface::class)
225  ->disableOriginalConstructor()
226  ->setMethods([
227  'getIdentifier',
228  'getValue',
229  'getFallback',
230  ])
231  ->getMock();
232  $scope->expects($this->once())
233  ->method('getIdentifier')
234  ->will(
235  $this->returnValue(Store::STORE_ID)
236  );
237  $scope->expects($this->once())
238  ->method('getValue')
239  ->will(
240  $this->returnValue(1)
241  );
242  $scopes = [
243  $scope,
244  ];
245 
246  $store = $this->getMockBuilder(Store::class)
247  ->disableOriginalConstructor()
248  ->setMethods([
249  'getWebsite',
250  ])
251  ->getMock();
252  $store->expects($this->once())
253  ->method('getWebsite')
254  ->will(
255  $this->returnValue(false)
256  );
257 
258  $linkFieldValue = 5;
259 
260  return [
261  [
262  $attribute,
263  $metadata,
264  $store,
265  $scopes,
266  $linkFieldValue,
267  ],
268  ];
269  }
270 
283  EntityMetadataInterface $metadata,
285  array $scopes,
286  array $expectedConditions,
287  $linkFieldValue
288  ) {
289  $storeManager = $this->getMockBuilder(StoreManager::class)
290  ->disableOriginalConstructor()
291  ->setMethods([
292  'getStore',
293  ])
294  ->getMock();
295  $storeManager->expects($this->once())
296  ->method('getStore')
297  ->will(
298  $this->returnValue($store)
299  );
300 
301  $conditionsBuilder = new ConditionBuilder($storeManager);
302  $result = $conditionsBuilder->buildExistingAttributeWebsiteScope(
303  $attribute,
304  $metadata,
305  $scopes,
306  $linkFieldValue
307  );
308 
309  $this->assertEquals($expectedConditions, $result);
310  }
311 
316  {
317  $attribute = $this->getValidAttributeMock();
318 
319  $dbAdapater = $this->getMockBuilder(Mysql::class)
320  ->disableOriginalConstructor()
321  ->setMethods([
322  'quoteIdentifier',
323  ])
324  ->getMock();
325  $dbAdapater->expects($this->exactly(3))
326  ->method('quoteIdentifier')
327  ->will(
328  $this->returnCallback(function ($input) {
329  return sprintf('`%s`', $input);
330  })
331  );
332 
333  $metadata = $this->getMockBuilder(EntityMetadata::class)
334  ->disableOriginalConstructor()
335  ->setMethods([
336  'getLinkField',
337  'getEntityConnection',
338  ])
339  ->getMock();
340  $metadata->expects($this->once())
341  ->method('getLinkField')
342  ->will(
343  $this->returnValue('entity_id')
344  );
345  $metadata->expects($this->exactly(3))
346  ->method('getEntityConnection')
347  ->will(
348  $this->returnValue($dbAdapater)
349  );
350 
351  $scopes = [
352  $this->getValidScopeMock(),
353  ];
354 
355  $store = $this->getValidStoreMock();
356 
357  $expectedConditions = [
358  [
359  'entity_id = ?' => 5,
360  'attribute_id = ?' => 12,
361  '`store_id` = ?' => 1,
362  ],
363  [
364  'entity_id = ?' => 5,
365  'attribute_id = ?' => 12,
366  '`store_id` = ?' => 2,
367  ],
368  [
369  'entity_id = ?' => 5,
370  'attribute_id = ?' => 12,
371  '`store_id` = ?' => 3,
372  ],
373  ];
374 
375  $linkFieldValue = 5;
376 
377  return [
378  [
379  $attribute,
380  $metadata,
381  $store,
382  $scopes,
383  $expectedConditions,
384  $linkFieldValue,
385  ],
386  ];
387  }
388 
399  EntityMetadataInterface $metadata,
400  array $scopes,
401  $linkFieldValue
402  ) {
403  $storeManager = $this->getMockBuilder(StoreManager::class)
404  ->disableOriginalConstructor()
405  ->setMethods([
406  'getStore',
407  ])
408  ->getMock();
409  $storeManager->expects($this->never())
410  ->method('getStore');
411 
412  $conditionsBuilder = new ConditionBuilder($storeManager);
413  $result = $conditionsBuilder->buildNewAttributesWebsiteScope(
414  $attribute,
415  $metadata,
416  $scopes,
417  $linkFieldValue
418  );
419 
420  $this->assertEquals([], $result);
421  }
422 
427  {
428  $attribute = $this->getMockBuilder(Attribute::class)
429  ->disableOriginalConstructor()
430  ->getMock();
431 
432  $metadata = $this->getMockBuilder(EntityMetadataInterface::class)
433  ->disableOriginalConstructor()
434  ->getMock();
435 
436  $scopes = [];
437 
438  $linkFieldValue = 5;
439 
440  return [
441  [
442  $attribute,
443  $metadata,
444  $scopes,
445  $linkFieldValue,
446  ],
447  ];
448  }
449 
462  EntityMetadataInterface $metadata,
464  array $scopes,
465  array $expectedConditions,
466  $linkFieldValue
467  ) {
468  $storeManager = $this->getMockBuilder(StoreManager::class)
469  ->disableOriginalConstructor()
470  ->setMethods([
471  'getStore',
472  ])
473  ->getMock();
474  $storeManager->expects($this->once())
475  ->method('getStore')
476  ->will(
477  $this->returnValue($store)
478  );
479 
480  $conditionsBuilder = new ConditionBuilder($storeManager);
481  $result = $conditionsBuilder->buildNewAttributesWebsiteScope(
482  $attribute,
483  $metadata,
484  $scopes,
485  $linkFieldValue
486  );
487 
488  $this->assertEquals($expectedConditions, $result);
489  }
490 
495  {
496  $attribute = $this->getValidAttributeMock();
497 
498  $metadata = $this->getMockBuilder(EntityMetadata::class)
499  ->disableOriginalConstructor()
500  ->setMethods([
501  'getLinkField',
502  ])
503  ->getMock();
504  $metadata->expects($this->once())
505  ->method('getLinkField')
506  ->will(
507  $this->returnValue('entity_id')
508  );
509 
510  $scopes = [
511  $this->getValidScopeMock(),
512  ];
513 
514  $store = $this->getValidStoreMock();
515 
516  $expectedConditions = [
517  [
518  'entity_id' => 5,
519  'attribute_id' => 12,
520  'store_id' => 1,
521  ],
522  [
523  'entity_id' => 5,
524  'attribute_id' => 12,
525  'store_id' => 2,
526  ],
527  [
528  'entity_id' => 5,
529  'attribute_id' => 12,
530  'store_id' => 3,
531  ],
532  ];
533 
534  $linkFieldValue = 5;
535 
536  return [
537  [
538  $attribute,
539  $metadata,
540  $store,
541  $scopes,
542  $expectedConditions,
543  $linkFieldValue,
544  ],
545  ];
546  }
547 
551  private function getValidAttributeMock()
552  {
553  $attribute = $this->getMockBuilder(CatalogEavAttribute::class)
554  ->disableOriginalConstructor()
555  ->setMethods([
556  'isScopeWebsite',
557  'getAttributeId',
558  ])
559  ->getMock();
560  $attribute->expects($this->never())
561  ->method('isScopeWebsite')
562  ->will(
563  $this->returnValue(true)
564  );
565  $attribute->expects($this->once())
566  ->method('getAttributeId')
567  ->will(
568  $this->returnValue(12)
569  );
570 
571  return $attribute;
572  }
573 
577  private function getValidStoreMock()
578  {
579  $website = $this->getMockBuilder(Website::class)
580  ->disableOriginalConstructor()
581  ->setMethods([
582  'getStoreIds',
583  ])
584  ->getMock();
585  $website->expects($this->once())
586  ->method('getStoreIds')
587  ->will(
588  $this->returnValue([
589  1,
590  2,
591  3,
592  ])
593  );
594 
595  $store = $this->getMockBuilder(Store::class)
596  ->disableOriginalConstructor()
597  ->setMethods([
598  'getWebsite',
599  ])
600  ->getMock();
601  $store->expects($this->once())
602  ->method('getWebsite')
603  ->will(
604  $this->returnValue($website)
605  );
606 
607  return $store;
608  }
609 
613  private function getValidScopeMock()
614  {
615  $scope = $this->getMockBuilder(ScopeInterface::class)
616  ->disableOriginalConstructor()
617  ->setMethods([
618  'getIdentifier',
619  'getValue',
620  'getFallback',
621  ])
622  ->getMock();
623  $scope->expects($this->once())
624  ->method('getIdentifier')
625  ->will(
626  $this->returnValue(Store::STORE_ID)
627  );
628  $scope->expects($this->once())
629  ->method('getValue')
630  ->will(
631  $this->returnValue(1)
632  );
633 
634  return $scope;
635  }
636 }
testBuildExistingAttributeWebsiteScopeStoreWebsiteNotFound(AbstractAttribute $attribute, EntityMetadataInterface $metadata, StoreInterface $store, array $scopes, $linkFieldValue)
testBuildExistingAttributeWebsiteScopeSuccess(AbstractAttribute $attribute, EntityMetadataInterface $metadata, StoreInterface $store, array $scopes, array $expectedConditions, $linkFieldValue)
$storeManager
testBuildExistingAttributeWebsiteScopeInappropriateAttribute(AbstractAttribute $attribute, EntityMetadataInterface $metadata, array $scopes, $linkFieldValue)
testBuildExistingAttributeWebsiteScopeStoreScopeNotFound(AbstractAttribute $attribute, EntityMetadataInterface $metadata, array $scopes, $linkFieldValue)
testBuildNewAttributeWebsiteScopeSuccess(AbstractAttribute $attribute, EntityMetadataInterface $metadata, StoreInterface $store, array $scopes, array $expectedConditions, $linkFieldValue)
testBuildNewAttributeWebsiteScopeUnappropriateAttribute(AbstractAttribute $attribute, EntityMetadataInterface $metadata, array $scopes, $linkFieldValue)