Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FixedBundleWithSpecialPriceCalculatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Bundle\Api\Data\LinkInterface;
10 
15 {
24  public function testPriceForFixedBundle(array $strategyModifiers, array $expectedResults)
25  {
26  if (empty($strategyModifiers)) {
27  $this->markTestSkipped('Unskip after fixing https://github.com/magento-engcom/msi/issues/1398');
28  }
29  $this->prepareFixture($strategyModifiers, 'bundle_product');
30  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
31 
33  $priceInfo = $bundleProduct->getPriceInfo();
35 
36  $this->assertEquals(
37  $expectedResults['minimalPrice'],
38  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
39  'Failed to check minimal price on product'
40  );
41  $this->assertEquals(
42  $expectedResults['maximalPrice'],
43  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
44  'Failed to check maximal price on product'
45  );
46 
47  $priceInfoFromIndexer = $this->productCollectionFactory->create()
48  ->addFieldToFilter('sku', 'bundle_product')
49  ->addPriceData()
50  ->load()
51  ->getFirstItem();
52 
53  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
54  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
55  }
56 
62  public function getTestCases()
63  {
64  return [
65  '
66  #1 Testing price for fixed bundle product
67  with special price and without any sub items and options
68  ' => [
69  'strategy' => $this->getBundleConfiguration1(),
70  'expectedResults' => [
71  // 110 * 0.5
72  'minimalPrice' => 55,
73 
74  // 110 * 0.5
75  'maximalPrice' => 55,
76  ]
77  ],
78 
79  '
80  #2 Testing price for fixed bundle product
81  with special price, fixed sub items and fixed options
82  ' => [
83  'strategy' => $this->getBundleConfiguration2(
85  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
86  ),
87  'expectedResults' => [
88  // 0.5 * (110 + 1 * 20) + 100
89  'minimalPrice' => 165,
90 
91  // 0.5 * (110 + 1 * 20) + 100
92  'maximalPrice' => 165,
93  ]
94  ],
95 
96  '
97  #3 Testing price for fixed bundle product
98  with special price, percent sub items and percent options
99  ' => [
100  'strategy' => $this->getBundleConfiguration2(
102  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
103  ),
104  'expectedResults' => [
105  // 0.5 * (110 + 110 * 0.2 + 110 * 1)
106  'minimalPrice' => 121,
107 
108  // 0.5 * (110 + 110 * 0.2 + 110 * 1)
109  'maximalPrice' => 121,
110  ]
111  ],
112 
113  '
114  #4 Testing price for fixed bundle product
115  with special price, fixed sub items and percent options
116  ' => [
117  'strategy' => $this->getBundleConfiguration2(
119  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
120  ),
121  'expectedResults' => [
122  // 0.5 * (110 + 1 * 20 + 110 * 1)
123  'minimalPrice' => 120,
124 
125  // 0.5 * (110 + 1 * 20 + 110 * 1)
126  'maximalPrice' => 120,
127  ]
128  ],
129 
130  '
131  #5 Testing price for fixed bundle product
132  with special price, percent sub items and fixed options
133  ' => [
134  'strategy' => $this->getBundleConfiguration2(
136  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
137  ),
138  'expectedResults' => [
139  // 0.5 * (110 + 110 * 0.2) + 100
140  'minimalPrice' => 166,
141 
142  // 0.5 * (110 + 110 * 0.2) + 100
143  'maximalPrice' => 166,
144  ]
145  ],
146 
147  '
148  #6 Testing price for fixed bundle product
149  with special price, fixed sub items and fixed options
150  ' => [
151  'strategy' => $this->getBundleConfiguration3(
153  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
154  ),
155  'expectedResults' => [
156  // 0.5 * 110 + 100
157  'minimalPrice' => 155,
158 
159  // 0.5 * (110 + 2 * 20) + 100
160  'maximalPrice' => 175,
161  ]
162  ],
163 
164  '
165  #7 Testing price for fixed bundle product
166  with special price, percent sub items and percent options
167  ' => [
168  'strategy' => $this->getBundleConfiguration3(
170  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
171  ),
172  'expectedResults' => [
173  // 0.5 * (110 + 110 * 1)
174  'minimalPrice' => 110,
175 
176  // 0.5 * (110 + 2 * 110 * 0.2 + 1 * 110)
177  'maximalPrice' => 132,
178  ]
179  ],
180 
181  '
182  #8 Testing price for fixed bundle product
183  with special price, fixed sub items and percent options
184  ' => [
185  'strategy' => $this->getBundleConfiguration3(
187  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
188  ),
189  'expectedResults' => [
190  // 0.5 * (110 + 1 * 110)
191  'minimalPrice' => 110,
192 
193  // 0.5 * (110 + 2 * 20 + 1 * 110)
194  'maximalPrice' => 130,
195  ]
196  ],
197 
198  '
199  #9 Testing price for fixed bundle product
200  with special price, percent sub items and fixed options
201  ' => [
202  'strategy' => $this->getBundleConfiguration3(
204  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
205  ),
206  'expectedResults' => [
207  // 0.5 * 110 + 100
208  'minimalPrice' => 155,
209 
210  // 0.5 * (110 + 2 * 0.2 * 110) + 100
211  'maximalPrice' => 177,
212  ]
213  ],
214 
215  '
216  #10 Testing price for fixed bundle product
217  with special price, fixed sub items and fixed options
218  ' => [
219  'strategy' => $this->getBundleConfiguration4(
221  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
222  ),
223  'expectedResults' => [
224  // 0.5 * (110 + 3 * 10) + 100
225  'minimalPrice' => 170,
226 
227  // 0.5 * (110 + 3 * 10 + 1 * 40) + 100
228  'maximalPrice' => 190,
229  ]
230  ],
231 
232  '
233  #11 Testing price for fixed bundle product
234  with special price, percent sub items and percent options
235  ' => [
236  'strategy' => $this->getBundleConfiguration4(
238  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
239  ),
240  'expectedResults' => [
241  // 0.5 * (110 + 3 * 110 * 0.1 + 110 * 1)
242  'minimalPrice' => 126.5,
243 
244  // 0.5 * (110 + 3 * 110 * 0.1 + 1 * 110 * 0.4 + 110 * 1)
245  'maximalPrice' => 148.5,
246  ]
247  ],
248 
249  '
250  #12 Testing price for fixed bundle product
251  with special price, fixed sub items and percent options
252  ' => [
253  'strategy' => $this->getBundleConfiguration4(
255  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
256  ),
257  'expectedResults' => [
258  // 0.5 * (110 + 3 * 10 + 1 * 110)
259  'minimalPrice' => 125,
260 
261  // 0.5 * (110 + 3 * 10 + 1 * 40 + 1 * 110)
262  'maximalPrice' => 145,
263  ]
264  ],
265 
266  '
267  #13 Testing price for fixed bundle product
268  with special price, percent sub items and fixed options
269  ' => [
270  'strategy' => $this->getBundleConfiguration4(
272  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
273  ),
274  'expectedResults' => [
275  // 0.5 * (110 + 3 * 110 * 0.1) + 100
276  'minimalPrice' => 171.5,
277 
278  // 0.5 * (110 + 3 * 110 * 0.1 + 1 * 110 * 0.4) + 100
279  'maximalPrice' => 193.5,
280  ]
281  ],
282 
283  '
284  #14 Testing price for fixed bundle product
285  with special price, fixed sub items and fixed options
286  ' => [
287  'strategy' => $this->getBundleConfiguration5(
289  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
290  ),
291  'expectedResults' => [
292  // 0.5 * (110 + 1 * 40) + 100
293  'minimalPrice' => 175,
294 
295  // 0.5 * (110 + 1 * 40 + 3 * 15) + 100
296  'maximalPrice' => 197.5,
297  ]
298  ],
299 
300  '
301  #15 Testing price for fixed bundle product
302  with special price, percent sub items and percent options
303  ' => [
304  'strategy' => $this->getBundleConfiguration5(
306  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
307  ),
308  'expectedResults' => [
309  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110)
310  'minimalPrice' => 132,
311 
312  // 0.5 * (110 + 1 * 110 * 0.4 + 3 * 110 * 0.15 + 110 * 1)
313  'maximalPrice' => 156.75,
314  ]
315  ],
316 
317  '
318  #16 Testing price for fixed bundle product
319  with special price, fixed sub items and percent options
320  ' => [
321  'strategy' => $this->getBundleConfiguration5(
323  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
324  ),
325  'expectedResults' => [
326  // 0.5 * (110 + 1 * 40 + 1 * 110)
327  'minimalPrice' => 130,
328 
329  // 0.5 * (110 + 1 * 40 + 3 * 15 + 1 * 110)
330  'maximalPrice' => 152.5,
331  ]
332  ],
333 
334  '
335  #17 Testing price for fixed bundle product
336  with special price, percent sub items and fixed options
337  ' => [
338  'strategy' => $this->getBundleConfiguration5(
340  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
341  ),
342  'expectedResults' => [
343  // 0.5 * (110 + 1 * 110 * 0.4) + 100
344  'minimalPrice' => 177,
345 
346  // 0.5 * (110 + 1 * 110 * 0.4 + 3 * 110 * 0.15) + 100
347  'maximalPrice' => 201.75,
348  ]
349  ],
350 
351  '
352  #18 Testing price for fixed bundle product
353  with special price, fixed sub items and fixed options
354  ' => [
355  'strategy' => $this->getBundleConfiguration6(
357  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
358  ),
359  'expectedResults' => [
360  // 0.5 * (110 + 1 * 40) + 100
361  'minimalPrice' => 175,
362 
363  // 0.5 * (110 + 3 * 15) + 100
364  'maximalPrice' => 177.5,
365  ]
366  ],
367 
368  '
369  #19 Testing price for fixed bundle product
370  with special price, percent sub items and percent options
371  ' => [
372  'strategy' => $this->getBundleConfiguration6(
374  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
375  ),
376  'expectedResults' => [
377  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110)
378  'minimalPrice' => 132,
379 
380  // 0.5 * (110 + 3 * 110 * 0.15 + 1 * 110)
381  'maximalPrice' => 134.75,
382  ]
383  ],
384 
385  '
386  #20 Testing price for fixed bundle product
387  with special price, fixed sub items and percent options
388  ' => [
389  'strategy' => $this->getBundleConfiguration6(
391  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
392  ),
393  'expectedResults' => [
394  // 0.5 * (110 + 1 * 40 + 110 * 1)
395  'minimalPrice' => 130,
396 
397  // 0.5 * (110 + 3 * 15 + 110 * 1)
398  'maximalPrice' => 132.5,
399  ]
400  ],
401 
402  '
403  #21 Testing price for fixed bundle product
404  with special price, percent sub items and fixed options
405  ' => [
406  'strategy' => $this->getBundleConfiguration6(
408  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
409  ),
410  'expectedResults' => [
411  // 0.5 * (110 + 1 * 110 * 0.4) + 100
412  'minimalPrice' => 177,
413 
414  // 0.5 * (110 + 3 * 110 * 0.15) + 100
415  'maximalPrice' => 179.75,
416  ]
417  ],
418 
419  '
420  #22 Testing price for fixed bundle product
421  with special price, fixed sub items and fixed options
422  ' => [
423  'strategy' => $this->getBundleConfiguration7(
425  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
426  ),
427  'expectedResults' => [
428  // 0.5 * (110 + 1 * 40 + 1 * 20) + 100
429  'minimalPrice' => 185,
430 
431  // 0.5 * (110 + 3 * 15 + 1 * 20 + 3 * 10) + 100
432  'maximalPrice' => 202.5,
433  ]
434  ],
435 
436  '
437  #23 Testing price for fixed bundle product
438  with special price, percent sub items and percent options
439  ' => [
440  'strategy' => $this->getBundleConfiguration7(
442  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
443  ),
444  'expectedResults' => [
445  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110 * 0.2 + 110 * 1)
446  'minimalPrice' => 143,
447 
448  // 0.5 * (110 + 3 * 110 * 0.15 + 1 * 110 * 0.2 + 3 * 110 * 0.1 + 110 * 1)
449  'maximalPrice' => 162.25,
450  ]
451  ],
452 
453  '
454  #24 Testing price for fixed bundle product
455  with special price, fixed sub items and percent options
456  ' => [
457  'strategy' => $this->getBundleConfiguration7(
459  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
460  ),
461  'expectedResults' => [
462  // 0.5 * (110 + 1 * 40 + 1 * 20 + 1 * 110)
463  'minimalPrice' => 140,
464 
465  // 0.5 * (110 + 3 * 15 + 1 * 20 + 3 * 10 + 1 * 110)
466  'maximalPrice' => 157.5,
467  ]
468  ],
469 
470  '
471  #25 Testing price for fixed bundle product
472  with special price, percent sub items and fixed options
473  ' => [
474  'strategy' => $this->getBundleConfiguration7(
476  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
477  ),
478  'expectedResults' => [
479  // 0.5 * (110 + 1 * 110 * 0.4 + 1 * 110 * 0.2) + 100
480  'minimalPrice' => 188,
481 
482  // 0.5 * (110 + 3 * 110 * 0.15 + 1 * 110 * 0.2 + 3 * 110 * 0.1) + 100
483  'maximalPrice' => 207.25,
484  ]
485  ],
486  ];
487  }
488 
493  private function getBundleConfiguration1()
494  {
495  return [];
496  }
497 
504  private function getBundleConfiguration2(
505  $selectionsPriceType,
506  $customOptionsPriceType
507  ) {
508  $optionsData = [
509  [
510  'title' => 'Op1',
511  'required' => true,
512  'type' => 'checkbox',
513  'links' => [
514  [
515  'sku' => 'simple1',
516  'qty' => 1,
517  'price' => 20,
518  'price_type' => $selectionsPriceType
519  ],
520  ]
521  ]
522  ];
523 
524  $customOptionsData = [
525  [
526  'price_type' => $customOptionsPriceType,
527  'title' => 'Test Field',
528  'type' => 'field',
529  'is_require' => 1,
530  'price' => 100,
531  'sku' => '1-text',
532  ]
533  ];
534 
535  return [
536  [
537  'modifierName' => 'addSimpleProduct',
538  'data' => [$optionsData]
539  ],
540  [
541  'modifierName' => 'addCustomOption',
542  'data' => [$customOptionsData]
543  ],
544  ];
545  }
546 
553  private function getBundleConfiguration3(
554  $selectionsPriceType,
555  $customOptionsPriceType
556  ) {
557  $optionsData = [
558  [
559  'title' => 'Op1',
560  'type' => 'checkbox',
561  'required' => false,
562  'links' => [
563  [
564  'sku' => 'simple1',
565  'price' => 20,
566  'qty' => 2,
567  'price_type' => $selectionsPriceType
568  ],
569  ]
570  ]
571  ];
572 
573  $customOptionsData = [
574  [
575  'price_type' => $customOptionsPriceType,
576  'title' => 'Test Field',
577  'type' => 'field',
578  'is_require' => 1,
579  'price' => 100,
580  'sku' => '1-text',
581  ]
582  ];
583 
584  return [
585  [
586  'modifierName' => 'addSimpleProduct',
587  'data' => [$optionsData]
588  ],
589  [
590  'modifierName' => 'addCustomOption',
591  'data' => [$customOptionsData]
592  ],
593  ];
594  }
595 
602  private function getBundleConfiguration4(
603  $selectionsPriceType,
604  $customOptionsPriceType
605  ) {
606  $optionsData = [
607  [
608  'title' => 'Op1',
609  'required' => true,
610  'type' => 'checkbox',
611  'links' => [
612  [
613  'sku' => 'simple1',
614  'qty' => 1,
615  'price' => 40,
616  'price_type' => $selectionsPriceType
617  ],
618  [
619  'sku' => 'simple2',
620  'price' => 10,
621  'qty' => 3,
622  'price_type' => $selectionsPriceType
623  ],
624  ]
625  ]
626  ];
627 
628  $customOptionsData = [
629  [
630  'price_type' => $customOptionsPriceType,
631  'title' => 'Test Field',
632  'type' => 'field',
633  'is_require' => 1,
634  'price' => 100,
635  'sku' => '1-text',
636  ]
637  ];
638 
639  return [
640  [
641  'modifierName' => 'addSimpleProduct',
642  'data' => [$optionsData]
643  ],
644  [
645  'modifierName' => 'addCustomOption',
646  'data' => [$customOptionsData]
647  ],
648  ];
649  }
650 
657  private function getBundleConfiguration5(
658  $selectionsPriceType,
659  $customOptionsPriceType
660  ) {
661  $optionsData = [
662  [
663  'title' => 'Op1',
664  'required' => true,
665  'type' => 'multi',
666  'links' => [
667  [
668  'sku' => 'simple1',
669  'qty' => 1,
670  'price' => 40,
671  'price_type' => $selectionsPriceType
672  ],
673  [
674  'sku' => 'simple2',
675  'price' => 15,
676  'qty' => 3,
677  'price_type' => $selectionsPriceType
678  ],
679  ]
680  ]
681  ];
682 
683  $customOptionsData = [
684  [
685  'price_type' => $customOptionsPriceType,
686  'title' => 'Test Field',
687  'type' => 'field',
688  'is_require' => 1,
689  'price' => 100,
690  'sku' => '1-text',
691  ]
692  ];
693 
694  return [
695  [
696  'modifierName' => 'addSimpleProduct',
697  'data' => [$optionsData]
698  ],
699  [
700  'modifierName' => 'addCustomOption',
701  'data' => [$customOptionsData]
702  ],
703  ];
704  }
705 
712  private function getBundleConfiguration6(
713  $selectionsPriceType,
714  $customOptionsPriceType
715  ) {
716  $optionsData = [
717  [
718  'title' => 'Op1',
719  'required' => true,
720  'type' => 'radio',
721  'links' => [
722  [
723  'sku' => 'simple1',
724  'qty' => 1,
725  'price' => 40,
726  'price_type' => $selectionsPriceType
727  ],
728  [
729  'sku' => 'simple2',
730  'price' => 15,
731  'qty' => 3,
732  'price_type' => $selectionsPriceType
733  ],
734  ]
735  ]
736  ];
737 
738  $customOptionsData = [
739  [
740  'price_type' => $customOptionsPriceType,
741  'title' => 'Test Field',
742  'type' => 'field',
743  'is_require' => 1,
744  'price' => 100,
745  'sku' => '1-text',
746  ]
747  ];
748 
749  return [
750  [
751  'modifierName' => 'addSimpleProduct',
752  'data' => [$optionsData]
753  ],
754  [
755  'modifierName' => 'addCustomOption',
756  'data' => [$customOptionsData]
757  ],
758  ];
759  }
760 
767  private function getBundleConfiguration7(
768  $selectionsPriceType,
769  $customOptionsPriceType
770  ) {
771  $optionsData = [
772  [
773  'title' => 'Op1',
774  'required' => true,
775  'type' => 'radio',
776  'links' => [
777  [
778  'sku' => 'simple1',
779  'qty' => 1,
780  'price' => 40,
781  'price_type' => $selectionsPriceType
782  ],
783  [
784  'sku' => 'simple2',
785  'price' => 15,
786  'qty' => 3,
787  'price_type' => $selectionsPriceType
788  ],
789  ]
790  ],
791  [
792  'title' => 'Op2',
793  'required' => true,
794  'type' => 'checkbox',
795  'links' => [
796  [
797  'sku' => 'simple1',
798  'qty' => 1,
799  'price' => 20,
800  'price_type' => $selectionsPriceType
801  ],
802  [
803  'sku' => 'simple2',
804  'price' => 10,
805  'qty' => 3,
806  'price_type' => $selectionsPriceType
807  ],
808  ]
809  ]
810  ];
811 
812  $customOptionsData = [
813  [
814  'price_type' => $customOptionsPriceType,
815  'title' => 'Test Field',
816  'type' => 'field',
817  'is_require' => 1,
818  'price' => 100,
819  'sku' => '1-text',
820  ]
821  ];
822 
823  return [
824  [
825  'modifierName' => 'addSimpleProduct',
826  'data' => [$optionsData]
827  ],
828  [
829  'modifierName' => 'addCustomOption',
830  'data' => [$customOptionsData]
831  ],
832  ];
833  }
834 }
$bundleProduct