MassAssignGroupTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Index;
  7. use Magento\Framework\App\Action\Context;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * Class MassAssignGroupTest
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class MassAssignGroupTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Customer\Controller\Adminhtml\Index\MassAssignGroup
  18. */
  19. protected $massAction;
  20. /**
  21. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $contextMock;
  24. /**
  25. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $resultRedirectMock;
  28. /**
  29. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $requestMock;
  32. /**
  33. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $responseMock;
  36. /**
  37. * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $messageManagerMock;
  40. /**
  41. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $objectManagerMock;
  44. /**
  45. * @var \Magento\Customer\Model\ResourceModel\Customer\Collection|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $customerCollectionMock;
  48. /**
  49. * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $customerCollectionFactoryMock;
  52. /**
  53. * @var \Magento\Ui\Component\MassAction\Filter|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $filterMock;
  56. /**
  57. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. protected $customerRepositoryMock;
  60. /**
  61. * @inheritdoc
  62. */
  63. protected function setUp()
  64. {
  65. $objectManagerHelper = new ObjectManagerHelper($this);
  66. $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
  67. $resultRedirectFactory = $this->createMock(
  68. \Magento\Backend\Model\View\Result\RedirectFactory::class
  69. );
  70. $this->responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  71. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  72. ->disableOriginalConstructor()->getMock();
  73. $this->objectManagerMock = $this->createPartialMock(
  74. \Magento\Framework\ObjectManager\ObjectManager::class,
  75. ['create']
  76. );
  77. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
  78. $this->customerCollectionMock =
  79. $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->customerCollectionFactoryMock =
  83. $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class)
  84. ->disableOriginalConstructor()
  85. ->setMethods(['create'])
  86. ->getMock();
  87. $redirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $resultFactoryMock->expects($this->any())
  94. ->method('create')
  95. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
  96. ->willReturn($redirectMock);
  97. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirectMock);
  101. $this->contextMock->expects($this->once())->method('getMessageManager')->willReturn($this->messageManagerMock);
  102. $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
  103. $this->contextMock->expects($this->once())->method('getResponse')->willReturn($this->responseMock);
  104. $this->contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock);
  105. $this->contextMock->expects($this->any())
  106. ->method('getResultRedirectFactory')
  107. ->willReturn($resultRedirectFactory);
  108. $this->contextMock->expects($this->any())
  109. ->method('getResultFactory')
  110. ->willReturn($resultFactoryMock);
  111. $this->filterMock = $this->createMock(\Magento\Ui\Component\MassAction\Filter::class);
  112. $this->filterMock->expects($this->once())
  113. ->method('getCollection')
  114. ->with($this->customerCollectionMock)
  115. ->willReturnArgument(0);
  116. $this->customerCollectionFactoryMock->expects($this->once())
  117. ->method('create')
  118. ->willReturn($this->customerCollectionMock);
  119. $this->customerRepositoryMock = $this
  120. ->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
  121. ->getMockForAbstractClass();
  122. $this->massAction = $objectManagerHelper->getObject(
  123. \Magento\Customer\Controller\Adminhtml\Index\MassAssignGroup::class,
  124. [
  125. 'context' => $this->contextMock,
  126. 'filter' => $this->filterMock,
  127. 'collectionFactory' => $this->customerCollectionFactoryMock,
  128. 'customerRepository' => $this->customerRepositoryMock,
  129. ]
  130. );
  131. }
  132. /**
  133. * Unit test to verify mass customer group assignment use case
  134. *
  135. * @throws \Magento\Framework\Exception\LocalizedException
  136. */
  137. public function testExecute()
  138. {
  139. $customersIds = [10, 11, 12];
  140. $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  141. ->setMethods(['setData'])
  142. ->disableOriginalConstructor()
  143. ->getMockForAbstractClass();
  144. $this->customerCollectionMock->expects($this->any())
  145. ->method('getAllIds')
  146. ->willReturn($customersIds);
  147. $this->customerRepositoryMock->expects($this->any())
  148. ->method('getById')
  149. ->willReturnMap([[10, $customerMock], [11, $customerMock], [12, $customerMock]]);
  150. $this->messageManagerMock->expects($this->once())
  151. ->method('addSuccess')
  152. ->with(__('A total of %1 record(s) were updated.', count($customersIds)));
  153. $this->resultRedirectMock->expects($this->any())
  154. ->method('setPath')
  155. ->with('customer/*/index')
  156. ->willReturnSelf();
  157. $this->massAction->execute();
  158. }
  159. /**
  160. * Unit test to verify expected error during mass customer group assignment use case
  161. *
  162. * @throws \Magento\Framework\Exception\LocalizedException
  163. */
  164. public function testExecuteWithException()
  165. {
  166. $customersIds = [10, 11, 12];
  167. $this->customerCollectionMock->expects($this->any())
  168. ->method('getAllIds')
  169. ->willReturn($customersIds);
  170. $this->customerRepositoryMock->expects($this->any())
  171. ->method('getById')
  172. ->willThrowException(new \Exception('Some message.'));
  173. $this->messageManagerMock->expects($this->once())
  174. ->method('addError')
  175. ->with('Some message.');
  176. $this->massAction->execute();
  177. }
  178. }