Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MappableConditionProcessorTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\Eav\Model\Config as EavConfig;
15 
16 class MappableConditionProcessorTest extends \PHPUnit\Framework\TestCase
17 {
21  private $mappableConditionProcessor;
22 
26  private $eavConfigMock;
27 
31  private $objectManagerHelper;
32 
36  private $customConditionProcessorBuilderMock;
37 
38  protected function setUp()
39  {
40  $this->eavConfigMock = $this->getMockBuilder(EavConfig::class)
41  ->disableOriginalConstructor()
42  ->setMethods(['getAttribute'])
43  ->getMock();
44 
45  $this->customConditionProcessorBuilderMock = $this->getMockBuilder(
46  CustomConditionProviderInterface::class
47  )->disableOriginalConstructor()
48  ->setMethods(['hasProcessorForField'])
49  ->getMockForAbstractClass();
50 
51  $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
52 
53  $this->mappableConditionProcessor = $this->objectManagerHelper->getObject(
54  MappableConditionsProcessor::class,
55  [
56  'customConditionProvider' => $this->customConditionProcessorBuilderMock,
57  'eavConfig' => $this->eavConfigMock,
58  ]
59  );
60  }
61 
86  public function testConditionV1()
87  {
88  $field1 = 'field-1';
89  $field2 = 'field-2';
90 
91  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
92  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
93  $inputCondition = $this->getMockForCombinedCondition(
94  [
95  $simpleCondition1,
96  $simpleCondition2
97  ],
98  'any'
99  );
100 
101  $validResult = $this->getMockForCombinedCondition([], 'any');
102 
103  $this->customConditionProcessorBuilderMock
104  ->method('hasProcessorForField')
105  ->will(
106  $this->returnValueMap(
107  [
108  [$field1, true],
109  [$field2, false],
110  ]
111  )
112  );
113 
114  $this->eavConfigMock
115  ->method('getAttribute')
116  ->willReturn(null);
117 
118  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
119 
120  $this->assertEquals($validResult, $result);
121  }
122 
150  public function testConditionV2()
151  {
152  $field1 = 'field-1';
153  $field2 = 'field-2';
154 
155  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
156  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
157  $inputCondition = $this->getMockForCombinedCondition(
158  [
159  $simpleCondition1,
160  $simpleCondition2
161  ],
162  'all'
163  );
164 
165  $validResult = $this->getMockForCombinedCondition(
166  [
167  $simpleCondition1
168  ],
169  'all'
170  );
171 
172  $this->customConditionProcessorBuilderMock
173  ->method('hasProcessorForField')
174  ->will(
175  $this->returnValueMap(
176  [
177  [$field1, true],
178  [$field2, false],
179  ]
180  )
181  );
182 
183  $this->eavConfigMock
184  ->method('getAttribute')
185  ->willReturn(null);
186 
187  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
188 
189  $this->assertEquals($validResult, $result);
190  }
191 
216  public function testConditionV3()
217  {
218  $field1 = 'field-1';
219  $field2 = 'field-2';
220 
221  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
222  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
223  $inputCondition = $this->getMockForCombinedCondition(
224  [
225  $simpleCondition1,
226  $simpleCondition2
227  ],
228  'all'
229  );
230 
231  $validResult = $this->getMockForCombinedCondition([], 'all');
232 
233  $this->customConditionProcessorBuilderMock
234  ->method('hasProcessorForField')
235  ->will(
236  $this->returnValueMap(
237  [
238  [$field1, false],
239  [$field2, false],
240  ]
241  )
242  );
243 
244  $this->eavConfigMock
245  ->method('getAttribute')
246  ->willReturn(null);
247 
248  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
249 
250  $this->assertEquals($validResult, $result);
251  }
252 
312  public function testConditionV4()
313  {
314  $field1 = 'field-1';
315  $field2 = 'field-2';
316 
317  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
318  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
319  $subCondition1 = $this->getMockForCombinedCondition(
320  [
321  $simpleCondition1,
322  $simpleCondition2
323  ],
324  'all'
325  );
326 
327  $field3 = 'field-3';
328  $field4 = 'field-4';
329 
330  $simpleCondition3 = $this->getMockForSimpleCondition($field3);
331  $simpleCondition4 = $this->getMockForSimpleCondition($field4);
332  $subCondition2 = $this->getMockForCombinedCondition(
333  [
334  $simpleCondition3,
335  $simpleCondition4
336  ],
337  'any'
338  );
339 
340  $inputCondition = $this->getMockForCombinedCondition(
341  [
342  $subCondition1,
343  $subCondition2
344  ],
345  'any'
346  );
347 
348  $validSubCondition1 = $this->getMockForCombinedCondition(
349  [
350  $simpleCondition2
351  ],
352  'all'
353  );
354  $validSubCondition2 = $this->getMockForCombinedCondition(
355  [
356  $simpleCondition3,
357  $simpleCondition4
358  ],
359  'any'
360  );
361  $validResult = $this->getMockForCombinedCondition(
362  [
363  $validSubCondition1,
364  $validSubCondition2
365  ],
366  'any'
367  );
368 
369  $this->customConditionProcessorBuilderMock
370  ->method('hasProcessorForField')
371  ->will(
372  $this->returnValueMap(
373  [
374  [$field1, false],
375  [$field2, true],
376  [$field3, true],
377  [$field4, true],
378  ]
379  )
380  );
381 
382  $this->eavConfigMock
383  ->method('getAttribute')
384  ->willReturn(null);
385 
386  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
387 
388  $this->assertEquals($validResult, $result);
389  }
390 
442  public function testConditionV5()
443  {
444  $field1 = 'field-1';
445  $field2 = 'field-2';
446 
447  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
448  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
449  $subCondition1 = $this->getMockForCombinedCondition(
450  [
451  $simpleCondition1,
452  $simpleCondition2
453  ],
454  'any'
455  );
456 
457  $field3 = 'field-3';
458  $field4 = 'field-4';
459 
460  $simpleCondition3 = $this->getMockForSimpleCondition($field3);
461  $simpleCondition4 = $this->getMockForSimpleCondition($field4);
462  $subCondition2 = $this->getMockForCombinedCondition(
463  [
464  $simpleCondition3,
465  $simpleCondition4
466  ],
467  'any'
468  );
469 
470  $inputCondition = $this->getMockForCombinedCondition(
471  [
472  $subCondition1,
473  $subCondition2
474  ],
475  'all'
476  );
477 
478  $validSubCondition2 = $this->getMockForCombinedCondition(
479  [
480  $simpleCondition3,
481  $simpleCondition4
482  ],
483  'any'
484  );
485  $validResult = $this->getMockForCombinedCondition(
486  [
487  $validSubCondition2
488  ],
489  'all'
490  );
491 
492  $this->customConditionProcessorBuilderMock
493  ->method('hasProcessorForField')
494  ->will(
495  $this->returnValueMap(
496  [
497  [$field1, false],
498  [$field2, true],
499  [$field3, true],
500  [$field4, true],
501  ]
502  )
503  );
504 
505  $this->eavConfigMock
506  ->method('getAttribute')
507  ->willReturn(null);
508 
509  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
510 
511  $this->assertEquals($validResult, $result);
512  }
513 
539  public function testConditionV6()
540  {
541  $field1 = 'field-1';
542  $field2 = 'field-2';
543 
544  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
545  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
546 
547  $field3 = 'field-3';
548  $field4 = 'field-4';
549 
550  $simpleCondition3 = $this->getMockForSimpleCondition($field3);
551  $simpleCondition4 = $this->getMockForSimpleCondition($field4);
552  $subCondition1 = $this->getMockForCombinedCondition(
553  [
554  $simpleCondition3,
555  $simpleCondition4
556  ],
557  'any'
558  );
559 
560  $inputCondition = $this->getMockForCombinedCondition(
561  [
562  $simpleCondition1,
563  $simpleCondition2,
564  $subCondition1
565  ],
566  'all'
567  );
568 
569  $this->customConditionProcessorBuilderMock
570  ->method('hasProcessorForField')
571  ->will(
572  $this->returnValueMap(
573  [
574  [$field1, true],
575  [$field2, true],
576  [$field3, true],
577  [$field4, true],
578  ]
579  )
580  );
581 
582  $this->eavConfigMock
583  ->method('getAttribute')
584  ->willReturn(null);
585 
586  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
587 
588  $this->assertEquals($inputCondition, $result);
589  }
590 
683  public function testConditionV7()
684  {
685  $field1 = 'field-1';
686  $field2 = 'field-2';
687  $field3 = 'field-3';
688 
689  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
690  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
691  $simpleCondition3 = $this->getMockForSimpleCondition($field3);
692 
693  $subCondition1 = $this->getMockForCombinedCondition(
694  [
695  $simpleCondition2,
696  $simpleCondition3
697  ],
698  'any'
699  );
700  $subCondition2 = $this->getMockForCombinedCondition(
701  [
702  $simpleCondition1,
703  $subCondition1
704  ],
705  'all'
706  );
707 
708  $field4 = 'field-4';
709  $field5 = 'field-5';
710  $field6 = 'field-6';
711  $field7 = 'field-7';
712 
713  $simpleCondition4 = $this->getMockForSimpleCondition($field4);
714  $simpleCondition5 = $this->getMockForSimpleCondition($field5);
715  $simpleCondition6 = $this->getMockForSimpleCondition($field6);
716  $simpleCondition7 = $this->getMockForSimpleCondition($field7);
717 
718  $subCondition3 = $this->getMockForCombinedCondition(
719  [
720  $simpleCondition4,
721  $simpleCondition5
722  ],
723  'any'
724  );
725  $subCondition4 = $this->getMockForCombinedCondition(
726  [
727  $simpleCondition6,
728  $simpleCondition7
729  ],
730  'any'
731  );
732  $subCondition5 = $this->getMockForCombinedCondition(
733  [
734  $subCondition3,
735  $subCondition4
736  ],
737  'all'
738  );
739 
740  $inputCondition = $this->getMockForCombinedCondition(
741  [
742  $subCondition2,
743  $subCondition5
744  ],
745  'any'
746  );
747 
748  $validSubCondition2 = $this->getMockForCombinedCondition(
749  [
750  $simpleCondition1
751  ],
752  'all'
753  );
754  $validSubCondition4 = $this->getMockForCombinedCondition(
755  [
756  $subCondition4
757  ],
758  'all'
759  );
760 
761  $validResult = $this->getMockForCombinedCondition(
762  [
763  $validSubCondition2,
764  $validSubCondition4
765  ],
766  'any'
767  );
768 
769  $this->customConditionProcessorBuilderMock
770  ->method('hasProcessorForField')
771  ->will(
772  $this->returnValueMap(
773  [
774  [$field1, true],
775  [$field2, true],
776  [$field3, false],
777  [$field4, true],
778  [$field5, false],
779  [$field6, true],
780  [$field7, true],
781  ]
782  )
783  );
784 
785  $this->eavConfigMock
786  ->method('getAttribute')
787  ->willReturn(null);
788 
789  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
790 
791  $this->assertEquals($validResult, $result);
792  }
793 
833  public function testConditionV8()
834  {
835  $field1 = 'field-1';
836  $field2 = 'field-2';
837 
838  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
839  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
840  $subCondition1 = $this->getMockForCombinedCondition(
841  [
842  $simpleCondition1,
843  $simpleCondition2
844  ],
845  'any'
846  );
847 
848  $field3 = 'field-3';
849  $field4 = 'field-4';
850 
851  $simpleCondition3 = $this->getMockForSimpleCondition($field3);
852  $simpleCondition4 = $this->getMockForSimpleCondition($field4);
853  $subCondition2 = $this->getMockForCombinedCondition(
854  [
855  $simpleCondition3,
856  $simpleCondition4
857  ],
858  'any'
859  );
860 
861  $inputCondition = $this->getMockForCombinedCondition(
862  [
863  $subCondition1,
864  $subCondition2
865  ],
866  'any'
867  );
868 
869  $validResult = $this->getMockForCombinedCondition([], 'any');
870 
871  $this->customConditionProcessorBuilderMock
872  ->method('hasProcessorForField')
873  ->will(
874  $this->returnValueMap(
875  [
876  [$field1, false],
877  [$field2, true],
878  [$field3, true],
879  [$field4, false],
880  ]
881  )
882  );
883 
884  $this->eavConfigMock
885  ->method('getAttribute')
886  ->willReturn(null);
887 
888  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
889 
890  $this->assertEquals($validResult, $result);
891  }
892 
933  public function testConditionV9()
934  {
935  $field1 = 'field-1';
936  $field2 = 'field-2';
937 
938  $simpleCondition1 = $this->getMockForSimpleCondition($field1);
939  $simpleCondition2 = $this->getMockForSimpleCondition($field2);
940  $subCondition1 = $this->getMockForCombinedCondition(
941  [
942  $simpleCondition1,
943  $simpleCondition2
944  ],
945  'any'
946  );
947 
948  $field3 = 'field-3';
949  $field4 = 'field-4';
950 
951  $simpleCondition3 = $this->getMockForSimpleCondition($field3);
952  $simpleCondition4 = $this->getMockForSimpleCondition($field4);
953  $subCondition2 = $this->getMockForCombinedCondition(
954  [
955  $simpleCondition3,
956  $simpleCondition4
957  ],
958  'any'
959  );
960 
961  $field5 = 'field-5';
962  $simpleCondition5 = $this->getMockForSimpleCondition($field5);
963 
964  $inputCondition = $this->getMockForCombinedCondition(
965  [
966  $subCondition1,
967  $subCondition2,
968  $simpleCondition5
969  ],
970  'any'
971  );
972 
973  $validResult = $this->getMockForCombinedCondition([], 'any');
974 
975  $this->customConditionProcessorBuilderMock
976  ->method('hasProcessorForField')
977  ->will(
978  $this->returnValueMap(
979  [
980  [$field1, false],
981  [$field2, true],
982  [$field3, true],
983  [$field4, false],
984  [$field5, true],
985  ]
986  )
987  );
988 
989  $this->eavConfigMock
990  ->method('getAttribute')
991  ->willReturn(null);
992 
993  $result = $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
994 
995  $this->assertEquals($validResult, $result);
996  }
997 
1002  public function testException()
1003  {
1004  $simpleCondition = $this->getMockForSimpleCondition('field');
1005  $simpleCondition->setType('olo-lo');
1006  $inputCondition = $this->getMockForCombinedCondition([$simpleCondition], 'any');
1007 
1008  $this->mappableConditionProcessor->rebuildConditionsTree($inputCondition);
1009  }
1010 
1016  protected function getMockForCombinedCondition($subConditions, $aggregator)
1017  {
1018  $mock = $this->getMockBuilder(CombinedCondition::class)
1019  ->disableOriginalConstructor()
1020  ->setMethods()
1021  ->getMock();
1022 
1023  $mock->setConditions($subConditions);
1024  $mock->setAggregator($aggregator);
1025  $mock->setType(CombinedCondition::class);
1026 
1027  return $mock;
1028  }
1029 
1035  {
1036  $mock = $this->getMockBuilder(SimpleCondition::class)
1037  ->disableOriginalConstructor()
1038  ->setMethods()
1039  ->getMock();
1040 
1041  $mock->setAttribute($attribute);
1042  $mock->setType(SimpleCondition::class);
1043 
1044  return $mock;
1045  }
1046 }