Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomOptionPriceTest.php
Go to the documentation of this file.
1 <?php
7 
9 
12 
18 class CustomOptionPriceTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $object;
24 
28  protected $product;
29 
33  protected $priceInfo;
34 
38  protected $calculator;
39 
43  protected $amount;
44 
48  protected $priceCurrencyMock;
49 
53  protected function setUp()
54  {
55  $this->product = $this->createPartialMock(
56  \Magento\Catalog\Model\Product::class,
57  ['getOptionById', '__wakeup', 'getPriceInfo', 'getOptions']
58  );
59 
60  $this->priceInfo = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
61 
62  $this->product->expects($this->any())
63  ->method('getPriceInfo')
64  ->will($this->returnValue($this->priceInfo));
65 
66  $this->calculator = $this->createMock(\Magento\Framework\Pricing\Adjustment\Calculator::class);
67 
68  $this->amount = $this->createMock(\Magento\Framework\Pricing\Amount\Base::class);
69 
70  $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
71 
72  $this->object = new CustomOptionPrice(
73  $this->product,
75  $this->calculator,
76  $this->priceCurrencyMock
77  );
78  }
79 
84  protected function setupOptions(array $optionsData)
85  {
86  $options = [];
87  foreach ($optionsData as $optionData) {
88  $optionValueMax = $this->getOptionValueMock($optionData['max_option_price']);
89  $optionValueMin = $this->getOptionValueMock($optionData['min_option_price']);
90 
91  $optionItemMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class)
92  ->disableOriginalConstructor()
93  ->setMethods(['getValues', '__wakeup', 'getIsRequire', 'getId', 'getType'])
94  ->getMock();
95  $optionItemMock->expects($this->any())
96  ->method('getId')
97  ->will($this->returnValue($optionData['id']));
98  $optionItemMock->expects($this->any())
99  ->method('getType')
100  ->will($this->returnValue($optionData['type']));
101  $optionItemMock->expects($this->any())
102  ->method('getIsRequire')
103  ->will($this->returnValue($optionData['is_require']));
104  $optionItemMock->expects($this->any())
105  ->method('getValues')
106  ->will($this->returnValue([$optionValueMax, $optionValueMin]));
107  $options[] = $optionItemMock;
108  }
109  return $options;
110  }
111 
117  {
118  $options = [];
119  foreach ($optionsData as $optionData) {
120  $optionItemMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class)
121  ->disableOriginalConstructor()
122  ->setMethods([
123  'getValues',
124  '__wakeup',
125  'getIsRequire',
126  'getId',
127  'getType',
128  'getPriceType',
129  'getPrice',
130  ])
131  ->getMock();
132  $optionItemMock->expects($this->any())
133  ->method('getId')
134  ->will($this->returnValue($optionData['id']));
135  $optionItemMock->expects($this->any())
136  ->method('getType')
137  ->will($this->returnValue($optionData['type']));
138  $optionItemMock->expects($this->any())
139  ->method('getIsRequire')
140  ->will($this->returnValue($optionData['is_require']));
141  $optionItemMock->expects($this->any())
142  ->method('getValues')
143  ->will($this->returnValue(null));
144  $optionItemMock->expects($this->any())
145  ->method('getPriceType')
146  ->willReturn($optionData['price_type']);
147  $optionItemMock->expects($this->any())
148  ->method('getPrice')
149  ->with($optionData['price_type'] == Value::TYPE_PERCENT)
150  ->willReturn($optionData['price']);
151  $options[] = $optionItemMock;
152  }
153  return $options;
154  }
155 
159  public function testGetValue()
160  {
161  $option1Id = 1;
162  $option1MaxPrice = 100;
163  $option1MinPrice = 10;
164  $option1Type = 'select';
165 
166  $option2Id = 2;
167  $option2MaxPrice = 200;
168  $option2MinPrice = 20;
170 
171  $optionsData = [
172  [
173  'id' => $option1Id,
174  'type' => $option1Type,
175  'max_option_price' => $option1MaxPrice,
176  'min_option_price' => $option1MinPrice,
177  'is_require' => true,
178  ],
179  [
180  'id' => $option2Id,
181  'type' => $option2Type,
182  'max_option_price' => $option2MaxPrice,
183  'min_option_price' => $option2MinPrice,
184  'is_require' => false,
185  ]
186  ];
187 
188  $singleValueOptionId = 3;
189  $singleValueOptionPrice = '50';
190  $singleValueOptionType = 'text';
191 
192  $singleValueOptions = $this->setupSingleValueOptions(
193  [
194  [
195  'id' => $singleValueOptionId,
196  'type' => $singleValueOptionType,
197  'price' => $singleValueOptionPrice,
198  'price_type' => 'fixed',
199  'is_require' => true,
200  ],
201  ]
202  );
203 
205  $options[] = $singleValueOptions[0];
206  $this->product->expects($this->once())
207  ->method('getOptions')
208  ->will($this->returnValue($options));
209 
210  $expectedResult = [
211  [
212  'option_id' => $option1Id,
213  'type' => $option1Type,
214  'min' => $option1MinPrice,
215  'max' => $option1MaxPrice,
216  ],
217  [
218  'option_id' => $option2Id,
219  'type' => $option2Type,
220  'min' => 0.,
221  'max' => $option2MaxPrice + $option2MinPrice,
222  ],
223  [
224  'option_id' => $singleValueOptionId,
225  'type' => $singleValueOptionType,
226  'min' => $singleValueOptionPrice,
227  'max' => $singleValueOptionPrice,
228  ]
229  ];
230  $result = $this->object->getValue();
231  $this->assertEquals($expectedResult, $result);
232  }
233 
234  public function testGetCustomOptionRange()
235  {
236  $option1Id = 1;
237  $option1MaxPrice = 100;
238  $option1MinPrice = 10;
239  $option1Type = 'select';
240 
241  $option2Id = '2';
242  $option2MaxPrice = 200;
243  $option2MinPrice = 20;
244  $option2Type = 'choice';
245 
246  $optionsData = [
247  [
248  'id' => $option1Id,
249  'type' => $option1Type,
250  'max_option_price' => $option1MaxPrice,
251  'min_option_price' => $option1MinPrice,
252  'is_require' => true,
253  ],
254  [
255  'id' => $option2Id,
256  'type' => $option2Type,
257  'max_option_price' => $option2MaxPrice,
258  'min_option_price' => $option2MinPrice,
259  'is_require' => false,
260  ]
261  ];
263 
264  $this->product->expects($this->any())
265  ->method('getOptions')
266  ->will($this->returnValue($options));
267 
268  $convertMinValue = $option1MinPrice / 2;
269  $convertedMaxValue = ($option2MaxPrice + $option1MaxPrice) / 2;
270  $this->priceCurrencyMock->expects($this->at(0))
271  ->method('convertAndRound')
272  ->with($option1MinPrice)
273  ->willReturn($convertMinValue);
274  $this->priceCurrencyMock->expects($this->at(1))
275  ->method('convertAndRound')
276  ->with($option2MaxPrice + $option1MaxPrice)
277  ->willReturn($convertedMaxValue);
278  $this->assertEquals($option1MinPrice / 2, $this->object->getCustomOptionRange(true));
279  $this->assertEquals($convertedMaxValue, $this->object->getCustomOptionRange(false));
280  }
281 
286  protected function getOptionValueMock($price)
287  {
288  $optionValueMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option\Value::class)
289  ->disableOriginalConstructor()
290  ->setMethods(['getPriceType', 'getPrice', 'getId', '__wakeup', 'getOption', 'getData'])
291  ->getMock();
292  $optionValueMock->expects($this->any())
293  ->method('getPriceType')
294  ->will($this->returnValue('percent'));
295  $optionValueMock->expects($this->any())
296  ->method('getPrice')
297  ->with($this->equalTo(true))
298  ->will($this->returnValue($price));
299 
300  $optionValueMock->expects($this->any())
301  ->method('getData')
302  ->with(\Magento\Catalog\Model\Product\Option\Value::KEY_PRICE)
303  ->willReturn($price);
304 
305  $optionMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class)
306  ->disableOriginalConstructor()
307  ->setMethods(['getProduct'])
308  ->getMock();
309 
310  $optionValueMock->expects($this->any())->method('getOption')->willReturn($optionMock);
311 
312  $optionMock->expects($this->any())->method('getProduct')->willReturn($this->product);
313 
314  $priceMock = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class)
315  ->disableOriginalConstructor()
316  ->setMethods(['getValue'])
317  ->getMockForAbstractClass();
318  $priceMock->method('getValue')->willReturn($price);
319 
320  $this->priceInfo->method('getPrice')->willReturn($priceMock);
321 
322  return $optionValueMock;
323  }
324 
328  public function testGetSelectedOptions()
329  {
330  $optionId1 = 1;
331  $optionId2 = 2;
332  $optionValue = 10;
333  $optionType = 'select';
334  $optionValueMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option\DefaultType::class)
335  ->disableOriginalConstructor()
336  ->setMethods(['getValue'])
337  ->getMock();
338  $optionMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class)
339  ->disableOriginalConstructor()
340  ->setMethods(['getId', 'getType', 'groupFactory', '__wakeup'])
341  ->getMock();
342  $groupMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option\Type\Select::class)
343  ->disableOriginalConstructor()
344  ->setMethods(['setOption', 'setConfigurationItemOption', 'getOptionPrice'])
345  ->getMock();
346 
347  $groupMock->expects($this->once())
348  ->method('setOption')
349  ->with($this->equalTo($optionMock))
350  ->will($this->returnSelf());
351  $groupMock->expects($this->once())
352  ->method('setConfigurationItemOption')
353  ->with($this->equalTo($optionValueMock))
354  ->will($this->returnSelf());
355  $groupMock->expects($this->once())
356  ->method('getOptionPrice')
357  ->with($this->equalTo($optionValue), $this->equalTo(0.))
358  ->will($this->returnValue($optionValue));
359  $optionMock->expects($this->at(0))
360  ->method('getId')
361  ->will($this->returnValue($optionId1));
362  $optionMock->expects($this->once())
363  ->method('getType')
364  ->will($this->returnValue($optionType));
365  $optionMock->expects($this->once())
366  ->method('groupFactory')
367  ->with($this->equalTo($optionType))
368  ->will($this->returnValue($groupMock));
369  $optionValueMock->expects($this->once())
370  ->method('getValue')
371  ->will($this->returnValue($optionValue));
372  $optionIds = new \Magento\Framework\DataObject(['value' => '1,2']);
373 
374  $customOptions = ['option_ids' => $optionIds, 'option_1' => $optionValueMock, 'option_2' => null];
375  $this->product->setCustomOptions($customOptions);
376  $this->product->expects($this->at(0))
377  ->method('getOptionById')
378  ->with($this->equalTo($optionId1))
379  ->will($this->returnValue($optionMock));
380  $this->product->expects($this->at(1))
381  ->method('getOptionById')
382  ->with($this->equalTo($optionId2))
383  ->will($this->returnValue(null));
384 
385  // Return from cache
386  $result = $this->object->getSelectedOptions();
387  $this->equalTo($optionValue, $result);
388  }
389 
393  public function testGetOptions()
394  {
395  $price = 100;
396  $displayValue = 120;
397  $id = 1;
398  $expected = [$id => [$price => ['base_amount' => $price, 'adjustment' => $displayValue]]];
399 
400  $this->amount->expects($this->once())
401  ->method('getValue')
402  ->will($this->returnValue(120));
403 
404  $this->calculator->expects($this->once())
405  ->method('getAmount')
406  ->will($this->returnValue($this->amount));
407 
408  $optionValueMock = $this->getOptionValueMock($price);
409  $optionValueMock->expects($this->once())
410  ->method('getId')
411  ->will($this->returnValue($id));
412  $optionItemMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class)
413  ->disableOriginalConstructor()
414  ->setMethods(['getValues', '__wakeup'])
415  ->getMock();
416  $optionItemMock->expects($this->any())
417  ->method('getValues')
418  ->will($this->returnValue([$optionValueMock]));
419  $options = [$optionItemMock];
420  $this->product->expects($this->once())
421  ->method('getOptions')
422  ->will($this->returnValue($options));
423  $result = $this->object->getOptions();
424  $this->assertEquals($expected, $result);
425  $result = $this->object->getOptions();
426  $this->assertEquals($expected, $result);
427  }
428 }
if( $block->displayPriceExclTax()||$block->displayBothPrices())(__('Excl. Tax')) ?>"> <?php if ($block -> displayPriceWithWeeeDetails()): ?> <span class="cart-tax-total" data-mage-init=' Magento Weee Helper Data Magento Weee Helper Data title amount
Definition: unit.phtml:68
$optionData
$id
Definition: fieldset.phtml:14
$price