Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FixedBundleWithTierPriceCalculatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Bundle\Api\Data\LinkInterface;
10 use \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
11 
18 {
20  private $tierPriceFactory;
21 
22  protected function setUp()
23  {
24  parent::setUp();
25  $this->tierPriceFactory = $this->objectManager->create(ProductTierPriceInterfaceFactory::class);
26  }
27 
36  public function testPriceForFixedBundle(array $strategyModifiers, array $expectedResults)
37  {
38  $this->prepareFixture($strategyModifiers, 'bundle_product');
39  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
40 
41  if (empty($bundleProduct->getOptions())) {
42  $this->markTestSkipped('Unskip after fixing https://github.com/magento-engcom/msi/issues/1398');
43  }
44 
46  $priceInfo = $bundleProduct->getPriceInfo();
48 
49  $this->assertEquals(
50  $expectedResults['minimalPrice'],
51  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
52  'Failed to check minimal price on product'
53  );
54  $this->assertEquals(
55  $expectedResults['maximalPrice'],
56  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
57  'Failed to check maximal price on product'
58  );
59 
60  $priceInfoFromIndexer = $this->productCollectionFactory->create()
61  ->addFieldToFilter('sku', 'bundle_product')
62  ->addPriceData()
63  ->load()
64  ->getFirstItem();
65 
66  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
67  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
68  }
69 
75  public function getTestCases()
76  {
77  return [
78  '
79  #1 Testing product price
80  with tier price and without any sub items and options
81  ' => [
82  'strategy' => $this->getBundleConfiguration1(),
83  'expectedResults' => [
84  // 110 * 0.5
85  'minimalPrice' => 55,
86 
87  // 110 * 0.5
88  'maximalPrice' => 55,
89  ]
90  ],
91 
92  '
93  #2 Testing product price
94  with tier price, fixed sub items and fixed options
95  ' => [
96  'strategy' => $this->getProductConfiguration2(
98  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
99  ),
100  'expectedResults' => [
101  // 0.5 * (110 + 1 * 20) + 100
102  'minimalPrice' => 165,
103 
104  // 0.5 * (110 + 1 * 20) + 100
105  'maximalPrice' => 165,
106  ]
107  ],
108 
109  '
110  #3 Testing product price
111  with tier price, percent sub items and percent options
112  ' => [
113  'strategy' => $this->getProductConfiguration2(
115  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
116  ),
117  'expectedResults' => [
118  // 0.5 * (110 + 110 * 0.2 + 110 * 1)
119  'minimalPrice' => 121,
120 
121  // 0.5 * (110 + 110 * 0.2 + 110 * 1)
122  'maximalPrice' => 121,
123  ]
124  ],
125 
126  '
127  #4 Testing product price
128  with tier price, fixed sub items and percent options
129  ' => [
130  'strategy' => $this->getProductConfiguration2(
132  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
133  ),
134  'expectedResults' => [
135  // 0.5 * (110 + 1 * 20 + 110 * 1)
136  'minimalPrice' => 120,
137 
138  // 0.5 * (110 + 1 * 20 + 110 * 1)
139  'maximalPrice' => 120,
140  ]
141  ],
142 
143  '
144  #5 Testing product price
145  with tier price, percent sub items and fixed options
146  ' => [
147  'strategy' => $this->getProductConfiguration2(
149  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
150  ),
151  'expectedResults' => [
152  // 0.5 * (110 + 110 * 0.2) + 100
153  'minimalPrice' => 166,
154 
155  // 0.5 * (110 + 110 * 0.2) + 100
156  'maximalPrice' => 166,
157  ]
158  ],
159 
160  '
161  #6 Testing product price
162  with tier price, fixed sub items and fixed options
163  ' => [
164  'strategy' => $this->getProductConfiguration3(
166  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
167  ),
168  'expectedResults' => [
169  // 0.5 * 110 + 100
170  'minimalPrice' => 155,
171 
172  // 0.5 * (110 + 2 * 20) + 100
173  'maximalPrice' => 175,
174  ]
175  ],
176 
177  '
178  #7 Testing product price
179  with tier price, percent sub items and percent options
180  ' => [
181  'strategy' => $this->getProductConfiguration3(
183  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
184  ),
185  'expectedResults' => [
186  // 0.5 * (110 + 110 * 1)
187  'minimalPrice' => 110,
188 
189  // 0.5 * (110 + 2 * 110 * 0.2 + 1 * 110)
190  'maximalPrice' => 132,
191  ]
192  ],
193 
194  '
195  #8 Testing product price
196  with tier price, fixed sub items and percent options
197  ' => [
198  'strategy' => $this->getProductConfiguration3(
200  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
201  ),
202  'expectedResults' => [
203  // 0.5 * (110 + 1 * 110)
204  'minimalPrice' => 110,
205 
206  // 0.5 * (110 + 2 * 20 + 1 * 110)
207  'maximalPrice' => 130,
208 
209  ]
210  ],
211 
212  '
213  #9 Testing product price
214  with tier price, percent sub items and fixed options
215  ' => [
216  'strategy' => $this->getProductConfiguration3(
218  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
219  ),
220  'expectedResults' => [
221  // 0.5 * 110 + 100
222  'minimalPrice' => 155,
223 
224  // 0.5 * (110 + 2 * 0.2 * 110) + 100
225  'maximalPrice' => 177,
226  ]
227  ],
228 
229  '
230  #10 Testing product price
231  with tier price, fixed sub items and fixed options
232  ' => [
233  'strategy' => $this->getProductConfiguration4(
235  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
236  ),
237  'expectedResults' => [
238  // 0.5 * (110 + 3 * 10) + 100
239  'minimalPrice' => 170,
240 
241  // 0.5 * (110 + 3 * 10 + 1 * 40) + 100
242  'maximalPrice' => 190,
243  ]
244  ],
245 
246  '
247  #11 Testing product price
248  with tier price, percent sub items and percent options
249  ' => [
250  'strategy' => $this->getProductConfiguration4(
252  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
253  ),
254  'expectedResults' => [
255  // 0.5 * (110 + 3 * 110 * 0.1 + 110 * 1)
256  'minimalPrice' => 126.5,
257 
258  // 0.5 * (110 + 3 * 110 * 0.1 + 1 * 110 * 0.4 + 110 * 1)
259  'maximalPrice' => 148.5,
260  ]
261  ],
262 
263  '
264  #12 Testing product price
265  with tier price, fixed sub items and percent options
266  ' => [
267  'strategy' => $this->getProductConfiguration4(
269  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
270  ),
271  'expectedResults' => [
272  // 0.5 * (110 + 3 * 10 + 1 * 110)
273  'minimalPrice' => 125,
274 
275  // 0.5 * (110 + 3 * 10 + 1 * 40 + 1 * 110)
276  'maximalPrice' => 145,
277  ]
278  ],
279 
280  '
281  #13 Testing product price
282  with tier price, percent sub items and fixed options
283  ' => [
284  'strategy' => $this->getProductConfiguration4(
286  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
287  ),
288  'expectedResults' => [
289  // 0.5 * (110 + 3 * 110 * 0.1) + 100
290  'minimalPrice' => 171.5,
291 
292  // 0.5 * (110 + 3 * 110 * 0.1 + 1 * 110 * 0.4) + 100
293  'maximalPrice' => 193.5,
294  ]
295  ],
296 
297  '
298  #14 Testing product price
299  with tier price, fixed sub items and fixed options
300  ' => [
301  'strategy' => $this->getProductConfiguration5(
303  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
304  ),
305  'expectedResults' => [
306  // 0.5 * (110 + 1 * 40) + 100
307  'minimalPrice' => 175,
308 
309  // 0.5 * (110 + 1 * 40 + 3 * 15) + 100
310  'maximalPrice' => 197.5,
311  ]
312  ],
313 
314  '
315  #15 Testing product price
316  with tier price, percent sub items and percent options
317  ' => [
318  'strategy' => $this->getProductConfiguration5(
320  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
321  ),
322  'expectedResults' => [
323  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110)
324  'minimalPrice' => 132,
325 
326  // 0.5 * (110 + 1 * 110 * 0.4 + 3 * 110 * 0.15 + 110 * 1)
327  'maximalPrice' => 156.75,
328  ]
329  ],
330 
331  '
332  #16 Testing product price
333  with tier price, fixed sub items and percent options
334  ' => [
335  'strategy' => $this->getProductConfiguration5(
337  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
338  ),
339  'expectedResults' => [
340  // 0.5 * (110 + 1 * 40 + 1 * 110)
341  'minimalPrice' => 130,
342 
343  // 0.5 * (110 + 1 * 40 + 3 * 15 + 1 * 110)
344  'maximalPrice' => 152.5,
345  ]
346  ],
347 
348  '
349  #17 Testing product price
350  with tier price, percent sub items and fixed options
351  ' => [
352  'strategy' => $this->getProductConfiguration5(
354  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
355  ),
356  'expectedResults' => [
357  // 0.5 * (110 + 1 * 110 * 0.4) + 100
358  'minimalPrice' => 177,
359 
360  // 0.5 * (110 + 1 * 110 * 0.4 + 3 * 110 * 0.15) + 100
361  'maximalPrice' => 201.75,
362  ]
363  ],
364 
365  '
366  #18 Testing product price
367  with tier price, fixed sub items and fixed options
368  ' => [
369  'strategy' => $this->getProductConfiguration6(
371  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
372  ),
373  'expectedResults' => [
374  // 0.5 * (110 + 1 * 40) + 100
375  'minimalPrice' => 175,
376 
377  // 0.5 * (110 + 3 * 15) + 100
378  'maximalPrice' => 177.5,
379  ]
380  ],
381 
382  '
383  #19 Testing product price
384  with tier price, percent sub items and percent options
385  ' => [
386  'strategy' => $this->getProductConfiguration6(
388  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
389  ),
390  'expectedResults' => [
391  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110)
392  'minimalPrice' => 132,
393 
394  // 0.5 * (110 + 3 * 110 * 0.15 + 1 * 110)
395  'maximalPrice' => 134.75,
396  ]
397  ],
398 
399  '
400  #20 Testing product price
401  with tier price, fixed sub items and percent options
402  ' => [
403  'strategy' => $this->getProductConfiguration6(
405  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
406  ),
407  'expectedResults' => [
408  // 0.5 * (110 + 1 * 40 + 110 * 1)
409  'minimalPrice' => 130,
410 
411  // 0.5 * (110 + 3 * 15 + 110 * 1)
412  'maximalPrice' => 132.5,
413  ]
414  ],
415 
416  '
417  #21 Testing product price
418  with tier price, percent sub items and fixed options
419  ' => [
420  'strategy' => $this->getProductConfiguration6(
422  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
423  ),
424  'expectedResults' => [
425  // 0.5 * (110 + 1 * 110 * 0.4) + 100
426  'minimalPrice' => 177,
427 
428  // 0.5 * (110 + 3 * 110 * 0.15) + 100
429  'maximalPrice' => 179.75,
430  ]
431  ],
432 
433  '
434  #22 Testing product price
435  with tier price, fixed sub items and fixed options
436  ' => [
437  'strategy' => $this->getProductConfiguration7(
439  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
440  ),
441  'expectedResults' => [
442  // 0.5 * (110 + 1 * 40 + 1 * 20) + 100
443  'minimalPrice' => 185,
444 
445  // 0.5 * (110 + 3 * 15 + 1 * 20 + 3 * 10) + 100
446  'maximalPrice' => 202.5,
447  ]
448  ],
449 
450  '
451  #23 Testing product price
452  with tier price, percent sub items and percent options
453  ' => [
454  'strategy' => $this->getProductConfiguration7(
456  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
457  ),
458  'expectedResults' => [
459  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110 * 0.2 + 110 * 1)
460  'minimalPrice' => 143,
461 
462  // 0.5 * (110 + 3 * 110 * 0.15 + 1 * 110 * 0.2 + 3 * 110 * 0.1 + 110 * 1)
463  'maximalPrice' => 162.25,
464  ]
465  ],
466 
467  '
468  #24 Testing product price
469  with tier price, fixed sub items and percent options
470  ' => [
471  'strategy' => $this->getProductConfiguration7(
473  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
474  ),
475  'expectedResults' => [
476  // 0.5 * (110 + 1 * 40 + 1 * 20 + 1 * 110)
477  'minimalPrice' => 140,
478 
479  // 0.5 * (110 + 3 * 15 + 1 * 20 + 3 * 10 + 1 * 110)
480  'maximalPrice' => 157.5,
481  ]
482  ],
483 
484  '
485  #25 Testing product price
486  with tier price, percent sub items and fixed options
487  ' => [
488  'strategy' => $this->getProductConfiguration7(
490  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
491  ),
492  'expectedResults' => [
493  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110 * 0.2) + 100
494  'minimalPrice' => 188,
495 
496  // 0.5 * (110 + 3 * 110 * 0.15 + 1 * 110 * 0.2 + 3 * 110 * 0.1) + 100
497  'maximalPrice' => 207.25,
498  ]
499  ],
500  ];
501  }
502 
507  private function getBundleConfiguration1()
508  {
509  $tierPriceData = [
511  'qty' => 1,
512  'value' => 50,
513  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
514  ];
515 
516  return [
517  [
518  'modifierName' => 'addTierPrice',
519  'data' => [$tierPriceData]
520  ]
521  ];
522  }
523 
530  private function getProductConfiguration2($selectionsPriceType, $customOptionsPriceType)
531  {
532  $optionsData = [
533  [
534  'title' => 'Op1',
535  'required' => true,
536  'type' => 'checkbox',
537  'links' => [
538  [
539  'sku' => 'simple1',
540  'qty' => 1,
541  'price' => 20,
542  'price_type' => $selectionsPriceType
543  ],
544  ]
545  ]
546  ];
547 
548  $customOptionsData = [
549  [
550  'price_type' => $customOptionsPriceType,
551  'title' => 'Test Field',
552  'type' => 'field',
553  'is_require' => 1,
554  'price' => 100,
555  'sku' => '1-text',
556  ]
557  ];
558 
559  $tierPriceData = [
561  'qty' => 1,
562  'value' => 50,
563  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
564  ];
565 
566  return [
567  [
568  'modifierName' => 'addTierPrice',
569  'data' => [$tierPriceData]
570  ],
571  [
572  'modifierName' => 'addSimpleProduct',
573  'data' => [$optionsData]
574  ],
575  [
576  'modifierName' => 'addCustomOption',
577  'data' => [$customOptionsData]
578  ],
579  ];
580  }
581 
588  private function getProductConfiguration3($selectionsPriceType, $customOptionsPriceType)
589  {
590  $optionsData = [
591  [
592  'title' => 'Op1',
593  'type' => 'checkbox',
594  'required' => false,
595  'links' => [
596  [
597  'sku' => 'simple1',
598  'price' => 20,
599  'qty' => 2,
600  'price_type' => $selectionsPriceType
601  ],
602  ]
603  ]
604  ];
605 
606  $customOptionsData = [
607  [
608  'price_type' => $customOptionsPriceType,
609  'title' => 'Test Field',
610  'type' => 'field',
611  'is_require' => 1,
612  'price' => 100,
613  'sku' => '1-text',
614  ]
615  ];
616 
617  $tierPriceData = [
619  'qty' => 1,
620  'value' => 50,
621  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
622  ];
623 
624  return [
625  [
626  'modifierName' => 'addTierPrice',
627  'data' => [$tierPriceData]
628  ],
629  [
630  'modifierName' => 'addSimpleProduct',
631  'data' => [$optionsData]
632  ],
633  [
634  'modifierName' => 'addCustomOption',
635  'data' => [$customOptionsData]
636  ],
637  ];
638  }
639 
646  private function getProductConfiguration4($selectionsPriceType, $customOptionsPriceType)
647  {
648  $optionsData = [
649  [
650  'title' => 'Op1',
651  'required' => true,
652  'type' => 'checkbox',
653  'links' => [
654  [
655  'sku' => 'simple1',
656  'qty' => 1,
657  'price' => 40,
658  'price_type' => $selectionsPriceType
659  ],
660  [
661  'sku' => 'simple2',
662  'price' => 10,
663  'qty' => 3,
664  'price_type' => $selectionsPriceType
665  ],
666  ]
667  ]
668  ];
669 
670  $customOptionsData = [
671  [
672  'price_type' => $customOptionsPriceType,
673  'title' => 'Test Field',
674  'type' => 'field',
675  'is_require' => 1,
676  'price' => 100,
677  'sku' => '1-text',
678  ]
679  ];
680 
681  $tierPriceData = [
683  'qty' => 1,
684  'value' => 50,
685  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
686  ];
687 
688  return [
689  [
690  'modifierName' => 'addTierPrice',
691  'data' => [$tierPriceData]
692  ],
693  [
694  'modifierName' => 'addSimpleProduct',
695  'data' => [$optionsData]
696  ],
697  [
698  'modifierName' => 'addCustomOption',
699  'data' => [$customOptionsData]
700  ],
701  ];
702  }
703 
710  private function getProductConfiguration5($selectionsPriceType, $customOptionsPriceType)
711  {
712  $optionsData = [
713  [
714  'title' => 'Op1',
715  'required' => true,
716  'type' => 'multi',
717  'links' => [
718  [
719  'sku' => 'simple1',
720  'qty' => 1,
721  'price' => 40,
722  'price_type' => $selectionsPriceType
723  ],
724  [
725  'sku' => 'simple2',
726  'price' => 15,
727  'qty' => 3,
728  'price_type' => $selectionsPriceType
729  ],
730  ]
731  ]
732  ];
733 
734  $customOptionsData = [
735  [
736  'price_type' => $customOptionsPriceType,
737  'title' => 'Test Field',
738  'type' => 'field',
739  'is_require' => 1,
740  'price' => 100,
741  'sku' => '1-text',
742  ]
743  ];
744 
745  $tierPriceData = [
747  'qty' => 1,
748  'value' => 50,
749  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
750  ];
751 
752  return [
753  [
754  'modifierName' => 'addTierPrice',
755  'data' => [$tierPriceData]
756  ],
757  [
758  'modifierName' => 'addSimpleProduct',
759  'data' => [$optionsData]
760  ],
761  [
762  'modifierName' => 'addCustomOption',
763  'data' => [$customOptionsData]
764  ],
765  ];
766  }
767 
774  private function getProductConfiguration6($selectionsPriceType, $customOptionsPriceType)
775  {
776  $optionsData = [
777  [
778  'title' => 'Op1',
779  'required' => true,
780  'type' => 'radio',
781  'links' => [
782  [
783  'sku' => 'simple1',
784  'qty' => 1,
785  'price' => 40,
786  'price_type' => $selectionsPriceType
787  ],
788  [
789  'sku' => 'simple2',
790  'price' => 15,
791  'qty' => 3,
792  'price_type' => $selectionsPriceType
793  ],
794  ]
795  ]
796  ];
797 
798  $customOptionsData = [
799  [
800  'price_type' => $customOptionsPriceType,
801  'title' => 'Test Field',
802  'type' => 'field',
803  'is_require' => 1,
804  'price' => 100,
805  'sku' => '1-text',
806  ]
807  ];
808 
809  $tierPriceData = [
811  'qty' => 1,
812  'value' => 50,
813  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
814  ];
815 
816  return [
817  [
818  'modifierName' => 'addTierPrice',
819  'data' => [$tierPriceData]
820  ],
821  [
822  'modifierName' => 'addSimpleProduct',
823  'data' => [$optionsData]
824  ],
825  [
826  'modifierName' => 'addCustomOption',
827  'data' => [$customOptionsData]
828  ],
829  ];
830  }
831 
838  private function getProductConfiguration7($selectionsPriceType, $customOptionsPriceType)
839  {
840  $optionsData = [
841  [
842  'title' => 'Op1',
843  'required' => true,
844  'type' => 'radio',
845  'links' => [
846  [
847  'sku' => 'simple1',
848  'qty' => 1,
849  'price' => 40,
850  'price_type' => $selectionsPriceType
851  ],
852  [
853  'sku' => 'simple2',
854  'price' => 15,
855  'qty' => 3,
856  'price_type' => $selectionsPriceType
857  ],
858  ]
859  ],
860  [
861  'title' => 'Op2',
862  'required' => true,
863  'type' => 'checkbox',
864  'links' => [
865  [
866  'sku' => 'simple1',
867  'qty' => 1,
868  'price' => 20,
869  'price_type' => $selectionsPriceType
870  ],
871  [
872  'sku' => 'simple2',
873  'price' => 10,
874  'qty' => 3,
875  'price_type' => $selectionsPriceType
876  ],
877  ]
878  ]
879  ];
880 
881  $customOptionsData = [
882  [
883  'price_type' => $customOptionsPriceType,
884  'title' => 'Test Field',
885  'type' => 'field',
886  'is_require' => 1,
887  'price' => 100,
888  'sku' => '1-text',
889  ]
890  ];
891 
892  $tierPriceData = [
894  'qty' => 1,
895  'value' => 50,
896  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
897  ];
898 
899  return [
900  [
901  'modifierName' => 'addTierPrice',
902  'data' => [$tierPriceData]
903  ],
904  [
905  'modifierName' => 'addSimpleProduct',
906  'data' => [$optionsData]
907  ],
908  [
909  'modifierName' => 'addCustomOption',
910  'data' => [$customOptionsData]
911  ],
912  ];
913  }
914 
920  protected function addTierPrice(\Magento\Catalog\Model\Product $product, $tirePriceData)
921  {
922  $tierPrice = $this->tierPriceFactory->create([
923  'data' => $tirePriceData
924  ]);
925  $product->setTierPrices([$tierPrice]);
926 
927  return $product;
928  }
929 }
if($msrpShowOnGesture && $price['price']->getValue()< $product->getMsrp()) if($isSaleable) $tierPriceData
$bundleProduct