EditShippingTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 EditShippingTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Multishipping\Controller\Checkout\Address\EditShipping
  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. /**
  58. * @var \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $request;
  61. protected function setUp()
  62. {
  63. $objectManager = new ObjectManager($this);
  64. $this->configMock = $this->createMock(\Magento\Framework\View\Page\Config::class);
  65. $this->checkoutMock =
  66. $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
  67. $this->titleMock = $this->createMock(\Magento\Framework\View\Page\Title::class);
  68. $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  69. $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  70. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  71. $this->stateMock =
  72. $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class);
  73. $valueMap = [
  74. [\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class, $this->stateMock],
  75. [\Magento\Multishipping\Model\Checkout\Type\Multishipping::class, $this->checkoutMock]
  76. ];
  77. $this->objectManagerMock->expects($this->any())->method('get')->willReturnMap($valueMap);
  78. $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  79. ->disableOriginalConstructor()
  80. ->setMethods([])
  81. ->getMockForAbstractClass();
  82. $response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  83. ->disableOriginalConstructor()
  84. ->setMethods([])
  85. ->getMockForAbstractClass();
  86. $contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  87. $contextMock->expects($this->atLeastOnce())
  88. ->method('getRequest')
  89. ->will($this->returnValue($this->request));
  90. $contextMock->expects($this->atLeastOnce())
  91. ->method('getResponse')
  92. ->will($this->returnValue($response));
  93. $contextMock->expects($this->any())->method('getView')->willReturn($this->viewMock);
  94. $contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  95. $methods = ['setTitle', 'getTitle', 'setSuccessUrl', 'setBackUrl', 'setErrorUrl', '__wakeUp'];
  96. $this->addressFormMock =
  97. $this->createPartialMock(\Magento\Customer\Block\Address\Edit::class, $methods);
  98. $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  99. $contextMock->expects($this->any())->method('getUrl')->willReturn($this->urlMock);
  100. $this->pageMock = $this->createMock(\Magento\Framework\View\Result\Page::class);
  101. $this->pageMock->expects($this->any())->method('getConfig')->willReturn($this->configMock);
  102. $this->configMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock);
  103. $this->viewMock->expects($this->any())->method('getPage')->willReturn($this->pageMock);
  104. $this->controller = $objectManager->getObject(
  105. \Magento\Multishipping\Controller\Checkout\Address\EditShipping::class,
  106. ['context' => $contextMock]
  107. );
  108. }
  109. public function testExecute()
  110. {
  111. $this->stateMock
  112. ->expects($this->once())
  113. ->method('setActiveStep')
  114. ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::STEP_SHIPPING);
  115. $this->request->expects($this->once())->method('getParam')->with('id')->willReturn(1);
  116. $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
  117. $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
  118. $this->layoutMock
  119. ->expects($this->once())
  120. ->method('getBlock')
  121. ->with('customer_address_edit')
  122. ->willReturn($this->addressFormMock);
  123. $this->addressFormMock
  124. ->expects($this->once())
  125. ->method('setTitle')
  126. ->with('Edit Shipping Address')
  127. ->willReturnSelf();
  128. $helperMock = $this->createPartialMock(\Magento\Multishipping\Helper\Data::class, ['__']);
  129. $helperMock->expects($this->any())->method('__')->willReturn('Edit Shipping Address');
  130. $this->addressFormMock->expects($this->once())->method('setSuccessUrl')->with('success/url')->willReturnSelf();
  131. $this->addressFormMock->expects($this->once())->method('setErrorUrl')->with('error/url')->willReturnSelf();
  132. $valueMap = [
  133. ['*/*/editShippingPost', ['id' => 1], 'success/url'],
  134. ['*/*/*', null, 'error/url'],
  135. ['*/checkout/shipping', null, 'back/address']
  136. ];
  137. $this->urlMock->expects($this->any())->method('getUrl')->willReturnMap($valueMap);
  138. $this->titleMock->expects($this->once())->method('getDefault')->willReturn('default_title');
  139. $this->addressFormMock->expects($this->once())->method('getTitle')->willReturn('Address title');
  140. $this->titleMock->expects($this->once())->method('set')->with('Address title - default_title');
  141. $this->checkoutMock
  142. ->expects($this->once())
  143. ->method('getCustomerDefaultShippingAddress')
  144. ->willReturn('shipping_addres');
  145. $this->addressFormMock->expects($this->once())->method('setBackUrl')->with('back/address');
  146. $this->viewMock->expects($this->once())->method('renderLayout');
  147. $this->controller->execute();
  148. }
  149. public function testExecuteWhenCustomerAddressBlockNotExist()
  150. {
  151. $this->stateMock
  152. ->expects($this->once())
  153. ->method('setActiveStep')
  154. ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::STEP_SHIPPING);
  155. $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
  156. $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
  157. $this->layoutMock
  158. ->expects($this->once())
  159. ->method('getBlock')
  160. ->with('customer_address_edit');
  161. $this->urlMock->expects($this->never())->method('getUrl');
  162. $this->viewMock->expects($this->once())->method('renderLayout');
  163. $this->controller->execute();
  164. }
  165. }