AbstractCartTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Block\Cart;
  7. use \Magento\Checkout\Block\Cart\AbstractCart;
  8. class AbstractCartTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  12. */
  13. protected $_objectManager;
  14. protected function setUp()
  15. {
  16. $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  17. }
  18. /**
  19. * @dataProvider getItemRendererDataProvider
  20. * @param string|null $type
  21. * @param string $expectedType
  22. */
  23. public function testGetItemRenderer($type, $expectedType)
  24. {
  25. $renderer = $this->createMock(\Magento\Framework\View\Element\RendererList::class);
  26. $renderer->expects(
  27. $this->once()
  28. )->method(
  29. 'getRenderer'
  30. )->with(
  31. $expectedType,
  32. AbstractCart::DEFAULT_TYPE
  33. )->will(
  34. $this->returnValue('rendererObject')
  35. );
  36. $layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getChildName', 'getBlock']);
  37. $layout->expects($this->once())->method('getChildName')->will($this->returnValue('renderer.list'));
  38. $layout->expects(
  39. $this->once()
  40. )->method(
  41. 'getBlock'
  42. )->with(
  43. 'renderer.list'
  44. )->will(
  45. $this->returnValue($renderer)
  46. );
  47. /** @var $block \Magento\Sales\Block\Items\AbstractItems */
  48. $block = $this->_objectManager->getObject(
  49. \Magento\Checkout\Block\Cart\AbstractCart::class,
  50. [
  51. 'context' => $this->_objectManager->getObject(
  52. \Magento\Backend\Block\Template\Context::class,
  53. ['layout' => $layout]
  54. )
  55. ]
  56. );
  57. $this->assertSame('rendererObject', $block->getItemRenderer($type));
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function getItemRendererDataProvider()
  63. {
  64. return [[null, AbstractCart::DEFAULT_TYPE], ['some-type', 'some-type']];
  65. }
  66. /**
  67. * @expectedException \RuntimeException
  68. * @expectedExceptionMessage Renderer list for block "" is not defined
  69. */
  70. public function testGetItemRendererThrowsExceptionForNonexistentRenderer()
  71. {
  72. $layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getChildName', 'getBlock']);
  73. $layout->expects($this->once())->method('getChildName')->will($this->returnValue(null));
  74. /** @var $block \Magento\Checkout\Block\Cart\AbstractCart */
  75. $block = $this->_objectManager->getObject(
  76. \Magento\Checkout\Block\Cart\AbstractCart::class,
  77. [
  78. 'context' => $this->_objectManager->getObject(
  79. \Magento\Backend\Block\Template\Context::class,
  80. ['layout' => $layout]
  81. )
  82. ]
  83. );
  84. $block->getItemRenderer('some-type');
  85. }
  86. /**
  87. * @param array $expectedResult
  88. * @param bool $isVirtual
  89. * @dataProvider getTotalsCacheDataProvider
  90. */
  91. public function testGetTotalsCache($expectedResult, $isVirtual)
  92. {
  93. $totals = $isVirtual ? ['billing_totals'] : ['shipping_totals'];
  94. $addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  95. $checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
  96. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  97. $checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
  98. $quoteMock->expects($this->once())->method('isVirtual')->willReturn($isVirtual);
  99. $quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock);
  100. $quoteMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock);
  101. $addressMock->expects($this->once())->method('getTotals')->willReturn($totals);
  102. /** @var \Magento\Checkout\Block\Cart\AbstractCart $model */
  103. $model = $this->_objectManager->getObject(
  104. \Magento\Checkout\Block\Cart\AbstractCart::class,
  105. ['checkoutSession' => $checkoutSessionMock]
  106. );
  107. $this->assertEquals($expectedResult, $model->getTotalsCache());
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function getTotalsCacheDataProvider()
  113. {
  114. return [
  115. [['billing_totals'], true],
  116. [['shipping_totals'], false]
  117. ];
  118. }
  119. }