Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DynamicBundlePriceCalculatorTest.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  $priceInfoFromIndexer = $this->productCollectionFactory->create()
43  ->addFieldToFilter('sku', 'bundle_product')
44  ->addPriceData()
45  ->load()
46  ->getFirstItem();
47 
48  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
49  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
50  }
51 
61  public function testPriceForDynamicBundleInWebsiteScope(array $strategyModifiers, array $expectedResults)
62  {
63  $this->prepareFixture($strategyModifiers, 'bundle_product');
64  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
65 
67  $priceInfo = $bundleProduct->getPriceInfo();
69 
70  $this->assertEquals(
71  $expectedResults['minimalPrice'],
72  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
73  'Failed to check minimal price on product'
74  );
75  $this->assertEquals(
76  $expectedResults['maximalPrice'],
77  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
78  'Failed to check maximal price on product'
79  );
80 
81  $priceInfoFromIndexer = $this->productCollectionFactory->create()
82  ->addFieldToFilter('sku', 'bundle_product')
83  ->addPriceData()
84  ->load()
85  ->getFirstItem();
86 
87  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
88  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
89  }
90 
95  public function getTestCases()
96  {
97  return [
98  '#1 Testing price for dynamic bundle product with one simple' => [
99  'strategy' => $this->getBundleConfiguration1(),
100  'expectedResults' => [
101  // just price from simple1
102  'minimalPrice' => 10,
103  // just price from simple1
104  'maximalPrice' => 10
105  ]
106  ],
107 
108  '#2 Testing price for dynamic bundle product with three simples and different qty' => [
109  'strategy' => $this->getBundleConfiguration2(),
110  'expectedResults' => [
111  // min price from simples 3*10 or 30
112  'minimalPrice' => 30,
113  // (3 * 10) + (2 * 20) + 30
114  'maximalPrice' => 100
115  ]
116  ],
117 
118  '#3 Testing price for dynamic bundle product with four simples and different price' => [
119  'strategy' => $this->getBundleConfiguration3(),
120  'expectedResults' => [
121  // 10
122  'minimalPrice' => 10,
123  // 10 + 20 + 30
124  'maximalPrice' => 60
125  ]
126  ],
127 
128  '#4 Testing price for dynamic bundle with two non required options' => [
129  'strategy' => $this->getBundleConfiguration4(),
130  'expectedResults' => [
131  // 1 * 10
132  'minimalPrice' => 10,
133  // 3 * 20 + 1 * 10 + 3 * 20
134  'maximalPrice' => 130
135  ]
136  ],
137 
138  '#5 Testing price for dynamic bundle with two required options' => [
139  'strategy' => $this->getBundleConfiguration5(),
140  'expectedResults' => [
141  // 1 * 10 + 1 * 10
142  'minimalPrice' => 20,
143  // 3 * 20 + 1 * 10 + 3 * 20
144  'maximalPrice' => 130
145  ]
146  ],
147  ];
148  }
149 
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 
184  private function getBundleConfiguration2()
185  {
186  $optionsData = [
187  [
188  'title' => 'op1',
189  'required' => true,
190  'type' => 'checkbox',
191  'links' => [
192  [
193  'sku' => 'simple1',
194  'qty' => 3,
195  ],
196  [
197  'sku' => 'simple2',
198  'qty' => 2,
199  ],
200  [
201  'sku' => 'simple3',
202  'qty' => 1,
203  ],
204  ]
205  ]
206  ];
207 
208  return [
209  [
210  'modifierName' => 'addSimpleProduct',
211  'data' => [$optionsData]
212  ],
213  ];
214  }
215 
221  private function getBundleConfiguration3()
222  {
223  $optionsData = [
224  [
225  'title' => 'op1',
226  'required' => true,
227  'type' => 'checkbox',
228  'links' => [
229  [
230  'sku' => 'simple1',
231  'qty' => 1,
232  ],
233  [
234  'sku' => 'simple2',
235  'qty' => 1,
236  ],
237  [
238  'sku' => 'simple3',
239  'qty' => 1,
240  ]
241  ]
242  ]
243  ];
244 
245  return [
246  [
247  'modifierName' => 'addSimpleProduct',
248  'data' => [$optionsData]
249  ],
250  ];
251  }
252 
257  private function getBundleConfiguration4()
258  {
259  $optionsData = [
260  [
261  'title' => 'Op1',
262  'required' => false,
263  'type' => 'radio',
264  'links' => [
265  [
266  'sku' => 'simple1',
267  'qty' => 1,
268  ],
269  [
270  'sku' => 'simple2',
271  'qty' => 3,
272  ],
273  ]
274  ],
275  [
276  'title' => 'Op2',
277  'required' => false,
278  'type' => 'checkbox',
279  'links' => [
280  [
281  'sku' => 'simple1',
282  'qty' => 1,
283  ],
284  [
285  'sku' => 'simple2',
286  'qty' => 3,
287  ],
288  ]
289  ]
290  ];
291 
292  return [
293  [
294  'modifierName' => 'addSimpleProduct',
295  'data' => [$optionsData]
296  ],
297  ];
298  }
299 
304  private function getBundleConfiguration5()
305  {
306  $optionsData = [
307  [
308  'title' => 'Op1',
309  'required' => true,
310  'type' => 'radio',
311  'links' => [
312  [
313  'sku' => 'simple1',
314  'qty' => 1,
315  ],
316  [
317  'sku' => 'simple2',
318  'qty' => 3,
319  ],
320  ]
321  ],
322  [
323  'title' => 'Op2',
324  'required' => true,
325  'type' => 'checkbox',
326  'links' => [
327  [
328  'sku' => 'simple1',
329  'qty' => 1,
330  ],
331  [
332  'sku' => 'simple2',
333  'qty' => 3,
334  ],
335  ]
336  ]
337  ];
338 
339  return [
340  [
341  'modifierName' => 'addSimpleProduct',
342  'data' => [$optionsData]
343  ],
344  ];
345  }
346 }
$bundleProduct