RenderTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Test\Unit;
  7. use \Magento\Framework\Pricing\Render;
  8. /**
  9. * Test class for \Magento\Framework\Pricing\Render
  10. */
  11. class RenderTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var Render
  15. */
  16. protected $model;
  17. /**
  18. * @var Render\Layout|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $priceLayout;
  21. /**
  22. * @var \Magento\Framework\Pricing\Price\PriceInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $price;
  25. /**
  26. * @var \Magento\Framework\Pricing\Amount\Base|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $amount;
  29. /**
  30. * @var \Magento\Framework\Pricing\SaleableInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $saleableItem;
  33. /**
  34. * @var \Magento\Framework\Pricing\Render\RendererPool|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $renderPool;
  37. protected function setUp()
  38. {
  39. $this->priceLayout = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Layout::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->price = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class)
  43. ->disableOriginalConstructor()
  44. ->getMockForAbstractClass();
  45. $this->amount = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->saleableItem = $this->getMockBuilder(\Magento\Framework\Pricing\SaleableInterface::class)
  49. ->disableOriginalConstructor()
  50. ->getMockForAbstractClass();
  51. $this->renderPool = $this->getMockBuilder(\Magento\Framework\Pricing\Render\RendererPool::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  55. $this->model = $objectManager->getObject(
  56. \Magento\Framework\Pricing\Render::class,
  57. [
  58. 'priceLayout' => $this->priceLayout
  59. ]
  60. );
  61. }
  62. public function testSetLayout()
  63. {
  64. $priceRenderHandle = 'price_render_handle';
  65. $this->priceLayout->expects($this->once())
  66. ->method('addHandle')
  67. ->with($priceRenderHandle);
  68. $this->priceLayout->expects($this->once())
  69. ->method('loadLayout');
  70. $layout = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
  71. $this->model->setPriceRenderHandle($priceRenderHandle);
  72. $this->model->setLayout($layout);
  73. }
  74. /**
  75. * @expectedException \RuntimeException
  76. */
  77. public function testRenderWithoutRenderList()
  78. {
  79. $priceType = 'final';
  80. $arguments = ['param' => 1];
  81. $result = '';
  82. $this->priceLayout->expects($this->once())
  83. ->method('getBlock')
  84. ->with('render.product.prices')
  85. ->will($this->returnValue(false));
  86. $this->assertEquals($result, $this->model->render($priceType, $this->saleableItem, $arguments));
  87. }
  88. public function testRender()
  89. {
  90. $priceType = 'final';
  91. $arguments = ['param' => 1];
  92. $result = 'simple.final';
  93. $pricingRender = $this->createMock(\Magento\Framework\Pricing\Render::class);
  94. $this->renderPool->expects($this->once())
  95. ->method('createPriceRender')
  96. ->will($this->returnValue($pricingRender));
  97. $pricingRender->expects($this->once())
  98. ->method('toHtml')
  99. ->will($this->returnValue('simple.final'));
  100. $this->priceLayout->expects($this->once())
  101. ->method('getBlock')
  102. ->with('render.product.prices')
  103. ->will($this->returnValue($this->renderPool));
  104. $this->assertEquals($result, $this->model->render($priceType, $this->saleableItem, $arguments));
  105. }
  106. public function testRenderDefault()
  107. {
  108. $priceType = 'special';
  109. $arguments = ['param' => 15];
  110. $result = 'default.special';
  111. $pricingRender = $this->createMock(\Magento\Framework\Pricing\Render::class);
  112. $this->renderPool->expects($this->once())
  113. ->method('createPriceRender')
  114. ->will($this->returnValue($pricingRender));
  115. $pricingRender->expects($this->once())
  116. ->method('toHtml')
  117. ->will($this->returnValue('default.special'));
  118. $this->priceLayout->expects($this->once())
  119. ->method('getBlock')
  120. ->with('render.product.prices')
  121. ->will($this->returnValue($this->renderPool));
  122. $this->assertEquals($result, $this->model->render($priceType, $this->saleableItem, $arguments));
  123. }
  124. public function testRenderDefaultDefault()
  125. {
  126. $priceType = 'final';
  127. $arguments = ['param' => 15];
  128. $result = 'default.default';
  129. $pricingRender = $this->createMock(\Magento\Framework\Pricing\Render::class);
  130. $this->renderPool->expects($this->once())
  131. ->method('createPriceRender')
  132. ->will($this->returnValue($pricingRender));
  133. $pricingRender->expects($this->once())
  134. ->method('toHtml')
  135. ->will($this->returnValue('default.default'));
  136. $this->priceLayout->expects($this->once())
  137. ->method('getBlock')
  138. ->with('render.product.prices')
  139. ->will($this->returnValue($this->renderPool));
  140. $this->assertEquals($result, $this->model->render($priceType, $this->saleableItem, $arguments));
  141. }
  142. public function testAmountRender()
  143. {
  144. $arguments = ['param' => 15];
  145. $expectedResult = 'default.default';
  146. $pricingRender = $this->createMock(
  147. \Magento\Framework\Pricing\Render\Amount::class
  148. );
  149. $this->renderPool->expects($this->once())
  150. ->method('createAmountRender')
  151. ->with(
  152. $this->equalTo($this->amount),
  153. $this->equalTo($this->saleableItem),
  154. $this->equalTo($this->price),
  155. $this->equalTo($arguments)
  156. )
  157. ->will($this->returnValue($pricingRender));
  158. $pricingRender->expects($this->once())
  159. ->method('toHtml')
  160. ->will($this->returnValue('default.default'));
  161. $this->priceLayout->expects($this->once())
  162. ->method('getBlock')
  163. ->with('render.product.prices')
  164. ->will($this->returnValue($this->renderPool));
  165. $result = $this->model->renderAmount($this->amount, $this->price, $this->saleableItem, $arguments);
  166. $this->assertEquals($expectedResult, $result);
  167. }
  168. /**
  169. * @expectedException \RuntimeException
  170. * @expectedExceptionMessage Wrong Price Rendering layout configuration. Factory block is missed
  171. */
  172. public function testAmountRenderNoRenderPool()
  173. {
  174. $this->priceLayout->expects($this->once())
  175. ->method('getBlock')
  176. ->with('render.product.prices')
  177. ->will($this->returnValue(false));
  178. $this->model->renderAmount($this->amount, $this->price, $this->saleableItem);
  179. }
  180. }