PriceBoxTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\Render;
  7. use Magento\Framework\Pricing\Render\PriceBox;
  8. /**
  9. * Test class for \Magento\Framework\Pricing\Render\PriceBox
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class PriceBoxTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var PriceBox
  21. */
  22. protected $model;
  23. /**
  24. * @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $context;
  27. /**
  28. * @var \Magento\Framework\Pricing\Render\RendererPool|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $rendererPool;
  31. /**
  32. * @var \Magento\Framework\Pricing\SaleableInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $saleable;
  35. /**
  36. * @var \Magento\Framework\Pricing\Price\PriceInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $price;
  39. protected function setUp()
  40. {
  41. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  42. $this->rendererPool = $this->getMockBuilder(\Magento\Framework\Pricing\Render\RendererPool::class)
  43. ->disableOriginalConstructor()
  44. ->setMethods(['createAmountRender'])
  45. ->getMock();
  46. $layout = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
  47. $eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  48. $scopeConfigMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  49. $cacheState = $this->getMockBuilder(\Magento\Framework\App\Cache\StateInterface::class)
  50. ->getMockForAbstractClass();
  51. $storeConfig = $this->getMockBuilder(\Magento\Store\Model\Store\Config::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
  55. ->setMethods(['getLayout', 'getEventManager', 'getStoreConfig', 'getScopeConfig', 'getCacheState'])
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->context->expects($this->any())
  59. ->method('getLayout')
  60. ->will($this->returnValue($layout));
  61. $this->context->expects($this->any())
  62. ->method('getEventManager')
  63. ->will($this->returnValue($eventManager));
  64. $this->context->expects($this->any())
  65. ->method('getStoreConfig')
  66. ->will($this->returnValue($storeConfig));
  67. $this->context->expects($this->any())
  68. ->method('getScopeConfig')
  69. ->will($this->returnValue($scopeConfigMock));
  70. $this->context->expects($this->any())
  71. ->method('getCacheState')
  72. ->will($this->returnValue($cacheState));
  73. $this->saleable = $this->createMock(\Magento\Framework\Pricing\SaleableInterface::class);
  74. $this->price = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
  75. $this->model = $this->objectManager->getObject(
  76. \Magento\Framework\Pricing\Render\PriceBox::class,
  77. [
  78. 'context' => $this->context,
  79. 'saleableItem' => $this->saleable,
  80. 'price' => $this->price,
  81. 'rendererPool' => $this->rendererPool
  82. ]
  83. );
  84. }
  85. /**
  86. * @param array $data
  87. * @param string $priceCode
  88. * @param array $cssClasses
  89. * @dataProvider toHtmlDataProvider
  90. */
  91. public function testToHtml($data, $priceCode, $cssClasses)
  92. {
  93. $this->price->expects($this->once())
  94. ->method('getPriceCode')
  95. ->will($this->returnValue($priceCode));
  96. $priceBox = $this->objectManager->getObject(
  97. \Magento\Framework\Pricing\Render\PriceBox::class,
  98. [
  99. 'context' => $this->context,
  100. 'saleableItem' => $this->saleable,
  101. 'price' => $this->price,
  102. 'rendererPool' => $this->rendererPool,
  103. 'data' => $data
  104. ]
  105. );
  106. $priceBox->toHtml();
  107. $this->assertEquals($cssClasses, $priceBox->getData('css_classes'));
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function toHtmlDataProvider()
  113. {
  114. return [
  115. [
  116. 'data' => [],
  117. 'price_code' => 'test_price',
  118. 'css_classes' => 'price-test_price',
  119. ],
  120. [
  121. 'data' => ['css_classes' => 'some_css_class'],
  122. 'price_code' => 'test_price',
  123. 'css_classes' => 'some_css_class price-test_price'
  124. ]];
  125. }
  126. public function testGetSaleableItem()
  127. {
  128. $this->assertEquals($this->saleable, $this->model->getSaleableItem());
  129. }
  130. public function testGetPrice()
  131. {
  132. $this->assertEquals($this->price, $this->model->getPrice());
  133. }
  134. public function testGetPriceType()
  135. {
  136. $priceCode = 'test_price';
  137. $price = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
  138. $priceInfo = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
  139. $priceInfo->expects($this->once())
  140. ->method('getPrice')
  141. ->with($priceCode)
  142. ->will($this->returnValue($price));
  143. $this->saleable->expects($this->once())
  144. ->method('getPriceInfo')
  145. ->will($this->returnValue($priceInfo));
  146. $this->assertEquals($price, $this->model->getPriceType($priceCode));
  147. }
  148. public function testRenderAmount()
  149. {
  150. $amount = $this->createMock(\Magento\Framework\Pricing\Amount\AmountInterface::class);
  151. $arguments = [];
  152. $resultHtml = 'result_html';
  153. $amountRender = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
  154. ->disableOriginalConstructor()
  155. ->setMethods(['toHtml'])
  156. ->getMock();
  157. $amountRender->expects($this->once())
  158. ->method('toHtml')
  159. ->will($this->returnValue($resultHtml));
  160. $this->rendererPool->expects($this->once())
  161. ->method('createAmountRender')
  162. ->with($amount, $this->saleable, $this->price, $arguments)
  163. ->will($this->returnValue($amountRender));
  164. $this->assertEquals($resultHtml, $this->model->renderAmount($amount, $arguments));
  165. }
  166. public function testGetPriceIdHasDataPriceId()
  167. {
  168. $priceId = 'data_price_id';
  169. $this->model->setData('price_id', $priceId);
  170. $this->assertEquals($priceId, $this->model->getPriceId());
  171. }
  172. /**
  173. * @dataProvider getPriceIdProvider
  174. * @param string $prefix
  175. * @param string $suffix
  176. * @param string $defaultPrefix
  177. * @param string $defaultSuffix
  178. */
  179. public function testGetPriceId($prefix, $suffix, $defaultPrefix, $defaultSuffix)
  180. {
  181. $priceId = 'price_id';
  182. $this->saleable->expects($this->once())
  183. ->method('getId')
  184. ->will($this->returnValue($priceId));
  185. if (!empty($prefix)) {
  186. $this->model->setData('price_id_prefix', $prefix);
  187. $expectedPriceId = $prefix . $priceId;
  188. } else {
  189. $expectedPriceId = $defaultPrefix . $priceId;
  190. }
  191. if (!empty($suffix)) {
  192. $this->model->setData('price_id_suffix', $suffix);
  193. $expectedPriceId = $expectedPriceId . $suffix;
  194. } else {
  195. $expectedPriceId = $expectedPriceId . $defaultSuffix;
  196. }
  197. $this->assertEquals($expectedPriceId, $this->model->getPriceId($defaultPrefix, $defaultSuffix));
  198. }
  199. /**
  200. * @return array
  201. */
  202. public function getPriceIdProvider()
  203. {
  204. return [
  205. ['prefix', 'suffix', 'default_prefix', 'default_suffix'],
  206. ['prefix', 'suffix', 'default_prefix', ''],
  207. ['prefix', 'suffix', '', 'default_suffix'],
  208. ['prefix', '', 'default_prefix', 'default_suffix'],
  209. ['', 'suffix', 'default_prefix', 'default_suffix'],
  210. ['', '', 'default_prefix', 'default_suffix'],
  211. ['prefix', 'suffix', '', '']
  212. ];
  213. }
  214. public function testGetRendererPool()
  215. {
  216. $this->assertEquals($this->rendererPool, $this->model->getRendererPool());
  217. }
  218. /**
  219. * This tests ensures that protected method getCacheLifetime() returns a null value when cacheLifeTime is not
  220. * explicitly set in the parent block
  221. */
  222. public function testCacheLifetime()
  223. {
  224. $reflectionClass = new \ReflectionClass(get_class($this->model));
  225. $methodReflection = $reflectionClass->getMethod('getCacheLifetime');
  226. $methodReflection->setAccessible(true);
  227. $cacheLifeTime = $methodReflection->invoke($this->model);
  228. $this->assertNull($cacheLifeTime, 'Expected null cache lifetime');
  229. }
  230. }