Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FixedBundlePriceCalculatorTest.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  $this->prepareFixture($strategyModifiers, 'bundle_product');
27  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
28 
30  $priceInfo = $bundleProduct->getPriceInfo();
32 
33  $this->assertEquals(
34  $expectedResults['minimalPrice'],
35  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
36  'Failed to check minimal price on product'
37  );
38  $this->assertEquals(
39  $expectedResults['maximalPrice'],
40  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
41  'Failed to check maximal price on product'
42  );
43 
44  $priceInfoFromIndexer = $this->productCollectionFactory->create()
45  ->addIdFilter([42])
46  ->addPriceData()
47  ->load()
48  ->getFirstItem();
49 
50  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
51  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
52  }
53 
63  public function testPriceForFixedBundleInWebsiteScope(array $strategyModifiers, array $expectedResults)
64  {
65  $this->prepareFixture($strategyModifiers, 'bundle_product');
66  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
67 
69  $priceInfo = $bundleProduct->getPriceInfo();
71 
72  $this->assertEquals(
73  $expectedResults['minimalPrice'],
74  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
75  'Failed to check minimal price on product'
76  );
77  $this->assertEquals(
78  $expectedResults['maximalPrice'],
79  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
80  'Failed to check maximal price on product'
81  );
82 
83  $priceInfoFromIndexer = $this->productCollectionFactory->create()
84  ->addFieldToFilter('sku', 'bundle_product')
85  ->addPriceData()
86  ->load()
87  ->getFirstItem();
88 
89  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
90  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
91  }
92 
98  public function getTestCases()
99  {
100  return [
101  '#1 Testing price for fixed bundle product with one simple' => [
102  'strategy' => $this->getProductWithOneSimple(),
103  'expectedResults' => [
104  // 110 + 10 (price from simple1)
105  'minimalPrice' => 120,
106  // 110 + 10 (sum of simple price)
107  'maximalPrice' => 120,
108  ]
109  ],
110 
111  '#2 Testing price for fixed bundle product with three simples and different qty' => [
112  'strategy' => $this->getProductWithDifferentQty(),
113  'expectedResults' => [
114  // 110 + 10 (min price from simples)
115  'minimalPrice' => 120,
116  // 110 + (3 * 10) + (2 * 10) + 10
117  'maximalPrice' => 170,
118  ]
119  ],
120 
121  '#3 Testing price for fixed bundle product with three simples and different price' => [
122  'strategy' => $this->getProductWithDifferentPrice(),
123  'expectedResults' => [
124  // 110 + 10
125  'minimalPrice' => 120,
126  // 110 + 60
127  'maximalPrice' => 170,
128  ]
129  ],
130 
131  '#4 Testing price for fixed bundle product with three simples' => [
132  'strategy' => $this->getProductWithSamePrice(),
133  'expectedResults' => [
134  // 110 + 10
135  'minimalPrice' => 120,
136  // 110 + 30
137  'maximalPrice' => 140,
138  ]
139  ],
140 
141  '
142  #5 Testing price for fixed bundle product
143  with fixed sub items, fixed options and without any discounts
144  ' => [
145  'strategy' => $this->getBundleConfiguration3(
147  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
148  ),
149  'expectedResults' => [
150  // 110 + 1 * 20 + 100
151  'minimalPrice' => 230,
152 
153  // 110 + 1 * 20 + 100
154  'maximalPrice' => 230,
155  ]
156  ],
157 
158  '
159  #6 Testing price for fixed bundle product
160  with percent sub items, percent options and without any discounts
161  ' => [
162  'strategy' => $this->getBundleConfiguration3(
164  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
165  ),
166  'expectedResults' => [
167  // 110 + 110 * 0.2 + 110 * 1
168  'minimalPrice' => 242,
169 
170  // 110 + 110 * 0.2 + 110 * 1
171  'maximalPrice' => 242,
172  ]
173  ],
174 
175  '
176  #7 Testing price for fixed bundle product
177  with fixed sub items, percent options and without any discounts
178  ' => [
179  'strategy' => $this->getBundleConfiguration3(
181  self::CUSTOM_OPTION_PRICE_TYPE_PERCENT
182  ),
183  'expectedResults' => [
184  // 110 + 1 * 20 + 110 * 1
185  'minimalPrice' => 240,
186 
187  // 110 + 1 * 20 + 110 * 1
188  'maximalPrice' => 240,
189  ]
190  ],
191 
192  '
193  #8 Testing price for fixed bundle product
194  with percent sub items, fixed options and without any discounts
195  ' => [
196  'strategy' => $this->getBundleConfiguration3(
198  self::CUSTOM_OPTION_PRICE_TYPE_FIXED
199  ),
200  'expectedResults' => [
201  // 110 + 110 * 0.2 + 100
202  'minimalPrice' => 232,
203 
204  // 110 + 110 * 0.2 + 100
205  'maximalPrice' => 232,
206  ]
207  ],
208  ];
209  }
210 
215  private function getProductWithOneSimple()
216  {
217  $optionsData = [
218  [
219  'title' => 'Op1',
220  'required' => true,
221  'type' => 'checkbox',
222  'links' => [
223  [
224  'sku' => 'simple1',
225  'price' => 10,
226  'qty' => 1,
227  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
228  ],
229  ]
230  ],
231  ];
232 
233  return [
234  [
235  'modifierName' => 'addSimpleProduct',
236  'data' => [$optionsData]
237  ],
238  ];
239  }
240 
245  private function getProductWithDifferentQty()
246  {
247  $optionsData = [
248  [
249  'title' => 'Op1',
250  'required' => true,
251  'type' => 'checkbox',
252  'links' => [
253  [
254  'sku' => 'simple1',
255  'price' => 10,
256  'qty' => 3,
257  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
258  ],
259  [
260  'sku' => 'simple2',
261  'price' => 10,
262  'qty' => 2,
263  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
264  ],
265  [
266  'sku' => 'simple3',
267  'price' => 10,
268  'qty' => 1,
269  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
270  ],
271  ]
272  ]
273  ];
274 
275  return [
276  [
277  'modifierName' => 'addSimpleProduct',
278  'data' => [$optionsData]
279  ],
280  ];
281  }
282 
287  private function getProductWithSamePrice()
288  {
289  $optionsData = [
290  [
291  'title' => 'Op1',
292  'required' => true,
293  'type' => 'checkbox',
294  'links' => [
295  [
296  'sku' => 'simple1',
297  'price' => 10,
298  'qty' => 1,
299  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
300  ],
301  [
302  'sku' => 'simple2',
303  'price' => 10,
304  'qty' => 1,
305  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
306  ],
307  [
308  'sku' => 'simple3',
309  'price' => 10,
310  'qty' => 1,
311  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
312  ]
313  ]
314  ]
315  ];
316 
317  return [
318  [
319  'modifierName' => 'addSimpleProduct',
320  'data' => [$optionsData]
321  ],
322  ];
323  }
324 
329  private function getProductWithDifferentPrice()
330  {
331  $optionsData = [
332  [
333  'title' => 'Op1',
334  'required' => true,
335  'type' => 'checkbox',
336  'links' => [
337  [
338  'sku' => 'simple1',
339  'price' => 10,
340  'qty' => 1,
341  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
342  ],
343  [
344  'sku' => 'simple2',
345  'price' => 20,
346  'qty' => 1,
347  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
348  ],
349  [
350  'sku' => 'simple3',
351  'price' => 30,
352  'qty' => 1,
353  'price_type' => LinkInterface::PRICE_TYPE_FIXED,
354  ]
355  ]
356  ]
357  ];
358 
359  return [
360  [
361  'modifierName' => 'addSimpleProduct',
362  'data' => [$optionsData]
363  ],
364  ];
365  }
366 
373  private function getBundleConfiguration3($selectionsPriceType, $customOptionsPriceType)
374  {
375  $optionsData = [
376  [
377  'title' => 'Op1',
378  'required' => true,
379  'type' => 'checkbox',
380  'links' => [
381  [
382  'sku' => 'simple1',
383  'qty' => 1,
384  'price' => 20,
385  'price_type' => $selectionsPriceType
386  ],
387  ]
388  ],
389  ];
390 
391  $customOptionsData = [
392  [
393  'price_type' => $customOptionsPriceType,
394  'title' => 'Test Field',
395  'type' => 'field',
396  'is_require' => 1,
397  'price' => 100,
398  'sku' => '1-text',
399  ]
400  ];
401 
402  return [
403  [
404  'modifierName' => 'addSimpleProduct',
405  'data' => [$optionsData]
406  ],
407  [
408  'modifierName' => 'addCustomOption',
409  'data' => [$customOptionsData]
410  ],
411  ];
412  }
413 }
$bundleProduct