MassSubscribeTest.php 7.8 KB

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