|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Controller\Address; |
| 7 | + |
| 8 | +use Magento\Customer\Controller\Address\Delete; |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 10 | + |
| 11 | +/** |
| 12 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 13 | + */ |
| 14 | +class DeleteTest extends \PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + /** @var Delete */ |
| 17 | + protected $model; |
| 18 | + |
| 19 | + /** @var \Magento\Framework\App\Action\Context */ |
| 20 | + protected $context; |
| 21 | + |
| 22 | + /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */ |
| 23 | + protected $sessionMock; |
| 24 | + |
| 25 | + /** @var \Magento\Framework\Data\Form\FormKey\Validator|\PHPUnit_Framework_MockObject_MockObject */ |
| 26 | + protected $validatorMock; |
| 27 | + |
| 28 | + /** @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 29 | + protected $addressRepositoryMock; |
| 30 | + |
| 31 | + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + protected $request; |
| 33 | + |
| 34 | + /** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 35 | + protected $address; |
| 36 | + |
| 37 | + /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 38 | + protected $messageManager; |
| 39 | + |
| 40 | + /** @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 41 | + protected $resultRedirectFactory; |
| 42 | + |
| 43 | + /** @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */ |
| 44 | + protected $resultRedirect; |
| 45 | + |
| 46 | + protected function setUp() |
| 47 | + { |
| 48 | + $this->sessionMock = $this->getMockBuilder('Magento\Customer\Model\Session') |
| 49 | + ->disableOriginalConstructor() |
| 50 | + ->getMock(); |
| 51 | + $this->validatorMock = $this->getMockBuilder('Magento\Framework\Data\Form\FormKey\Validator') |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->getMock(); |
| 54 | + $formFactoryMock = $this->getMockBuilder('Magento\Customer\Model\Metadata\FormFactory') |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->getMock(); |
| 57 | + $this->addressRepositoryMock = $this->getMockBuilder('Magento\Customer\Api\AddressRepositoryInterface') |
| 58 | + ->getMockForAbstractClass(); |
| 59 | + $addressInterfaceFactoryMock = $this->getMockBuilder('Magento\Customer\Api\Data\AddressInterfaceFactory') |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->setMethods(['create']) |
| 62 | + ->getMock(); |
| 63 | + $regionInterfaceFactoryMock = $this->getMockBuilder('Magento\Customer\Api\Data\RegionInterfaceFactory') |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->setMethods(['create']) |
| 66 | + ->getMock(); |
| 67 | + $dataObjectProcessorMock = $this->getMockBuilder('Magento\Framework\Reflection\DataObjectProcessor') |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->getMock(); |
| 70 | + $dataObjectHelperMock = $this->getMockBuilder('Magento\Framework\Api\DataObjectHelper') |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->getMock(); |
| 73 | + $forwardFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\ForwardFactory') |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->setMethods(['create']) |
| 76 | + ->getMock(); |
| 77 | + $pageFactoryMock = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory') |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->getMock(); |
| 80 | + $this->request = $this->getMockBuilder('Magento\Framework\App\RequestInterface') |
| 81 | + ->getMockForAbstractClass(); |
| 82 | + $this->address = $this->getMockBuilder('Magento\Customer\Api\Data\AddressInterface') |
| 83 | + ->getMockForAbstractClass(); |
| 84 | + $this->messageManager = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface') |
| 85 | + ->getMockForAbstractClass(); |
| 86 | + $this->resultRedirectFactory = $this->getMockBuilder('Magento\Framework\Controller\Result\RedirectFactory') |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
| 89 | + $this->resultRedirect = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect') |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->getMock(); |
| 92 | + |
| 93 | + $objectManager = new ObjectManagerHelper($this); |
| 94 | + $this->context = $objectManager->getObject( |
| 95 | + 'Magento\Framework\App\Action\Context', |
| 96 | + [ |
| 97 | + 'request' => $this->request, |
| 98 | + 'messageManager' => $this->messageManager, |
| 99 | + 'resultRedirectFactory' => $this->resultRedirectFactory, |
| 100 | + ] |
| 101 | + ); |
| 102 | + |
| 103 | + $this->model = new Delete( |
| 104 | + $this->context, |
| 105 | + $this->sessionMock, |
| 106 | + $this->validatorMock, |
| 107 | + $formFactoryMock, |
| 108 | + $this->addressRepositoryMock, |
| 109 | + $addressInterfaceFactoryMock, |
| 110 | + $regionInterfaceFactoryMock, |
| 111 | + $dataObjectProcessorMock, |
| 112 | + $dataObjectHelperMock, |
| 113 | + $forwardFactoryMock, |
| 114 | + $pageFactoryMock |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + public function testExecute() |
| 119 | + { |
| 120 | + $addressId = 1; |
| 121 | + $customerId = 2; |
| 122 | + |
| 123 | + $this->resultRedirectFactory->expects($this->once()) |
| 124 | + ->method('create') |
| 125 | + ->willReturn($this->resultRedirect); |
| 126 | + $this->request->expects($this->once()) |
| 127 | + ->method('getParam') |
| 128 | + ->with('id', false) |
| 129 | + ->willReturn($addressId); |
| 130 | + $this->validatorMock->expects($this->once()) |
| 131 | + ->method('validate') |
| 132 | + ->with($this->request) |
| 133 | + ->willReturn(true); |
| 134 | + $this->addressRepositoryMock->expects($this->once()) |
| 135 | + ->method('getById') |
| 136 | + ->with($addressId) |
| 137 | + ->willReturn($this->address); |
| 138 | + $this->sessionMock->expects($this->once()) |
| 139 | + ->method('getCustomerId') |
| 140 | + ->willReturn($customerId); |
| 141 | + $this->address->expects($this->once()) |
| 142 | + ->method('getCustomerId') |
| 143 | + ->willReturn($customerId); |
| 144 | + $this->addressRepositoryMock->expects($this->once()) |
| 145 | + ->method('deleteById') |
| 146 | + ->with($addressId); |
| 147 | + $this->messageManager->expects($this->once()) |
| 148 | + ->method('addSuccess') |
| 149 | + ->with(__('You deleted the address.')); |
| 150 | + $this->resultRedirect->expects($this->once()) |
| 151 | + ->method('setPath') |
| 152 | + ->with('*/*/index') |
| 153 | + ->willReturnSelf(); |
| 154 | + $this->assertSame($this->resultRedirect, $this->model->execute()); |
| 155 | + } |
| 156 | + |
| 157 | + public function testExecuteWithException() |
| 158 | + { |
| 159 | + $addressId = 1; |
| 160 | + $customerId = 2; |
| 161 | + |
| 162 | + $this->resultRedirectFactory->expects($this->once()) |
| 163 | + ->method('create') |
| 164 | + ->willReturn($this->resultRedirect); |
| 165 | + $this->request->expects($this->once()) |
| 166 | + ->method('getParam') |
| 167 | + ->with('id', false) |
| 168 | + ->willReturn($addressId); |
| 169 | + $this->validatorMock->expects($this->once()) |
| 170 | + ->method('validate') |
| 171 | + ->with($this->request) |
| 172 | + ->willReturn(true); |
| 173 | + $this->addressRepositoryMock->expects($this->once()) |
| 174 | + ->method('getById') |
| 175 | + ->with($addressId) |
| 176 | + ->willReturn($this->address); |
| 177 | + $this->sessionMock->expects($this->once()) |
| 178 | + ->method('getCustomerId') |
| 179 | + ->willReturn($customerId); |
| 180 | + $this->address->expects($this->once()) |
| 181 | + ->method('getCustomerId') |
| 182 | + ->willReturn(34); |
| 183 | + $exception = new \Exception('Exception'); |
| 184 | + $this->messageManager->expects($this->once()) |
| 185 | + ->method('addError') |
| 186 | + ->with(__('We can\'t delete the address right now.')) |
| 187 | + ->willThrowException($exception); |
| 188 | + $this->messageManager->expects($this->once()) |
| 189 | + ->method('addException') |
| 190 | + ->with($exception, __('We can\'t delete the address right now.')); |
| 191 | + $this->resultRedirect->expects($this->once()) |
| 192 | + ->method('setPath') |
| 193 | + ->with('*/*/index') |
| 194 | + ->willReturnSelf(); |
| 195 | + $this->assertSame($this->resultRedirect, $this->model->execute()); |
| 196 | + } |
| 197 | +} |
0 commit comments