NewShippingTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Test\Unit\Controller\Checkout\Address;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class NewShippingTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Multishipping\Controller\Checkout\Address\NewShipping
  15. */
  16. protected $controller;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $configMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $objectManagerMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $stateMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $viewMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $layoutMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $addressFormMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $urlMock;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $pageMock;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $titleMock;
  53. /**
  54. * @var \PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $checkoutMock;
  57. protected function setUp()
  58. {
  59. $objectManager = new ObjectManager($this);
  60. $this->configMock = $this->createMock(\Magento\Framework\View\Page\Config::class);
  61. $this->checkoutMock =
  62. $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
  63. $this->titleMock = $this->createMock(\Magento\Framework\View\Page\Title::class);
  64. $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  65. $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  66. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  67. $this->stateMock =
  68. $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class);
  69. $valueMap = [
  70. [\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class, $this->stateMock],
  71. [\Magento\Multishipping\Model\Checkout\Type\Multishipping::class, $this->checkoutMock]
  72. ];
  73. $this->objectManagerMock->expects($this->any())->method('get')->willReturnMap($valueMap);
  74. $request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  75. ->disableOriginalConstructor()
  76. ->setMethods([])
  77. ->getMockForAbstractClass();
  78. $response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  79. ->disableOriginalConstructor()
  80. ->setMethods([])
  81. ->getMockForAbstractClass();
  82. $contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  83. $contextMock->expects($this->atLeastOnce())
  84. ->method('getRequest')
  85. ->will($this->returnValue($request));
  86. $contextMock->expects($this->atLeastOnce())
  87. ->method('getResponse')
  88. ->will($this->returnValue($response));
  89. $contextMock->expects($this->any())->method('getView')->willReturn($this->viewMock);
  90. $contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  91. $methods = ['setTitle', 'getTitle', 'setSuccessUrl', 'setBackUrl', 'setErrorUrl', '__wakeUp'];
  92. $this->addressFormMock =
  93. $this->createPartialMock(\Magento\Customer\Block\Address\Edit::class, $methods);
  94. $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  95. $contextMock->expects($this->any())->method('getUrl')->willReturn($this->urlMock);
  96. $this->pageMock = $this->createMock(\Magento\Framework\View\Result\Page::class);
  97. $this->pageMock->expects($this->any())->method('getConfig')->willReturn($this->configMock);
  98. $this->configMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock);
  99. $this->viewMock->expects($this->any())->method('getPage')->willReturn($this->pageMock);
  100. $this->controller = $objectManager->getObject(
  101. \Magento\Multishipping\Controller\Checkout\Address\NewShipping::class,
  102. ['context' => $contextMock]
  103. );
  104. }
  105. /**
  106. * @param string $backUrl
  107. * @param string $shippingAddress
  108. * @param string $url
  109. * @dataProvider executeDataProvider
  110. */
  111. public function testExecute($backUrl, $shippingAddress, $url)
  112. {
  113. $this->stateMock
  114. ->expects($this->once())
  115. ->method('setActiveStep')
  116. ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::STEP_SELECT_ADDRESSES);
  117. $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
  118. $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
  119. $this->layoutMock
  120. ->expects($this->once())
  121. ->method('getBlock')
  122. ->with('customer_address_edit')
  123. ->willReturn($this->addressFormMock);
  124. $this->addressFormMock
  125. ->expects($this->once())
  126. ->method('setTitle')
  127. ->with('Create Shipping Address')
  128. ->willReturnSelf();
  129. $helperMock = $this->createPartialMock(\Magento\Multishipping\Helper\Data::class, ['__']);
  130. $helperMock->expects($this->any())->method('__')->willReturn('Create Shipping Address');
  131. $this->addressFormMock->expects($this->once())->method('setSuccessUrl')->with('success/url')->willReturnSelf();
  132. $this->addressFormMock->expects($this->once())->method('setErrorUrl')->with('error/url')->willReturnSelf();
  133. $valueMap = [
  134. ['*/*/shippingSaved', null, 'success/url'],
  135. ['*/*/*', null, 'error/url'],
  136. [$backUrl, null, $url]
  137. ];
  138. $this->urlMock->expects($this->any())->method('getUrl')->willReturnMap($valueMap);
  139. $this->titleMock->expects($this->once())->method('getDefault')->willReturn('default_title');
  140. $this->addressFormMock->expects($this->once())->method('getTitle')->willReturn('Address title');
  141. $this->titleMock->expects($this->once())->method('set')->with('Address title - default_title');
  142. $this->checkoutMock
  143. ->expects($this->once())
  144. ->method('getCustomerDefaultShippingAddress')
  145. ->willReturn($shippingAddress);
  146. $this->addressFormMock->expects($this->once())->method('setBackUrl')->with($url);
  147. $this->viewMock->expects($this->once())->method('renderLayout');
  148. $this->controller->execute();
  149. }
  150. /**
  151. * @return array
  152. */
  153. public function executeDataProvider()
  154. {
  155. return [
  156. 'shipping_address_exists' => ['*/checkout/addresses', 'shipping_address', 'back/address'],
  157. 'shipping_address_not_exist' => ['*/cart/', null, 'back/cart']
  158. ];
  159. }
  160. public function testExecuteWhenCustomerAddressBlockNotExist()
  161. {
  162. $this->stateMock
  163. ->expects($this->once())
  164. ->method('setActiveStep')
  165. ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::STEP_SELECT_ADDRESSES);
  166. $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
  167. $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
  168. $this->layoutMock
  169. ->expects($this->once())
  170. ->method('getBlock')
  171. ->with('customer_address_edit');
  172. $this->urlMock->expects($this->never())->method('getUrl');
  173. $this->viewMock->expects($this->once())->method('renderLayout');
  174. $this->controller->execute();
  175. }
  176. }