RendererPoolTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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\RendererPool;
  8. /**
  9. * Test class for \Magento\Framework\Pricing\Render\RendererPool
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class RendererPoolTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\Pricing\Render\RendererPool | \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $object;
  19. /**
  20. * @var \Magento\Framework\View\Layout | \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $layoutMock;
  23. /**
  24. * @var \Magento\Catalog\Model\Product | \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $productMock;
  27. /**
  28. * @var \Magento\Catalog\Pricing\Price\BasePrice | \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $priceMock;
  31. /**
  32. * @var \Magento\Framework\View\LayoutInterface | \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $contextMock;
  35. protected function setUp()
  36. {
  37. $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->contextMock->expects($this->any())
  44. ->method('getLayout')
  45. ->will($this->returnValue($this->layoutMock));
  46. $this->productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->priceMock = $this->getMockBuilder(\Magento\Catalog\Pricing\Price\BasePrice::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. }
  53. /**
  54. * Test createPriceRender() if not found render class name
  55. *
  56. * @expectedException \InvalidArgumentException
  57. * @expectedExceptionMessage Class name for price code "price_test" not registered
  58. */
  59. public function testCreatePriceRenderNoClassName()
  60. {
  61. $methodData = [];
  62. $priceCode = 'price_test';
  63. $data = [];
  64. $type = 'simple';
  65. $this->productMock->expects($this->once())
  66. ->method('getTypeId')
  67. ->will($this->returnValue($type));
  68. $testedClass = $this->createTestedEntity($data);
  69. $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
  70. $this->assertNull($result);
  71. }
  72. /**
  73. * Test createPriceRender() if not found price model
  74. *
  75. * @expectedException \InvalidArgumentException
  76. * @expectedExceptionMessage Price model for price code "price_test" not registered
  77. */
  78. public function testCreatePriceRenderNoPriceModel()
  79. {
  80. $methodData = [];
  81. $priceCode = 'price_test';
  82. $type = 'simple';
  83. $className = 'Test';
  84. $data = [
  85. $type => [
  86. 'prices' => [
  87. $priceCode => [
  88. 'render_class' => $className,
  89. ],
  90. ],
  91. ],
  92. ];
  93. $priceModel = null;
  94. $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $priceInfoMock->expects($this->once())
  98. ->method('getPrice')
  99. ->with($this->equalTo($priceCode))
  100. ->will($this->returnValue($priceModel));
  101. $this->productMock->expects($this->once())
  102. ->method('getTypeId')
  103. ->will($this->returnValue($type));
  104. $this->productMock->expects($this->once())
  105. ->method('getPriceInfo')
  106. ->will($this->returnValue($priceInfoMock));
  107. $testedClass = $this->createTestedEntity($data);
  108. $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
  109. $this->assertNull($result);
  110. }
  111. /**
  112. * Test createPriceRender() if not found price model
  113. *
  114. * @expectedException \InvalidArgumentException
  115. * @expectedExceptionMessage Block "Magento\Framework\View\Element\Template\Context" must implement
  116. * \Magento\Framework\Pricing\Render\PriceBoxRenderInterface
  117. */
  118. public function testCreatePriceRenderBlockNotPriceBox()
  119. {
  120. $methodData = [];
  121. $priceCode = 'price_test';
  122. $type = 'simple';
  123. $className = \Magento\Framework\View\Element\Template\Context::class;
  124. $data = [
  125. $type => [
  126. 'prices' => [
  127. $priceCode => [
  128. 'render_class' => $className,
  129. ],
  130. ],
  131. ],
  132. ];
  133. $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $priceInfoMock->expects($this->once())
  137. ->method('getPrice')
  138. ->with($this->equalTo($priceCode))
  139. ->will($this->returnValue($this->priceMock));
  140. $this->productMock->expects($this->once())
  141. ->method('getTypeId')
  142. ->will($this->returnValue($type));
  143. $this->productMock->expects($this->once())
  144. ->method('getPriceInfo')
  145. ->will($this->returnValue($priceInfoMock));
  146. $contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. $block = new \Magento\Framework\View\Element\Template($contextMock);
  150. $testedClass = $this->createTestedEntity($data);
  151. $arguments = [
  152. 'data' => $methodData,
  153. 'rendererPool' => $testedClass,
  154. 'price' => $this->priceMock,
  155. 'saleableItem' => $this->productMock,
  156. ];
  157. $this->layoutMock->expects($this->once())
  158. ->method('createBlock')
  159. ->with($this->equalTo($className), $this->equalTo(''), $this->equalTo($arguments))
  160. ->will($this->returnValue($block));
  161. $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
  162. $this->assertNull($result);
  163. }
  164. /**
  165. * Test createPriceRender()
  166. */
  167. public function testCreatePriceRender()
  168. {
  169. $methodData = [];
  170. $priceCode = 'price_test';
  171. $type = 'simple';
  172. $className = \Magento\Framework\View\Element\Template\Context::class;
  173. $template = 'template.phtml';
  174. $data = [
  175. $type => [
  176. 'prices' => [
  177. $priceCode => [
  178. 'render_class' => $className,
  179. 'render_template' => $template,
  180. ],
  181. ],
  182. ],
  183. ];
  184. $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
  185. ->disableOriginalConstructor()
  186. ->getMock();
  187. $priceInfoMock->expects($this->once())
  188. ->method('getPrice')
  189. ->with($this->equalTo($priceCode))
  190. ->will($this->returnValue($this->priceMock));
  191. $this->productMock->expects($this->once())
  192. ->method('getTypeId')
  193. ->will($this->returnValue($type));
  194. $this->productMock->expects($this->once())
  195. ->method('getPriceInfo')
  196. ->will($this->returnValue($priceInfoMock));
  197. $renderBlock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\PriceBox::class)
  198. ->disableOriginalConstructor()
  199. ->getMock();
  200. $renderBlock->expects($this->once())
  201. ->method('setTemplate')
  202. ->with($this->equalTo($template));
  203. $testedClass = $this->createTestedEntity($data);
  204. $arguments = [
  205. 'data' => $methodData,
  206. 'rendererPool' => $testedClass,
  207. 'price' => $this->priceMock,
  208. 'saleableItem' => $this->productMock,
  209. ];
  210. $this->layoutMock->expects($this->once())
  211. ->method('createBlock')
  212. ->with($this->equalTo($className), $this->equalTo(''), $this->equalTo($arguments))
  213. ->will($this->returnValue($renderBlock));
  214. $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
  215. $this->assertInstanceOf(\Magento\Framework\Pricing\Render\PriceBoxRenderInterface::class, $result);
  216. }
  217. /**
  218. * Test createAmountRender() if amount render class not found
  219. *
  220. * @expectedException \InvalidArgumentException
  221. * @expectedExceptionMessage There is no amount render class for price code "base_price_test"
  222. */
  223. public function testCreateAmountRenderNoAmountClass()
  224. {
  225. $data = [];
  226. $type = 'simple';
  227. $methodData = [];
  228. $priceCode = 'base_price_test';
  229. $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
  230. ->disableOriginalConstructor()
  231. ->getMock();
  232. $this->productMock->expects($this->once())
  233. ->method('getTypeId')
  234. ->will($this->returnValue($type));
  235. $this->priceMock->expects($this->once())
  236. ->method('getPriceCode')
  237. ->will($this->returnValue($priceCode));
  238. $testedClass = $this->createTestedEntity($data);
  239. $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
  240. $this->assertNull($result);
  241. }
  242. /**
  243. * Test createAmountRender() if amount render block not implement Amount interface
  244. *
  245. * @expectedException \InvalidArgumentException
  246. * @expectedExceptionMessage Block "Magento\Framework\View\Element\Template\Context"
  247. * must implement \Magento\Framework\Pricing\Render\AmountRenderInterface
  248. */
  249. public function testCreateAmountRenderNotAmountInterface()
  250. {
  251. $type = 'simple';
  252. $methodData = [];
  253. $priceCode = 'base_price_test';
  254. $amountRenderClass = \Magento\Framework\View\Element\Template\Context::class;
  255. $data = [
  256. $type => [
  257. 'prices' => [
  258. $priceCode => [
  259. 'amount_render_class' => $amountRenderClass,
  260. ],
  261. ],
  262. ],
  263. ];
  264. $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
  265. ->disableOriginalConstructor()
  266. ->getMock();
  267. $this->productMock->expects($this->once())
  268. ->method('getTypeId')
  269. ->will($this->returnValue($type));
  270. $this->priceMock->expects($this->once())
  271. ->method('getPriceCode')
  272. ->will($this->returnValue($priceCode));
  273. $contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $block = new \Magento\Framework\View\Element\Template($contextMock);
  277. $testedClass = $this->createTestedEntity($data);
  278. $arguments = [
  279. 'data' => $methodData,
  280. 'rendererPool' => $testedClass,
  281. 'amount' => $amountMock,
  282. 'saleableItem' => $this->productMock,
  283. 'price' => $this->priceMock,
  284. ];
  285. $this->layoutMock->expects($this->once())
  286. ->method('createBlock')
  287. ->with($this->equalTo($amountRenderClass), $this->equalTo(''), $this->equalTo($arguments))
  288. ->will($this->returnValue($block));
  289. $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
  290. $this->assertNull($result);
  291. }
  292. /**
  293. * Test createAmountRender()
  294. */
  295. public function testCreateAmountRender()
  296. {
  297. $type = 'simple';
  298. $methodData = [];
  299. $priceCode = 'base_price_test';
  300. $template = 'template.phtml';
  301. $amountRenderClass = \Magento\Framework\Pricing\Render\Amount::class;
  302. $data = [
  303. $type => [
  304. 'prices' => [
  305. $priceCode => [
  306. 'amount_render_class' => $amountRenderClass,
  307. 'amount_render_template' => $template,
  308. ],
  309. ],
  310. ],
  311. ];
  312. $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
  313. ->disableOriginalConstructor()
  314. ->getMock();
  315. $this->productMock->expects($this->once())
  316. ->method('getTypeId')
  317. ->will($this->returnValue($type));
  318. $this->priceMock->expects($this->once())
  319. ->method('getPriceCode')
  320. ->will($this->returnValue($priceCode));
  321. $blockMock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
  322. ->disableOriginalConstructor()
  323. ->getMock();
  324. $testedClass = $this->createTestedEntity($data);
  325. $arguments = [
  326. 'data' => $methodData,
  327. 'rendererPool' => $testedClass,
  328. 'amount' => $amountMock,
  329. 'saleableItem' => $this->productMock,
  330. 'price' => $this->priceMock,
  331. ];
  332. $this->layoutMock->expects($this->once())
  333. ->method('createBlock')
  334. ->with($this->equalTo($amountRenderClass), $this->equalTo(''), $this->equalTo($arguments))
  335. ->will($this->returnValue($blockMock));
  336. $blockMock->expects($this->once())
  337. ->method('setTemplate')
  338. ->with($this->equalTo($template));
  339. $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
  340. $this->assertInstanceOf(\Magento\Framework\Pricing\Render\AmountRenderInterface::class, $result);
  341. }
  342. /**
  343. * Test getAdjustmentRenders() with not existed adjustment render class
  344. */
  345. public function testGetAdjustmentRendersNoRenderClass()
  346. {
  347. $typeId = 'simple';
  348. $priceCode = 'base_price_test';
  349. $this->productMock->expects($this->once())
  350. ->method('getTypeId')
  351. ->will($this->returnValue($typeId));
  352. $this->priceMock->expects($this->once())
  353. ->method('getPriceCode')
  354. ->will($this->returnValue($priceCode));
  355. $code = 'test_code';
  356. $adjustments = [$code => 'some data'];
  357. $data = [
  358. 'default' => [
  359. 'adjustments' => $adjustments,
  360. ],
  361. ];
  362. $testedClass = $this->createTestedEntity($data);
  363. $result = $testedClass->getAdjustmentRenders($this->productMock, $this->priceMock);
  364. $this->assertNull($result);
  365. }
  366. /**
  367. * Test getAdjustmentRenders() with not existed adjustment render template
  368. */
  369. public function testGetAdjustmentRendersNoRenderTemplate()
  370. {
  371. $typeId = 'simple';
  372. $priceCode = 'base_price_test';
  373. $this->productMock->expects($this->once())
  374. ->method('getTypeId')
  375. ->will($this->returnValue($typeId));
  376. $this->priceMock->expects($this->once())
  377. ->method('getPriceCode')
  378. ->will($this->returnValue($priceCode));
  379. $code = 'test_code';
  380. $adjustments = [
  381. $code => [
  382. 'adjustment_render_class' => 'Test',
  383. ],
  384. ];
  385. $data = [
  386. 'default' => [
  387. 'adjustments' => $adjustments,
  388. ],
  389. ];
  390. $testedClass = $this->createTestedEntity($data);
  391. $result = $testedClass->getAdjustmentRenders($this->productMock, $this->priceMock);
  392. $this->assertNull($result);
  393. }
  394. /**
  395. * Test getAdjustmentRenders()
  396. */
  397. public function testGetAdjustmentRenders()
  398. {
  399. $typeId = 'simple';
  400. $priceCode = 'base_price_test';
  401. $class = \Magento\Framework\View\Element\Template::class;
  402. $template = 'template.phtml';
  403. $code = 'tax';
  404. $adjustments = [
  405. $priceCode => [
  406. $code => [
  407. 'adjustment_render_class' => $class,
  408. 'adjustment_render_template' => $template,
  409. ],
  410. ],
  411. ];
  412. $data = [
  413. 'default' => [
  414. 'adjustments' => $adjustments,
  415. ],
  416. ];
  417. $this->productMock->expects($this->once())
  418. ->method('getTypeId')
  419. ->will($this->returnValue($typeId));
  420. $this->priceMock->expects($this->once())
  421. ->method('getPriceCode')
  422. ->will($this->returnValue($priceCode));
  423. $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class)
  424. ->disableOriginalConstructor()
  425. ->getMock();
  426. $blockMock->expects($this->once())
  427. ->method('setTemplate')
  428. ->with($this->equalTo($template));
  429. $this->layoutMock->expects($this->once())
  430. ->method('createBlock')
  431. ->with($this->equalTo($class))
  432. ->will($this->returnValue($blockMock));
  433. $testedClass = $this->createTestedEntity($data);
  434. $result = $testedClass->getAdjustmentRenders($this->productMock, $this->priceMock);
  435. $this->assertArrayHasKey($code, $result);
  436. $this->assertInstanceOf(\Magento\Framework\View\Element\Template::class, $result[$code]);
  437. }
  438. /**
  439. * Test getAmountRenderBlockTemplate() through createAmountRender() in case when template not exists
  440. *
  441. * @expectedException \InvalidArgumentException
  442. * @expectedExceptionMessage For type "simple" amount render block not configured
  443. */
  444. public function testGetAmountRenderBlockTemplateNoTemplate()
  445. {
  446. $type = 'simple';
  447. $methodData = [];
  448. $priceCode = 'base_price_test';
  449. $template = false;
  450. $amountRenderClass = \Magento\Framework\Pricing\Render\Amount::class;
  451. $data = [
  452. $type => [
  453. 'prices' => [
  454. $priceCode => [
  455. 'amount_render_class' => $amountRenderClass,
  456. 'amount_render_template' => $template,
  457. ],
  458. ],
  459. ],
  460. ];
  461. $amountMock = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
  462. ->disableOriginalConstructor()
  463. ->getMock();
  464. $this->productMock->expects($this->once())
  465. ->method('getTypeId')
  466. ->will($this->returnValue($type));
  467. $this->priceMock->expects($this->once())
  468. ->method('getPriceCode')
  469. ->will($this->returnValue($priceCode));
  470. $blockMock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
  471. ->disableOriginalConstructor()
  472. ->getMock();
  473. $testedClass = $this->createTestedEntity($data);
  474. $arguments = [
  475. 'data' => $methodData,
  476. 'rendererPool' => $testedClass,
  477. 'amount' => $amountMock,
  478. 'saleableItem' => $this->productMock,
  479. 'price' => $this->priceMock,
  480. ];
  481. $this->layoutMock->expects($this->once())
  482. ->method('createBlock')
  483. ->with($this->equalTo($amountRenderClass), $this->equalTo(''), $this->equalTo($arguments))
  484. ->will($this->returnValue($blockMock));
  485. $result = $testedClass->createAmountRender($amountMock, $this->productMock, $this->priceMock, $methodData);
  486. $this->assertNull($result);
  487. }
  488. /**
  489. * Test getRenderBlockTemplate() through createPriceRender() in case when template not exists
  490. *
  491. * @expectedException \InvalidArgumentException
  492. * @expectedExceptionMessage Price code "price_test" render block not configured
  493. */
  494. public function testGetRenderBlockTemplate()
  495. {
  496. $methodData = [];
  497. $priceCode = 'price_test';
  498. $type = 'simple';
  499. $className = \Magento\Framework\View\Element\Template\Context::class;
  500. $template = false;
  501. $data = [
  502. $type => [
  503. 'prices' => [
  504. $priceCode => [
  505. 'render_class' => $className,
  506. 'render_template' => $template,
  507. ],
  508. ],
  509. ],
  510. ];
  511. $priceInfoMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
  512. ->disableOriginalConstructor()
  513. ->getMock();
  514. $priceInfoMock->expects($this->once())
  515. ->method('getPrice')
  516. ->with($this->equalTo($priceCode))
  517. ->will($this->returnValue($this->priceMock));
  518. $this->productMock->expects($this->once())
  519. ->method('getTypeId')
  520. ->will($this->returnValue($type));
  521. $this->productMock->expects($this->once())
  522. ->method('getPriceInfo')
  523. ->will($this->returnValue($priceInfoMock));
  524. $renderBlock = $this->getMockBuilder(\Magento\Framework\Pricing\Render\PriceBox::class)
  525. ->disableOriginalConstructor()
  526. ->getMock();
  527. $testedClass = $this->createTestedEntity($data);
  528. $arguments = [
  529. 'data' => $methodData,
  530. 'rendererPool' => $testedClass,
  531. 'price' => $this->priceMock,
  532. 'saleableItem' => $this->productMock,
  533. ];
  534. $this->layoutMock->expects($this->once())
  535. ->method('createBlock')
  536. ->with($this->equalTo($className), $this->equalTo(''), $this->equalTo($arguments))
  537. ->will($this->returnValue($renderBlock));
  538. $result = $testedClass->createPriceRender($priceCode, $this->productMock, $methodData);
  539. $this->assertInstanceOf(\Magento\Framework\Pricing\Render\PriceBoxRenderInterface::class, $result);
  540. }
  541. /**
  542. * Create tested object with specified parameters
  543. *
  544. * @param array $data
  545. * @return RendererPool
  546. */
  547. protected function createTestedEntity(array $data = [])
  548. {
  549. return $this->object = new \Magento\Framework\Pricing\Render\RendererPool($this->contextMock, $data);
  550. }
  551. }