Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DynamicBundleWithSpecialPriceCalculatorTest.php
Go to the documentation of this file.
1 <?php
8 
13 {
22  public function testPriceForDynamicBundle(array $strategyModifiers, array $expectedResults)
23  {
24  $this->prepareFixture($strategyModifiers, 'bundle_product');
25  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
26 
28  $priceInfo = $bundleProduct->getPriceInfo();
30 
31  $this->assertEquals(
32  $expectedResults['minimalPrice'],
33  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
34  'Failed to check minimal price on product'
35  );
36  $this->assertEquals(
37  $expectedResults['maximalPrice'],
38  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
39  'Failed to check maximal price on product'
40  );
41 
42  if (isset($expectedResults['regularMinimalPrice'])) {
44  $this->assertEquals(
45  $expectedResults['regularMinimalPrice'],
46  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
47  'Failed to check minimal regular price on product'
48  );
49  }
50 
51  if (isset($expectedResults['regularMaximalPrice'])) {
53  $this->assertEquals(
54  $expectedResults['regularMaximalPrice'],
55  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
56  'Failed to check maximal regular price on product'
57  );
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  '#1 Testing price for dynamic bundle with one required option and special price' => [
79  'strategy' => $this->getBundleConfiguration1(),
80  'expectedResults' => [
81  // 0.5 * 10
82  'minimalPrice' => 5,
83  // 0.5 * 10
84  'maximalPrice' => 5,
85  ]
86  ],
87 
88  '#2 Testing price for dynamic bundle with one non required option and special price' => [
89  'strategy' => $this->getBundleConfiguration2(),
90  'expectedResults' => [
91  // 0.5 * 2 * 10
92  'minimalPrice' => 10,
93  // 0.5 * 2 * 10
94  'maximalPrice' => 10,
95  ]
96 
97  ],
98 
99  '
100  #3 Testing price for dynamic bundle
101  with one required checkbox type option, two simples and special price
102  ' => [
103  'strategy' => $this->getBundleConfiguration3(),
104  'expectedResults' => [
105  // 0.5 * 1 * 10
106  'minimalPrice' => 5,
107  // 0.5 * (1 * 10 + 3 * 30)
108  'maximalPrice' => 50,
109  ]
110  ],
111 
112  '
113  #4 Testing price for dynamic bundle
114  with one required multi type option, two simples with special price
115  ' => [
116  'strategy' => $this->getBundleConfiguration4(),
117  'expectedResults' => [
118  // 0.5 * (min (1 * 9.9, 2.5 * 4))
119  'minimalPrice' => 4.95,
120  // 0.5 * ( 1 * 9.9 + 2.5 * 4)
121  'maximalPrice' => 9.95,
122  ]
123  ],
124 
125  '#5 Testing price for dynamic bundle with one required option, one non required and special price' => [
126  'strategy' => $this->getBundleConfiguration5(),
127  'expectedResults' => [
128  // 0.5 * (3 * 2.5)
129  'minimalPrice' => 3.75,
130  // 0.5 * (3 * 13 + 1 * 30 + 1 * 10)
131  'maximalPrice' => 39.5,
132  // 1 * 10
133  'regularMinimalPrice' => '10',
134  // 3 * 20 + (30 * 1 + 13 * 3)
135  'regularMaximalPrice' => '129',
136  ]
137  ],
138 
139  '#6 Testing price for dynamic bundle with one simple product with special price' => [
140  'strategy' => $this->getBundleConfiguration6(),
141  'expectedResults' => [
142  // 0.5 * min(4 * 2.5, 1 * 9.9)
143  'minimalPrice' => 4.95,
144  // 0.5 * max(4 * 2.5, 1 * 9.9)
145  'maximalPrice' => 5,
146  ]
147  ],
148  ];
149  }
150 
155  private function getBundleConfiguration1()
156  {
157  $optionsData = [
158  [
159  'title' => 'Op1',
160  'required' => true,
161  'type' => 'checkbox',
162  'links' => [
163  [
164  'sku' => 'simple1',
165  'qty' => 1,
166  ],
167  ]
168  ]
169  ];
170 
171  return [
172  [
173  'modifierName' => 'addSimpleProduct',
174  'data' => [$optionsData]
175  ],
176  ];
177  }
178 
183  private function getBundleConfiguration2()
184  {
185  $optionsData = [
186  [
187  'title' => 'Op1',
188  'type' => 'checkbox',
189  'required' => false,
190  'links' => [
191  [
192  'sku' => 'simple1',
193  'qty' => 2,
194  ],
195  ]
196  ]
197  ];
198 
199  return [
200  [
201  'modifierName' => 'addSimpleProduct',
202  'data' => [$optionsData]
203  ],
204  ];
205  }
206 
211  private function getBundleConfiguration3()
212  {
213  $optionsData = [
214  [
215  'title' => 'Op1',
216  'required' => true,
217  'type' => 'checkbox',
218  'links' => [
219  [
220  'sku' => 'simple1',
221  'qty' => 1,
222  ],
223  [
224  'sku' => 'simple3',
225  'qty' => 3,
226  ],
227  ]
228  ]
229  ];
230 
231  return [
232  [
233  'modifierName' => 'addSimpleProduct',
234  'data' => [$optionsData]
235  ],
236  ];
237  }
238 
243  private function getBundleConfiguration4()
244  {
245  $optionsData = [
246  [
247  'title' => 'Op1',
248  'required' => true,
249  'type' => 'checkbox',
250  'links' => [
251  [
252  'sku' => 'simple5',
253  'qty' => 1,
254  ],
255  [
256  'sku' => 'simple2',
257  'qty' => 4,
258  ],
259  ]
260  ]
261  ];
262 
263  return [
264  [
265  'modifierName' => 'addSimpleProduct',
266  'data' => [$optionsData]
267  ],
268  ];
269  }
270 
275  private function getBundleConfiguration5()
276  {
277  $optionsData = [
278  [
279  'title' => 'Op1',
280  'required' => true,
281  'type' => 'radio',
282  'links' => [
283  [
284  'sku' => 'simple1',
285  'qty' => 1,
286  ],
287  [
288  'sku' => 'simple2',
289  'qty' => 3,
290  ],
291  ]
292  ],
293  [
294  'title' => 'Op2',
295  'required' => false,
296  'type' => 'checkbox',
297  'links' => [
298  [
299  'sku' => 'simple3',
300  'qty' => 1,
301  ],
302  [
303  'sku' => 'simple4',
304  'qty' => 3,
305  ],
306  ]
307  ]
308  ];
309 
310  return [
311  [
312  'modifierName' => 'addSimpleProduct',
313  'data' => [$optionsData]
314  ],
315  ];
316  }
317 
322  private function getBundleConfiguration6()
323  {
324  $optionsData = [
325  [
326  'title' => 'Op1',
327  'required' => true,
328  'type' => 'radio',
329  'links' => [
330  [
331  'sku' => 'simple2',
332  'qty' => 4,
333  ],
334  [
335  'sku' => 'simple5',
336  'qty' => 1,
337  ],
338  ]
339  ]
340  ];
341 
342  return [
343  [
344  'modifierName' => 'addSimpleProduct',
345  'data' => [$optionsData]
346  ],
347  ];
348  }
349 }
$bundleProduct