Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NewShippingTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class NewShippingTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $controller;
20 
24  protected $configMock;
25 
29  protected $objectManagerMock;
30 
34  protected $stateMock;
35 
39  protected $viewMock;
40 
44  protected $layoutMock;
45 
49  protected $addressFormMock;
50 
54  protected $urlMock;
55 
59  protected $pageMock;
60 
64  protected $titleMock;
65 
69  protected $checkoutMock;
70 
71  protected function setUp()
72  {
73  $objectManager = new ObjectManager($this);
74  $this->configMock = $this->createMock(\Magento\Framework\View\Page\Config::class);
75  $this->checkoutMock =
76  $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
77  $this->titleMock = $this->createMock(\Magento\Framework\View\Page\Title::class);
78  $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
79  $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
80  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
81  $this->stateMock =
82  $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class);
83  $valueMap = [
84  [\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class, $this->stateMock],
85  [\Magento\Multishipping\Model\Checkout\Type\Multishipping::class, $this->checkoutMock]
86  ];
87  $this->objectManagerMock->expects($this->any())->method('get')->willReturnMap($valueMap);
88  $request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
89  ->disableOriginalConstructor()
90  ->setMethods([])
91  ->getMockForAbstractClass();
92  $response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
93  ->disableOriginalConstructor()
94  ->setMethods([])
95  ->getMockForAbstractClass();
96  $contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
97  $contextMock->expects($this->atLeastOnce())
98  ->method('getRequest')
99  ->will($this->returnValue($request));
100  $contextMock->expects($this->atLeastOnce())
101  ->method('getResponse')
102  ->will($this->returnValue($response));
103  $contextMock->expects($this->any())->method('getView')->willReturn($this->viewMock);
104  $contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
105  $methods = ['setTitle', 'getTitle', 'setSuccessUrl', 'setBackUrl', 'setErrorUrl', '__wakeUp'];
106  $this->addressFormMock =
107  $this->createPartialMock(\Magento\Customer\Block\Address\Edit::class, $methods);
108  $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
109  $contextMock->expects($this->any())->method('getUrl')->willReturn($this->urlMock);
110  $this->pageMock = $this->createMock(\Magento\Framework\View\Result\Page::class);
111  $this->pageMock->expects($this->any())->method('getConfig')->willReturn($this->configMock);
112  $this->configMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock);
113  $this->viewMock->expects($this->any())->method('getPage')->willReturn($this->pageMock);
114  $this->controller = $objectManager->getObject(
115  \Magento\Multishipping\Controller\Checkout\Address\NewShipping::class,
116  ['context' => $contextMock]
117  );
118  }
119 
126  public function testExecute($backUrl, $shippingAddress, $url)
127  {
128  $this->stateMock
129  ->expects($this->once())
130  ->method('setActiveStep')
131  ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::STEP_SELECT_ADDRESSES);
132  $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
133  $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
134  $this->layoutMock
135  ->expects($this->once())
136  ->method('getBlock')
137  ->with('customer_address_edit')
138  ->willReturn($this->addressFormMock);
139  $this->addressFormMock
140  ->expects($this->once())
141  ->method('setTitle')
142  ->with('Create Shipping Address')
143  ->willReturnSelf();
144  $helperMock = $this->createPartialMock(\Magento\Multishipping\Helper\Data::class, ['__']);
145  $helperMock->expects($this->any())->method('__')->willReturn('Create Shipping Address');
146  $this->addressFormMock->expects($this->once())->method('setSuccessUrl')->with('success/url')->willReturnSelf();
147  $this->addressFormMock->expects($this->once())->method('setErrorUrl')->with('error/url')->willReturnSelf();
148  $valueMap = [
149  ['*/*/shippingSaved', null, 'success/url'],
150  ['*/*/*', null, 'error/url'],
151  [$backUrl, null, $url]
152  ];
153  $this->urlMock->expects($this->any())->method('getUrl')->willReturnMap($valueMap);
154  $this->titleMock->expects($this->once())->method('getDefault')->willReturn('default_title');
155  $this->addressFormMock->expects($this->once())->method('getTitle')->willReturn('Address title');
156  $this->titleMock->expects($this->once())->method('set')->with('Address title - default_title');
157  $this->checkoutMock
158  ->expects($this->once())
159  ->method('getCustomerDefaultShippingAddress')
160  ->willReturn($shippingAddress);
161  $this->addressFormMock->expects($this->once())->method('setBackUrl')->with($url);
162  $this->viewMock->expects($this->once())->method('renderLayout');
163  $this->controller->execute();
164  }
165 
169  public function executeDataProvider()
170  {
171  return [
172  'shipping_address_exists' => ['*/checkout/addresses', 'shipping_address', 'back/address'],
173  'shipping_address_not_exist' => ['*/cart/', null, 'back/cart']
174  ];
175  }
176 
178  {
179  $this->stateMock
180  ->expects($this->once())
181  ->method('setActiveStep')
182  ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::STEP_SELECT_ADDRESSES);
183  $this->viewMock->expects($this->once())->method('loadLayout')->willReturnSelf();
184  $this->viewMock->expects($this->any())->method('getLayout')->willReturn($this->layoutMock);
185  $this->layoutMock
186  ->expects($this->once())
187  ->method('getBlock')
188  ->with('customer_address_edit');
189  $this->urlMock->expects($this->never())->method('getUrl');
190  $this->viewMock->expects($this->once())->method('renderLayout');
191  $this->controller->execute();
192  }
193 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
$shippingAddress
Definition: order.php:40
$methods
Definition: billing.phtml:71