|
| 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\Adminhtml\Group; |
| 7 | + |
| 8 | +use Magento\Customer\Api\Data\GroupInterfaceFactory; |
| 9 | +use Magento\Customer\Controller\Adminhtml\Group\Save; |
| 10 | +use Magento\Framework\Controller\Result\RedirectFactory; |
| 11 | +use Magento\Framework\Controller\Result\Redirect; |
| 12 | +use Magento\Framework\Reflection\DataObjectProcessor; |
| 13 | + |
| 14 | +/** |
| 15 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 16 | + */ |
| 17 | +class SaveTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + /** @var Save */ |
| 20 | + protected $controller; |
| 21 | + |
| 22 | + /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */ |
| 23 | + protected $contextMock; |
| 24 | + |
| 25 | + /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */ |
| 26 | + protected $registryMock; |
| 27 | + |
| 28 | + /** @var \Magento\Customer\Api\GroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 29 | + protected $groupRepositoryMock; |
| 30 | + |
| 31 | + /** @var GroupInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + protected $groupInterfaceFactoryMock; |
| 33 | + |
| 34 | + /** @var \Magento\Backend\Model\View\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 35 | + protected $forwardFactoryMock; |
| 36 | + |
| 37 | + /** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 38 | + protected $pageFactoryMock; |
| 39 | + |
| 40 | + /** @var DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject */ |
| 41 | + protected $dataObjectProcessorMock; |
| 42 | + |
| 43 | + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 44 | + protected $request; |
| 45 | + |
| 46 | + /** @var RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 47 | + protected $resultRedirectFactory; |
| 48 | + |
| 49 | + /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject */ |
| 50 | + protected $resultRedirect; |
| 51 | + |
| 52 | + /** @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 53 | + protected $customerGroup; |
| 54 | + |
| 55 | + /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 56 | + protected $messageManager; |
| 57 | + |
| 58 | + /** @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject */ |
| 59 | + protected $resultForward; |
| 60 | + |
| 61 | + /** @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 62 | + protected $group; |
| 63 | + |
| 64 | + /** @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject */ |
| 65 | + protected $session; |
| 66 | + |
| 67 | + protected function setUp() |
| 68 | + { |
| 69 | + $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class) |
| 73 | + ->getMockForAbstractClass(); |
| 74 | + $this->groupRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\GroupRepositoryInterface::class) |
| 75 | + ->getMockForAbstractClass(); |
| 76 | + $this->groupInterfaceFactoryMock = $this->getMockBuilder(GroupInterfaceFactory::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->getMock(); |
| 79 | + $this->forwardFactoryMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\ForwardFactory::class) |
| 80 | + ->disableOriginalConstructor() |
| 81 | + ->setMethods(['create']) |
| 82 | + ->getMock(); |
| 83 | + $this->pageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->getMock(); |
| 86 | + $this->dataObjectProcessorMock = $this->getMockBuilder(DataObjectProcessor::class) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
| 89 | + $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) |
| 90 | + ->getMockForAbstractClass(); |
| 91 | + $this->resultRedirectFactory = $this->getMockBuilder(RedirectFactory::class) |
| 92 | + ->disableOriginalConstructor() |
| 93 | + ->getMock(); |
| 94 | + $this->resultRedirect = $this->getMockBuilder(Redirect::class) |
| 95 | + ->disableOriginalConstructor() |
| 96 | + ->getMock(); |
| 97 | + $this->customerGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class) |
| 98 | + ->getMockForAbstractClass(); |
| 99 | + $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class) |
| 100 | + ->getMockForAbstractClass(); |
| 101 | + $this->resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class) |
| 102 | + ->disableOriginalConstructor() |
| 103 | + ->getMock(); |
| 104 | + $this->group = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class) |
| 105 | + ->getMockForAbstractClass(); |
| 106 | + $this->session = $this->getMockBuilder(\Magento\Backend\Model\Session::class) |
| 107 | + ->disableOriginalConstructor() |
| 108 | + ->setMethods(['setCustomerGroupData']) |
| 109 | + ->getMock(); |
| 110 | + |
| 111 | + $this->contextMock->expects($this->once()) |
| 112 | + ->method('getMessageManager') |
| 113 | + ->willReturn($this->messageManager); |
| 114 | + $this->contextMock->expects($this->once()) |
| 115 | + ->method('getRequest') |
| 116 | + ->willReturn($this->request); |
| 117 | + $this->contextMock->expects($this->once()) |
| 118 | + ->method('getResultRedirectFactory') |
| 119 | + ->willReturn($this->resultRedirectFactory); |
| 120 | + $this->contextMock->expects($this->once()) |
| 121 | + ->method('getSession') |
| 122 | + ->willReturn($this->session); |
| 123 | + |
| 124 | + $this->controller = new Save( |
| 125 | + $this->contextMock, |
| 126 | + $this->registryMock, |
| 127 | + $this->groupRepositoryMock, |
| 128 | + $this->groupInterfaceFactoryMock, |
| 129 | + $this->forwardFactoryMock, |
| 130 | + $this->pageFactoryMock, |
| 131 | + $this->dataObjectProcessorMock |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + public function testExecuteWithTaxClassAndException() |
| 136 | + { |
| 137 | + $taxClass = '3'; |
| 138 | + $groupId = 0; |
| 139 | + $code = 'NOT LOGGED IN'; |
| 140 | + |
| 141 | + $this->request->expects($this->exactly(3)) |
| 142 | + ->method('getParam') |
| 143 | + ->withConsecutive( |
| 144 | + ['tax_class'], |
| 145 | + ['id'], |
| 146 | + ['code'] |
| 147 | + ) |
| 148 | + ->willReturnOnConsecutiveCalls($taxClass, $groupId, null); |
| 149 | + $this->resultRedirectFactory->expects($this->once()) |
| 150 | + ->method('create') |
| 151 | + ->willReturn($this->resultRedirect); |
| 152 | + $this->groupRepositoryMock->expects($this->once()) |
| 153 | + ->method('getById') |
| 154 | + ->with($groupId) |
| 155 | + ->willReturn($this->group); |
| 156 | + $this->group->expects($this->once()) |
| 157 | + ->method('getCode') |
| 158 | + ->willReturn($code); |
| 159 | + $this->group->expects($this->once()) |
| 160 | + ->method('setCode') |
| 161 | + ->with($code); |
| 162 | + $this->group->expects($this->once()) |
| 163 | + ->method('setTaxClassId') |
| 164 | + ->with($taxClass); |
| 165 | + $this->groupRepositoryMock->expects($this->once()) |
| 166 | + ->method('save') |
| 167 | + ->with($this->group); |
| 168 | + $this->messageManager->expects($this->once()) |
| 169 | + ->method('addSuccess') |
| 170 | + ->with(__('You saved the customer group.')); |
| 171 | + $exception = new \Exception('Exception'); |
| 172 | + $this->resultRedirect->expects($this->at(0)) |
| 173 | + ->method('setPath') |
| 174 | + ->with('customer/group') |
| 175 | + ->willThrowException($exception); |
| 176 | + $this->messageManager->expects($this->once()) |
| 177 | + ->method('addError') |
| 178 | + ->with('Exception'); |
| 179 | + $this->dataObjectProcessorMock->expects($this->once()) |
| 180 | + ->method('buildOutputDataArray') |
| 181 | + ->with($this->group, '\Magento\Customer\Api\Data\GroupInterface') |
| 182 | + ->willReturn(['code' => $code]); |
| 183 | + $this->session->expects($this->once()) |
| 184 | + ->method('setCustomerGroupData') |
| 185 | + ->with(['customer_group_code' => $code]); |
| 186 | + $this->resultRedirect->expects($this->at(1)) |
| 187 | + ->method('setPath') |
| 188 | + ->with('customer/group/edit', ['id' => $groupId]); |
| 189 | + $this->assertSame($this->resultRedirect, $this->controller->execute()); |
| 190 | + } |
| 191 | + |
| 192 | + public function testExecuteWithoutTaxClass() |
| 193 | + { |
| 194 | + $this->request->expects($this->once()) |
| 195 | + ->method('getParam') |
| 196 | + ->with('tax_class') |
| 197 | + ->willReturn(null); |
| 198 | + $this->forwardFactoryMock->expects($this->once()) |
| 199 | + ->method('create') |
| 200 | + ->willReturn($this->resultForward); |
| 201 | + $this->resultForward->expects($this->once()) |
| 202 | + ->method('forward') |
| 203 | + ->with('new') |
| 204 | + ->willReturnSelf(); |
| 205 | + $this->assertSame($this->resultForward, $this->controller->execute()); |
| 206 | + } |
| 207 | +} |
0 commit comments