UpdateItemQtyTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Controller\Sidebar;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class UpdateItemQtyTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Checkout\Controller\Sidebar\UpdateItemQty */
  12. protected $updateItemQty;
  13. /** @var ObjectManagerHelper */
  14. protected $objectManagerHelper;
  15. /** @var \Magento\Checkout\Model\Sidebar|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $sidebarMock;
  17. /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $loggerMock;
  19. /** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $jsonHelperMock;
  21. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $requestMock;
  23. /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $responseMock;
  25. protected function setUp()
  26. {
  27. $this->sidebarMock = $this->createMock(\Magento\Checkout\Model\Sidebar::class);
  28. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  29. $this->jsonHelperMock = $this->createMock(\Magento\Framework\Json\Helper\Data::class);
  30. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  31. $this->responseMock = $this->getMockForAbstractClass(
  32. \Magento\Framework\App\ResponseInterface::class,
  33. [],
  34. '',
  35. false,
  36. true,
  37. true,
  38. ['representJson']
  39. );
  40. $this->objectManagerHelper = new ObjectManagerHelper($this);
  41. $this->updateItemQty = $this->objectManagerHelper->getObject(
  42. \Magento\Checkout\Controller\Sidebar\UpdateItemQty::class,
  43. [
  44. 'sidebar' => $this->sidebarMock,
  45. 'logger' => $this->loggerMock,
  46. 'jsonHelper' => $this->jsonHelperMock,
  47. 'request' => $this->requestMock,
  48. 'response' => $this->responseMock,
  49. ]
  50. );
  51. }
  52. public function testExecute()
  53. {
  54. $this->requestMock->expects($this->at(0))
  55. ->method('getParam')
  56. ->with('item_id', null)
  57. ->willReturn('1');
  58. $this->requestMock->expects($this->at(1))
  59. ->method('getParam')
  60. ->with('item_qty', null)
  61. ->willReturn('2');
  62. $this->sidebarMock->expects($this->once())
  63. ->method('checkQuoteItem')
  64. ->with(1)
  65. ->willReturnSelf();
  66. $this->sidebarMock->expects($this->once())
  67. ->method('updateQuoteItem')
  68. ->with(1, 2)
  69. ->willReturnSelf();
  70. $this->sidebarMock->expects($this->once())
  71. ->method('getResponseData')
  72. ->with('')
  73. ->willReturn(
  74. [
  75. 'data' => [
  76. 'summary_qty' => 2,
  77. 'summary_text' => __(' items'),
  78. 'subtotal' => 12.34,
  79. ],
  80. ]
  81. );
  82. $this->jsonHelperMock->expects($this->once())
  83. ->method('jsonEncode')
  84. ->with(
  85. [
  86. 'data' => [
  87. 'summary_qty' => 2,
  88. 'summary_text' => __(' items'),
  89. 'subtotal' => 12.34,
  90. ],
  91. ]
  92. )
  93. ->willReturn('json encoded');
  94. $this->responseMock->expects($this->once())
  95. ->method('representJson')
  96. ->with('json encoded')
  97. ->willReturn('json represented');
  98. $this->assertEquals('json represented', $this->updateItemQty->execute());
  99. }
  100. public function testExecuteWithLocalizedException()
  101. {
  102. $this->requestMock->expects($this->at(0))
  103. ->method('getParam')
  104. ->with('item_id', null)
  105. ->willReturn('1');
  106. $this->requestMock->expects($this->at(1))
  107. ->method('getParam')
  108. ->with('item_qty', null)
  109. ->willReturn('2');
  110. $this->sidebarMock->expects($this->once())
  111. ->method('checkQuoteItem')
  112. ->with(1)
  113. ->willThrowException(new LocalizedException(__('Error!')));
  114. $this->sidebarMock->expects($this->once())
  115. ->method('getResponseData')
  116. ->with('Error!')
  117. ->willReturn(
  118. [
  119. 'success' => false,
  120. 'error_message' => 'Error!',
  121. ]
  122. );
  123. $this->jsonHelperMock->expects($this->once())
  124. ->method('jsonEncode')
  125. ->with(
  126. [
  127. 'success' => false,
  128. 'error_message' => 'Error!',
  129. ]
  130. )
  131. ->willReturn('json encoded');
  132. $this->responseMock->expects($this->once())
  133. ->method('representJson')
  134. ->with('json encoded')
  135. ->willReturn('json represented');
  136. $this->assertEquals('json represented', $this->updateItemQty->execute());
  137. }
  138. public function testExecuteWithException()
  139. {
  140. $this->requestMock->expects($this->at(0))
  141. ->method('getParam')
  142. ->with('item_id', null)
  143. ->willReturn('1');
  144. $this->requestMock->expects($this->at(1))
  145. ->method('getParam')
  146. ->with('item_qty', null)
  147. ->willReturn('2');
  148. $exception = new \Exception('Error!');
  149. $this->sidebarMock->expects($this->once())
  150. ->method('checkQuoteItem')
  151. ->with(1)
  152. ->willThrowException($exception);
  153. $this->loggerMock->expects($this->once())
  154. ->method('critical')
  155. ->with($exception)
  156. ->willReturn(null);
  157. $this->sidebarMock->expects($this->once())
  158. ->method('getResponseData')
  159. ->with('Error!')
  160. ->willReturn(
  161. [
  162. 'success' => false,
  163. 'error_message' => 'Error!',
  164. ]
  165. );
  166. $this->jsonHelperMock->expects($this->once())
  167. ->method('jsonEncode')
  168. ->with(
  169. [
  170. 'success' => false,
  171. 'error_message' => 'Error!',
  172. ]
  173. )
  174. ->willReturn('json encoded');
  175. $this->responseMock->expects($this->once())
  176. ->method('representJson')
  177. ->with('json encoded')
  178. ->willReturn('json represented');
  179. $this->assertEquals('json represented', $this->updateItemQty->execute());
  180. }
  181. }