Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DynamicBundlePriceCalculatorWithDimensionTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 {
26  public function testPriceForDynamicBundle(array $strategyModifiers, array $expectedResults)
27  {
28  $this->prepareFixture($strategyModifiers, 'bundle_product');
29  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
30 
32  $priceInfo = $bundleProduct->getPriceInfo();
34 
35  $this->assertEquals(
36  $expectedResults['minimalPrice'],
37  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
38  'Failed to check minimal price on product'
39  );
40  $this->assertEquals(
41  $expectedResults['maximalPrice'],
42  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
43  'Failed to check maximal price on product'
44  );
45 
46  $priceInfoFromIndexer = $this->productCollectionFactory->create()
47  ->addFieldToFilter('sku', 'bundle_product')
48  ->addPriceData()
49  ->load()
50  ->getFirstItem();
51 
52  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
53  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
54  }
55 
65  public function testPriceForDynamicBundleInWebsiteScope(array $strategyModifiers, array $expectedResults)
66  {
67  $this->prepareFixture($strategyModifiers, 'bundle_product');
68  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
69 
71  $priceInfo = $bundleProduct->getPriceInfo();
73 
74  $this->assertEquals(
75  $expectedResults['minimalPrice'],
76  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
77  'Failed to check minimal price on product'
78  );
79  $this->assertEquals(
80  $expectedResults['maximalPrice'],
81  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
82  'Failed to check maximal price on product'
83  );
84 
85  $priceInfoFromIndexer = $this->productCollectionFactory->create()
86  ->addFieldToFilter('sku', 'bundle_product')
87  ->addPriceData()
88  ->load()
89  ->getFirstItem();
90 
91  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
92  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
93  }
94 
99  public function getTestCases()
100  {
101  return [
102  '#1 Testing price for dynamic bundle product with one simple' => [
103  'strategy' => $this->getBundleConfiguration1(),
104  'expectedResults' => [
105  // just price from simple1
106  'minimalPrice' => 10,
107  // just price from simple1
108  'maximalPrice' => 10
109  ]
110  ],
111 
112  '#2 Testing price for dynamic bundle product with three simples and different qty' => [
113  'strategy' => $this->getBundleConfiguration2(),
114  'expectedResults' => [
115  // min price from simples 3*10 or 30
116  'minimalPrice' => 30,
117  // (3 * 10) + (2 * 20) + 30
118  'maximalPrice' => 100
119  ]
120  ],
121 
122  '#3 Testing price for dynamic bundle product with four simples and different price' => [
123  'strategy' => $this->getBundleConfiguration3(),
124  'expectedResults' => [
125  // 10
126  'minimalPrice' => 10,
127  // 10 + 20 + 30
128  'maximalPrice' => 60
129  ]
130  ],
131 
132  '#4 Testing price for dynamic bundle with two non required options' => [
133  'strategy' => $this->getBundleConfiguration4(),
134  'expectedResults' => [
135  // 1 * 10
136  'minimalPrice' => 10,
137  // 3 * 20 + 1 * 10 + 3 * 20
138  'maximalPrice' => 130
139  ]
140  ],
141 
142  '#5 Testing price for dynamic bundle with two required options' => [
143  'strategy' => $this->getBundleConfiguration5(),
144  'expectedResults' => [
145  // 1 * 10 + 1 * 10
146  'minimalPrice' => 20,
147  // 3 * 20 + 1 * 10 + 3 * 20
148  'maximalPrice' => 130
149  ]
150  ],
151  ];
152  }
153 
159  private function getBundleConfiguration1()
160  {
161  $optionsData = [
162  [
163  'title' => 'op1',
164  'required' => true,
165  'type' => 'checkbox',
166  'links' => [
167  [
168  'sku' => 'simple1',
169  'qty' => 1,
170  ],
171  ]
172  ],
173  ];
174 
175  return [
176  [
177  'modifierName' => 'addSimpleProduct',
178  'data' => [$optionsData]
179  ],
180  ];
181  }
182 
188  private function getBundleConfiguration2()
189  {
190  $optionsData = [
191  [
192  'title' => 'op1',
193  'required' => true,
194  'type' => 'checkbox',
195  'links' => [
196  [
197  'sku' => 'simple1',
198  'qty' => 3,
199  ],
200  [
201  'sku' => 'simple2',
202  'qty' => 2,
203  ],
204  [
205  'sku' => 'simple3',
206  'qty' => 1,
207  ],
208  ]
209  ]
210  ];
211 
212  return [
213  [
214  'modifierName' => 'addSimpleProduct',
215  'data' => [$optionsData]
216  ],
217  ];
218  }
219 
225  private function getBundleConfiguration3()
226  {
227  $optionsData = [
228  [
229  'title' => 'op1',
230  'required' => true,
231  'type' => 'checkbox',
232  'links' => [
233  [
234  'sku' => 'simple1',
235  'qty' => 1,
236  ],
237  [
238  'sku' => 'simple2',
239  'qty' => 1,
240  ],
241  [
242  'sku' => 'simple3',
243  'qty' => 1,
244  ]
245  ]
246  ]
247  ];
248 
249  return [
250  [
251  'modifierName' => 'addSimpleProduct',
252  'data' => [$optionsData]
253  ],
254  ];
255  }
256 
261  private function getBundleConfiguration4()
262  {
263  $optionsData = [
264  [
265  'title' => 'Op1',
266  'required' => false,
267  'type' => 'radio',
268  'links' => [
269  [
270  'sku' => 'simple1',
271  'qty' => 1,
272  ],
273  [
274  'sku' => 'simple2',
275  'qty' => 3,
276  ],
277  ]
278  ],
279  [
280  'title' => 'Op2',
281  'required' => false,
282  'type' => 'checkbox',
283  'links' => [
284  [
285  'sku' => 'simple1',
286  'qty' => 1,
287  ],
288  [
289  'sku' => 'simple2',
290  'qty' => 3,
291  ],
292  ]
293  ]
294  ];
295 
296  return [
297  [
298  'modifierName' => 'addSimpleProduct',
299  'data' => [$optionsData]
300  ],
301  ];
302  }
303 
308  private function getBundleConfiguration5()
309  {
310  $optionsData = [
311  [
312  'title' => 'Op1',
313  'required' => true,
314  'type' => 'radio',
315  'links' => [
316  [
317  'sku' => 'simple1',
318  'qty' => 1,
319  ],
320  [
321  'sku' => 'simple2',
322  'qty' => 3,
323  ],
324  ]
325  ],
326  [
327  'title' => 'Op2',
328  'required' => true,
329  'type' => 'checkbox',
330  'links' => [
331  [
332  'sku' => 'simple1',
333  'qty' => 1,
334  ],
335  [
336  'sku' => 'simple2',
337  'qty' => 3,
338  ],
339  ]
340  ]
341  ];
342 
343  return [
344  [
345  'modifierName' => 'addSimpleProduct',
346  'data' => [$optionsData]
347  ],
348  ];
349  }
350 }
$bundleProduct