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