ConfigureTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\Cart;
  7. use Magento\Framework\Controller\ResultFactory;
  8. /**
  9. * Shopping cart edit tests
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class ConfigureTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $objectManagerMock;
  19. /**
  20. * @var \Magento\Framework\Controller\ResultFactory | \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $resultFactoryMock;
  23. /**
  24. * @var \Magento\Framework\Controller\Result\Redirect | \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $resultRedirectMock;
  27. /**
  28. * @var \Magento\Framework\App\ResponseInterface | \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $responseMock;
  31. /**
  32. * @var \Magento\Framework\App\RequestInterface | \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $requestMock;
  35. /**
  36. * @var \Magento\Framework\Message\ManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $messageManagerMock;
  39. /**
  40. * @var \Magento\Checkout\Controller\Cart\Configure | \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $configureController;
  43. /**
  44. * @var \Magento\Framework\App\Action\Context | \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $contextMock;
  47. /**
  48. * @var \Magento\Checkout\Model\Cart | \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $cartMock;
  51. protected function setUp()
  52. {
  53. $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  54. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  55. $this->responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  56. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  57. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  58. $this->cartMock = $this->getMockBuilder(\Magento\Checkout\Model\Cart::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->contextMock->expects($this->once())
  68. ->method('getResultFactory')
  69. ->willReturn($this->resultFactoryMock);
  70. $this->contextMock->expects($this->once())
  71. ->method('getRequest')
  72. ->willReturn($this->requestMock);
  73. $this->contextMock->expects($this->once())
  74. ->method('getObjectManager')
  75. ->willReturn($this->objectManagerMock);
  76. $this->contextMock->expects($this->once())
  77. ->method('getMessageManager')
  78. ->willReturn($this->messageManagerMock);
  79. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  80. $this->configureController = $objectManagerHelper->getObject(
  81. \Magento\Checkout\Controller\Cart\Configure::class,
  82. [
  83. 'context' => $this->contextMock,
  84. 'cart' => $this->cartMock
  85. ]
  86. );
  87. }
  88. /**
  89. * Test checks controller call product view and send parameter to it
  90. *
  91. * @return void
  92. */
  93. public function testPrepareAndRenderCall()
  94. {
  95. $quoteId = 1;
  96. $actualProductId = 1;
  97. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $quoteItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $viewMock = $this->getMockBuilder(\Magento\Catalog\Helper\Product\View::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $pageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $buyRequestMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. //expects
  116. $this->requestMock->expects($this->at(0))
  117. ->method('getParam')
  118. ->with('id')
  119. ->willReturn($quoteId);
  120. $this->requestMock->expects($this->at(1))
  121. ->method('getParam')
  122. ->with('product_id')
  123. ->willReturn($actualProductId);
  124. $this->cartMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
  125. $quoteItemMock->expects($this->exactly(1))->method('getBuyRequest')->willReturn($buyRequestMock);
  126. $this->resultFactoryMock->expects($this->once())
  127. ->method('create')
  128. ->with(ResultFactory::TYPE_PAGE, [])
  129. ->willReturn($pageMock);
  130. $this->objectManagerMock->expects($this->at(0))
  131. ->method('get')
  132. ->with(\Magento\Catalog\Helper\Product\View::class)
  133. ->willReturn($viewMock);
  134. $viewMock->expects($this->once())->method('prepareAndRender')->with(
  135. $pageMock,
  136. $actualProductId,
  137. $this->configureController,
  138. $this->callback(
  139. function ($subject) use ($buyRequestMock) {
  140. return $subject->getBuyRequest() === $buyRequestMock;
  141. }
  142. )
  143. )->willReturn($pageMock);
  144. $quoteMock->expects($this->once())->method('getItemById')->willReturn($quoteItemMock);
  145. $quoteItemMock->expects($this->exactly(2))->method('getProduct')->willReturn($productMock);
  146. $productMock->expects($this->exactly(2))->method('getId')->willReturn($actualProductId);
  147. $this->assertSame($pageMock, $this->configureController->execute());
  148. }
  149. /**
  150. * Test checks controller redirect user to cart
  151. * if user request product id in cart edit page is not same as quota product id
  152. *
  153. * @return void
  154. */
  155. public function testRedirectWithWrongProductId()
  156. {
  157. $quotaId = 1;
  158. $productIdInQuota = 1;
  159. $productIdInRequest = null;
  160. $quoteItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  167. ->disableOriginalConstructor()
  168. ->getMock();
  169. $this->requestMock->expects($this->any())
  170. ->method('getParam')
  171. ->willReturnMap([
  172. ['id', null, $quotaId],
  173. ['product_id', null, $productIdInRequest]
  174. ]);
  175. $this->cartMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
  176. $quoteMock->expects($this->once())->method('getItemById')->willReturn($quoteItemMock);
  177. $quoteItemMock->expects($this->once())->method('getProduct')->willReturn($productMock);
  178. $productMock->expects($this->once())->method('getId')->willReturn($productIdInQuota);
  179. $this->messageManagerMock->expects($this->once())
  180. ->method('addErrorMessage')
  181. ->willReturn('');
  182. $this->resultRedirectMock->expects($this->once())
  183. ->method('setPath')
  184. ->with('checkout/cart', [])
  185. ->willReturnSelf();
  186. $this->resultFactoryMock->expects($this->once())
  187. ->method('create')
  188. ->with(ResultFactory::TYPE_REDIRECT, [])
  189. ->willReturn($this->resultRedirectMock);
  190. $this->assertSame($this->resultRedirectMock, $this->configureController->execute());
  191. }
  192. }