Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
RendererPoolTest.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class RendererPoolTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $object;
21 
25  protected $layoutMock;
26 
30  protected $productMock;
31 
35  protected $priceMock;
36 
40  protected $contextMock;
41 
42  protected function setUp()
43  {
44  $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47  $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->contextMock->expects($this->any())
51  ->method('getLayout')
52  ->will($this->returnValue($this->layoutMock));
53  $this->productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->priceMock = $this->getMockBuilder(\Magento\Catalog\Pricing\Price\BasePrice::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59  }
60 
68  {
69  $methodData = [];
70  $priceCode = 'price_test';
71  $data = [];
72  $type = 'simple';
73  $this->productMock->expects($this->once())
74  ->method('getTypeId')
75  ->will($this->returnValue($type));
76 
77  $testedClass = $this->createTestedEntity($data);
78  $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
79  $this->assertNull($result);
80  }
81 
89  {
90  $methodData = [];
91  $priceCode = 'price_test';
92  $type = 'simple';
93  $className = 'Test';
94  $data = [
95  $type => [
96  'prices' => [
97  $priceCode => [
98  'render_class' => $className,
99  ],
100  ],
101  ],
102  ];
103  $priceModel = null;
104 
105  $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  $priceInfoMock->expects($this->once())
109  ->method('getPrice')
110  ->with($this->equalTo($priceCode))
111  ->will($this->returnValue($priceModel));
112  $this->productMock->expects($this->once())
113  ->method('getTypeId')
114  ->will($this->returnValue($type));
115  $this->productMock->expects($this->once())
116  ->method('getPriceInfo')
117  ->will($this->returnValue($priceInfoMock));
118 
119  $testedClass = $this->createTestedEntity($data);
120  $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
121  $this->assertNull($result);
122  }
123 
132  {
133  $methodData = [];
134  $priceCode = 'price_test';
135  $type = 'simple';
136  $className = \Magento\Framework\View\Element\Template\Context::class;
137  $data = [
138  $type => [
139  'prices' => [
140  $priceCode => [
141  'render_class' => $className,
142  ],
143  ],
144  ],
145  ];
146 
147  $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
148  ->disableOriginalConstructor()
149  ->getMock();
150  $priceInfoMock->expects($this->once())
151  ->method('getPrice')
152  ->with($this->equalTo($priceCode))
153  ->will($this->returnValue($this->priceMock));
154  $this->productMock->expects($this->once())
155  ->method('getTypeId')
156  ->will($this->returnValue($type));
157  $this->productMock->expects($this->once())
158  ->method('getPriceInfo')
159  ->will($this->returnValue($priceInfoMock));
160 
161  $contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
162  ->disableOriginalConstructor()
163  ->getMock();
164  $block = new \Magento\Framework\View\Element\Template($contextMock);
165 
166  $testedClass = $this->createTestedEntity($data);
167 
168  $arguments = [
169  'data' => $methodData,
170  'rendererPool' => $testedClass,
171  'price' => $this->priceMock,
172  'saleableItem' => $this->productMock,
173  ];
174  $this->layoutMock->expects($this->once())
175  ->method('createBlock')
176  ->with($this->equalTo($className), $this->equalTo(''), $this->equalTo($arguments))
177  ->will($this->returnValue($block));
178 
179  $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
180  $this->assertNull($result);
181  }
182 
186  public function testCreatePriceRender()
187  {
188  $methodData = [];
189  $priceCode = 'price_test';
190  $type = 'simple';
191  $className = \Magento\Framework\View\Element\Template\Context::class;
192  $template = 'template.phtml';
193  $data = [
194  $type => [
195  'prices' => [
196  $priceCode => [
197  'render_class' => $className,
198  'render_template' => $template,
199  ],
200  ],
201  ],
202  ];
203 
204  $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
205  ->disableOriginalConstructor()
206  ->getMock();
207  $priceInfoMock->expects($this->once())
208  ->method('getPrice')
209  ->with($this->equalTo($priceCode))
210  ->will($this->returnValue($this->priceMock));
211  $this->productMock->expects($this->once())
212  ->method('getTypeId')
213  ->will($this->returnValue($type));
214  $this->productMock->expects($this->once())
215  ->method('getPriceInfo')
216  ->will($this->returnValue($priceInfoMock));
217 
218  $renderBlock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\PriceBox::class)
219  ->disableOriginalConstructor()
220  ->getMock();
221  $renderBlock->expects($this->once())
222  ->method('setTemplate')
223  ->with($this->equalTo($template));
224 
225  $testedClass = $this->createTestedEntity($data);
226 
227  $arguments = [
228  'data' => $methodData,
229  'rendererPool' => $testedClass,
230  'price' => $this->priceMock,
231  'saleableItem' => $this->productMock,
232  ];
233  $this->layoutMock->expects($this->once())
234  ->method('createBlock')
235  ->with($this->equalTo($className), $this->equalTo(''), $this->equalTo($arguments))
236  ->will($this->returnValue($renderBlock));
237 
238  $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
239  $this->assertInstanceOf(\Magento\Framework\Pricing\Render\PriceBoxRenderInterface::class, $result);
240  }
241 
249  {
250  $data = [];
251  $type = 'simple';
252  $methodData = [];
253  $priceCode = 'base_price_test';
254 
255  $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
256  ->disableOriginalConstructor()
257  ->getMock();
258  $this->productMock->expects($this->once())
259  ->method('getTypeId')
260  ->will($this->returnValue($type));
261  $this->priceMock->expects($this->once())
262  ->method('getPriceCode')
263  ->will($this->returnValue($priceCode));
264 
265  $testedClass = $this->createTestedEntity($data);
266  $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
267  $this->assertNull($result);
268  }
269 
278  {
279  $type = 'simple';
280  $methodData = [];
281  $priceCode = 'base_price_test';
282  $amountRenderClass = \Magento\Framework\View\Element\Template\Context::class;
283  $data = [
284  $type => [
285  'prices' => [
286  $priceCode => [
287  'amount_render_class' => $amountRenderClass,
288  ],
289  ],
290  ],
291  ];
292 
293  $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
294  ->disableOriginalConstructor()
295  ->getMock();
296  $this->productMock->expects($this->once())
297  ->method('getTypeId')
298  ->will($this->returnValue($type));
299  $this->priceMock->expects($this->once())
300  ->method('getPriceCode')
301  ->will($this->returnValue($priceCode));
302 
303  $contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
304  ->disableOriginalConstructor()
305  ->getMock();
306  $block = new \Magento\Framework\View\Element\Template($contextMock);
307 
308  $testedClass = $this->createTestedEntity($data);
309 
310  $arguments = [
311  'data' => $methodData,
312  'rendererPool' => $testedClass,
313  'amount' => $amountMock,
314  'saleableItem' => $this->productMock,
315  'price' => $this->priceMock,
316  ];
317 
318  $this->layoutMock->expects($this->once())
319  ->method('createBlock')
320  ->with($this->equalTo($amountRenderClass), $this->equalTo(''), $this->equalTo($arguments))
321  ->will($this->returnValue($block));
322 
323  $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
324  $this->assertNull($result);
325  }
326 
330  public function testCreateAmountRender()
331  {
332  $type = 'simple';
333  $methodData = [];
334  $priceCode = 'base_price_test';
335  $template = 'template.phtml';
336  $amountRenderClass = \Magento\Framework\Pricing\Render\Amount::class;
337  $data = [
338  $type => [
339  'prices' => [
340  $priceCode => [
341  'amount_render_class' => $amountRenderClass,
342  'amount_render_template' => $template,
343  ],
344  ],
345  ],
346  ];
347 
348  $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
349  ->disableOriginalConstructor()
350  ->getMock();
351  $this->productMock->expects($this->once())
352  ->method('getTypeId')
353  ->will($this->returnValue($type));
354  $this->priceMock->expects($this->once())
355  ->method('getPriceCode')
356  ->will($this->returnValue($priceCode));
357 
358  $blockMock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
359  ->disableOriginalConstructor()
360  ->getMock();
361 
362  $testedClass = $this->createTestedEntity($data);
363 
364  $arguments = [
365  'data' => $methodData,
366  'rendererPool' => $testedClass,
367  'amount' => $amountMock,
368  'saleableItem' => $this->productMock,
369  'price' => $this->priceMock,
370  ];
371 
372  $this->layoutMock->expects($this->once())
373  ->method('createBlock')
374  ->with($this->equalTo($amountRenderClass), $this->equalTo(''), $this->equalTo($arguments))
375  ->will($this->returnValue($blockMock));
376 
377  $blockMock->expects($this->once())
378  ->method('setTemplate')
379  ->with($this->equalTo($template));
380 
381  $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
382  $this->assertInstanceOf(\Magento\Framework\Pricing\Render\AmountRenderInterface::class, $result);
383  }
384 
389  {
390  $typeId = 'simple';
391  $priceCode = 'base_price_test';
392  $this->productMock->expects($this->once())
393  ->method('getTypeId')
394  ->will($this->returnValue($typeId));
395  $this->priceMock->expects($this->once())
396  ->method('getPriceCode')
397  ->will($this->returnValue($priceCode));
398 
399  $code = 'test_code';
400  $adjustments = [$code => 'some data'];
401  $data = [
402  'default' => [
403  'adjustments' => $adjustments,
404  ],
405  ];
406  $testedClass = $this->createTestedEntity($data);
407  $result = $testedClass->getAdjustmentRenders($this->productMock, $this->priceMock);
408  $this->assertNull($result);
409  }
410 
415  {
416  $typeId = 'simple';
417  $priceCode = 'base_price_test';
418  $this->productMock->expects($this->once())
419  ->method('getTypeId')
420  ->will($this->returnValue($typeId));
421  $this->priceMock->expects($this->once())
422  ->method('getPriceCode')
423  ->will($this->returnValue($priceCode));
424 
425  $code = 'test_code';
426  $adjustments = [
427  $code => [
428  'adjustment_render_class' => 'Test',
429  ],
430  ];
431  $data = [
432  'default' => [
433  'adjustments' => $adjustments,
434  ],
435  ];
436 
437  $testedClass = $this->createTestedEntity($data);
438  $result = $testedClass->getAdjustmentRenders($this->productMock, $this->priceMock);
439  $this->assertNull($result);
440  }
441 
445  public function testGetAdjustmentRenders()
446  {
447  $typeId = 'simple';
448  $priceCode = 'base_price_test';
449  $class = \Magento\Framework\View\Element\Template::class;
450  $template = 'template.phtml';
451 
452  $code = 'tax';
453  $adjustments = [
454  $priceCode => [
455  $code => [
456  'adjustment_render_class' => $class,
457  'adjustment_render_template' => $template,
458  ],
459  ],
460  ];
461  $data = [
462  'default' => [
463  'adjustments' => $adjustments,
464  ],
465  ];
466 
467  $this->productMock->expects($this->once())
468  ->method('getTypeId')
469  ->will($this->returnValue($typeId));
470  $this->priceMock->expects($this->once())
471  ->method('getPriceCode')
472  ->will($this->returnValue($priceCode));
473 
474  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class)
475  ->disableOriginalConstructor()
476  ->getMock();
477  $blockMock->expects($this->once())
478  ->method('setTemplate')
479  ->with($this->equalTo($template));
480 
481  $this->layoutMock->expects($this->once())
482  ->method('createBlock')
483  ->with($this->equalTo($class))
484  ->will($this->returnValue($blockMock));
485 
486  $testedClass = $this->createTestedEntity($data);
487  $result = $testedClass->getAdjustmentRenders($this->productMock, $this->priceMock);
488  $this->assertArrayHasKey($code, $result);
489  $this->assertInstanceOf(\Magento\Framework\View\Element\Template::class, $result[$code]);
490  }
491 
499  {
500  $type = 'simple';
501  $methodData = [];
502  $priceCode = 'base_price_test';
503  $template = false;
504  $amountRenderClass = \Magento\Framework\Pricing\Render\Amount::class;
505  $data = [
506  $type => [
507  'prices' => [
508  $priceCode => [
509  'amount_render_class' => $amountRenderClass,
510  'amount_render_template' => $template,
511  ],
512  ],
513  ],
514  ];
515 
516  $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
517  ->disableOriginalConstructor()
518  ->getMock();
519 
520  $this->productMock->expects($this->once())
521  ->method('getTypeId')
522  ->will($this->returnValue($type));
523  $this->priceMock->expects($this->once())
524  ->method('getPriceCode')
525  ->will($this->returnValue($priceCode));
526 
527  $blockMock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
528  ->disableOriginalConstructor()
529  ->getMock();
530 
531  $testedClass = $this->createTestedEntity($data);
532 
533  $arguments = [
534  'data' => $methodData,
535  'rendererPool' => $testedClass,
536  'amount' => $amountMock,
537  'saleableItem' => $this->productMock,
538  'price' => $this->priceMock,
539  ];
540 
541  $this->layoutMock->expects($this->once())
542  ->method('createBlock')
543  ->with($this->equalTo($amountRenderClass), $this->equalTo(''), $this->equalTo($arguments))
544  ->will($this->returnValue($blockMock));
545 
546  $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
547  $this->assertNull($result);
548  }
549 
556  public function testGetRenderBlockTemplate()
557  {
558  $methodData = [];
559  $priceCode = 'price_test';
560  $type = 'simple';
561  $className = \Magento\Framework\View\Element\Template\Context::class;
562  $template = false;
563  $data = [
564  $type => [
565  'prices' => [
566  $priceCode => [
567  'render_class' => $className,
568  'render_template' => $template,
569  ],
570  ],
571  ],
572  ];
573 
574  $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
575  ->disableOriginalConstructor()
576  ->getMock();
577  $priceInfoMock->expects($this->once())
578  ->method('getPrice')
579  ->with($this->equalTo($priceCode))
580  ->will($this->returnValue($this->priceMock));
581  $this->productMock->expects($this->once())
582  ->method('getTypeId')
583  ->will($this->returnValue($type));
584  $this->productMock->expects($this->once())
585  ->method('getPriceInfo')
586  ->will($this->returnValue($priceInfoMock));
587 
588  $renderBlock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\PriceBox::class)
589  ->disableOriginalConstructor()
590  ->getMock();
591 
592  $testedClass = $this->createTestedEntity($data);
593 
594  $arguments = [
595  'data' => $methodData,
596  'rendererPool' => $testedClass,
597  'price' => $this->priceMock,
598  'saleableItem' => $this->productMock,
599  ];
600  $this->layoutMock->expects($this->once())
601  ->method('createBlock')
602  ->with($this->equalTo($className), $this->equalTo(''), $this->equalTo($arguments))
603  ->will($this->returnValue($renderBlock));
604 
605  $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
606  $this->assertInstanceOf(\Magento\Framework\Pricing\Render\PriceBoxRenderInterface::class, $result);
607  }
608 
615  protected function createTestedEntity(array $data = [])
616  {
617  return $this->object = new \Magento\Framework\Pricing\Render\RendererPool($this->contextMock, $data);
618  }
619 }
$block
Definition: block.php:8
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
$arguments
$priceModel
Definition: default.phtml:16
$template
Definition: export.php:12
$code
Definition: info.phtml:12
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31