EditAddressTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 EditAddressTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Multishipping\Controller\Checkout\Address\EditAddress
  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 $viewMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $layoutMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $addressFormMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $urlMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $pageMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $titleMock;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $request;
  49. protected function setUp()
  50. {
  51. $objectManager = new ObjectManager($this);
  52. $this->configMock = $this->createMock(\Magento\Framework\View\Page\Config::class);
  53. $this->checkoutMock =
  54. $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
  55. $this->titleMock = $this->createMock(\Magento\Framework\View\Page\Title::class);
  56. $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  57. $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  58. $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods([])
  61. ->getMockForAbstractClass();
  62. $response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods([])
  65. ->getMockForAbstractClass();
  66. $contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  67. $contextMock->expects($this->atLeastOnce())
  68. ->method('getRequest')
  69. ->will($this->returnValue($this->request));
  70. $contextMock->expects($this->atLeastOnce())
  71. ->method('getResponse')
  72. ->will($this->returnValue($response));
  73. $contextMock->expects($this->any())->method('getView')->willReturn($this->viewMock);
  74. $methods = ['setTitle', 'getTitle', 'setSuccessUrl', 'setBackUrl', 'setErrorUrl', '__wakeUp'];
  75. $this->addressFormMock =
  76. $this->createPartialMock(\Magento\Customer\Block\Address\Edit::class, $methods);
  77. $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  78. $contextMock->expects($this->any())->method('getUrl')->willReturn($this->urlMock);
  79. $this->pageMock = $this->createMock(\Magento\Framework\View\Result\Page::class);
  80. $this->pageMock->expects($this->any())->method('getConfig')->willReturn($this->configMock);
  81. $this->configMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock);
  82. $this->viewMock->expects($this->any())->method('getPage')->willReturn($this->pageMock);
  83. $this->controller = $objectManager->getObject(
  84. \Magento\Multishipping\Controller\Checkout\Address\EditAddress::class,
  85. ['context' => $contextMock]
  86. );
  87. }
  88. public function testExecute()
  89. {
  90. $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
  91. $this->request->expects($this->once())->method('getParam')->with('id')->willReturn(1);
  92. $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
  93. $this->layoutMock
  94. ->expects($this->once())
  95. ->method('getBlock')
  96. ->with('customer_address_edit')
  97. ->willReturn($this->addressFormMock);
  98. $this->addressFormMock
  99. ->expects($this->once())
  100. ->method('setTitle')
  101. ->with('Edit Address')
  102. ->willReturnSelf();
  103. $helperMock = $this->createPartialMock(\Magento\Multishipping\Helper\Data::class, ['__']);
  104. $helperMock->expects($this->any())->method('__')->willReturn('Edit Address');
  105. $valueMap = [
  106. ['*/*/selectBilling', null, 'success/url'],
  107. ['*/*/*', ['id' => 1], 'error/url'],
  108. ];
  109. $this->urlMock->expects($this->any())->method('getUrl')->willReturnMap($valueMap);
  110. $this->addressFormMock->expects($this->once())->method('setSuccessUrl')->with('success/url')->willReturnSelf();
  111. $this->addressFormMock->expects($this->once())->method('setErrorUrl')->with('error/url')->willReturnSelf();
  112. $this->titleMock->expects($this->once())->method('getDefault')->willReturn('default_title');
  113. $this->addressFormMock->expects($this->once())->method('getTitle')->willReturn('Address title');
  114. $this->titleMock->expects($this->once())->method('set')->with('Address title - default_title');
  115. $this->addressFormMock->expects($this->once())->method('setBackUrl')->with('success/url');
  116. $this->viewMock->expects($this->once())->method('renderLayout');
  117. $this->controller->execute();
  118. }
  119. public function testExecuteWhenCustomerAddressBlockNotExist()
  120. {
  121. $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
  122. $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
  123. $this->layoutMock
  124. ->expects($this->once())
  125. ->method('getBlock')
  126. ->with('customer_address_edit');
  127. $this->urlMock->expects($this->never())->method('getUrl');
  128. $this->viewMock->expects($this->once())->method('renderLayout');
  129. $this->controller->execute();
  130. }
  131. }