NewBillingTest.php 5.7 KB

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