RemoveItemTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class RemoveItemTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Checkout\Controller\Sidebar\RemoveItem */
  15. protected $removeItem;
  16. /** @var ObjectManagerHelper */
  17. protected $objectManagerHelper;
  18. /** @var \Magento\Checkout\Model\Sidebar|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $sidebarMock;
  20. /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $loggerMock;
  22. /** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $jsonHelperMock;
  24. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $requestMock;
  26. /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $responseMock;
  28. /** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $resultPageFactoryMock;
  30. /**
  31. * @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $resultRedirectFactory;
  34. protected function setUp()
  35. {
  36. $this->sidebarMock = $this->createMock(\Magento\Checkout\Model\Sidebar::class);
  37. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  38. $this->jsonHelperMock = $this->createMock(\Magento\Framework\Json\Helper\Data::class);
  39. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  40. $this->responseMock = $this->getMockForAbstractClass(
  41. \Magento\Framework\App\ResponseInterface::class,
  42. [],
  43. '',
  44. false,
  45. true,
  46. true,
  47. ['representJson']
  48. );
  49. $this->resultPageFactoryMock = $this->createMock(\Magento\Framework\View\Result\PageFactory::class);
  50. $this->resultRedirectFactory = $this->createPartialMock(
  51. \Magento\Framework\Controller\Result\RedirectFactory::class,
  52. ['create']
  53. );
  54. $this->objectManagerHelper = new ObjectManagerHelper($this);
  55. $this->removeItem = $this->objectManagerHelper->getObject(
  56. \Magento\Checkout\Controller\Sidebar\RemoveItem::class,
  57. [
  58. 'sidebar' => $this->sidebarMock,
  59. 'logger' => $this->loggerMock,
  60. 'jsonHelper' => $this->jsonHelperMock,
  61. 'request' => $this->requestMock,
  62. 'response' => $this->responseMock,
  63. 'resultPageFactory' => $this->resultPageFactoryMock,
  64. 'resultRedirectFactory' => $this->resultRedirectFactory
  65. ]
  66. );
  67. $formKeyValidatorMock = $this->createMock(\Magento\Framework\Data\Form\FormKey\Validator::class);
  68. $this->setPropertyValue($this->removeItem, 'formKeyValidator', $formKeyValidatorMock);
  69. }
  70. public function testExecute()
  71. {
  72. $this->getPropertyValue($this->removeItem, 'formKeyValidator')
  73. ->expects($this->once())
  74. ->method('validate')
  75. ->with($this->requestMock)
  76. ->willReturn(true);
  77. $this->requestMock->expects($this->once())
  78. ->method('getParam')
  79. ->with('item_id', null)
  80. ->willReturn('1');
  81. $this->sidebarMock->expects($this->once())
  82. ->method('checkQuoteItem')
  83. ->with(1)
  84. ->willReturnSelf();
  85. $this->sidebarMock->expects($this->once())
  86. ->method('removeQuoteItem')
  87. ->with(1)
  88. ->willReturnSelf();
  89. $this->sidebarMock->expects($this->once())
  90. ->method('getResponseData')
  91. ->with('')
  92. ->willReturn(
  93. [
  94. 'cleanup' => true,
  95. 'data' => [
  96. 'summary_qty' => 0,
  97. 'summary_text' => __(' items'),
  98. 'subtotal' => 0,
  99. ],
  100. ]
  101. );
  102. $this->jsonHelperMock->expects($this->once())
  103. ->method('jsonEncode')
  104. ->with(
  105. [
  106. 'cleanup' => true,
  107. 'data' => [
  108. 'summary_qty' => 0,
  109. 'summary_text' => __(' items'),
  110. 'subtotal' => 0,
  111. ],
  112. ]
  113. )
  114. ->willReturn('json encoded');
  115. $this->responseMock->expects($this->once())
  116. ->method('representJson')
  117. ->with('json encoded')
  118. ->willReturn('json represented');
  119. $this->assertEquals('json represented', $this->removeItem->execute());
  120. }
  121. public function testExecuteWithLocalizedException()
  122. {
  123. $this->getPropertyValue($this->removeItem, 'formKeyValidator')
  124. ->expects($this->once())
  125. ->method('validate')
  126. ->with($this->requestMock)
  127. ->willReturn(true);
  128. $this->requestMock->expects($this->once())
  129. ->method('getParam')
  130. ->with('item_id', null)
  131. ->willReturn('1');
  132. $this->sidebarMock->expects($this->once())
  133. ->method('checkQuoteItem')
  134. ->with(1)
  135. ->willThrowException(new LocalizedException(__('Error message!')));
  136. $this->sidebarMock->expects($this->once())
  137. ->method('getResponseData')
  138. ->with('Error message!')
  139. ->willReturn(
  140. [
  141. 'success' => false,
  142. 'error_message' => 'Error message!',
  143. ]
  144. );
  145. $this->jsonHelperMock->expects($this->once())
  146. ->method('jsonEncode')
  147. ->with(
  148. [
  149. 'success' => false,
  150. 'error_message' => 'Error message!',
  151. ]
  152. )
  153. ->willReturn('json encoded');
  154. $this->responseMock->expects($this->once())
  155. ->method('representJson')
  156. ->with('json encoded')
  157. ->willReturn('json represented');
  158. $this->assertEquals('json represented', $this->removeItem->execute());
  159. }
  160. public function testExecuteWithException()
  161. {
  162. $this->getPropertyValue($this->removeItem, 'formKeyValidator')
  163. ->expects($this->once())
  164. ->method('validate')
  165. ->with($this->requestMock)
  166. ->willReturn(true);
  167. $this->requestMock->expects($this->once())
  168. ->method('getParam')
  169. ->with('item_id', null)
  170. ->willReturn('1');
  171. $exception = new \Exception('Error message!');
  172. $this->sidebarMock->expects($this->once())
  173. ->method('checkQuoteItem')
  174. ->with(1)
  175. ->willThrowException($exception);
  176. $this->loggerMock->expects($this->once())
  177. ->method('critical')
  178. ->with($exception)
  179. ->willReturn(null);
  180. $this->sidebarMock->expects($this->once())
  181. ->method('getResponseData')
  182. ->with('Error message!')
  183. ->willReturn(
  184. [
  185. 'success' => false,
  186. 'error_message' => 'Error message!',
  187. ]
  188. );
  189. $this->jsonHelperMock->expects($this->once())
  190. ->method('jsonEncode')
  191. ->with(
  192. [
  193. 'success' => false,
  194. 'error_message' => 'Error message!',
  195. ]
  196. )
  197. ->willReturn('json encoded');
  198. $this->responseMock->expects($this->once())
  199. ->method('representJson')
  200. ->with('json encoded')
  201. ->willReturn('json represented');
  202. $this->assertEquals('json represented', $this->removeItem->execute());
  203. }
  204. public function testExecuteWhenFormKeyValidationFailed()
  205. {
  206. $resultRedirect = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
  207. $resultRedirect->expects($this->once())->method('setPath')->with('*/cart/')->willReturnSelf();
  208. $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($resultRedirect);
  209. $this->getPropertyValue($this->removeItem, 'formKeyValidator')
  210. ->expects($this->once())
  211. ->method('validate')
  212. ->with($this->requestMock)
  213. ->willReturn(false);
  214. $this->assertEquals($resultRedirect, $this->removeItem->execute());
  215. }
  216. /**
  217. * Get any object property value.
  218. *
  219. * @param $object
  220. * @param $property
  221. * @return mixed
  222. * @deprecated
  223. */
  224. protected function getPropertyValue($object, $property)
  225. {
  226. $reflection = new \ReflectionClass(get_class($object));
  227. $reflectionProperty = $reflection->getProperty($property);
  228. $reflectionProperty->setAccessible(true);
  229. return $reflectionProperty->getValue($object);
  230. }
  231. /**
  232. * Set object property value.
  233. *
  234. * @param $object
  235. * @param $property
  236. * @param $value
  237. * @deprecated
  238. */
  239. protected function setPropertyValue(&$object, $property, $value)
  240. {
  241. $reflection = new \ReflectionClass(get_class($object));
  242. $reflectionProperty = $reflection->getProperty($property);
  243. $reflectionProperty->setAccessible(true);
  244. $reflectionProperty->setValue($object, $value);
  245. return $object;
  246. }
  247. }