SaveTest.php 8.5 KB

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