80 $objectManagerHelper =
new ObjectManagerHelper($this);
82 $this->contextMock = $this->createMock(\
Magento\Backend\
App\Action\Context::class);
83 $resultRedirectFactory = $this->createMock(\
Magento\Backend\Model\View\Result\RedirectFactory::class);
84 $this->responseMock = $this->createMock(\
Magento\Framework\
App\ResponseInterface::class);
85 $this->requestMock = $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
86 ->disableOriginalConstructor()->getMock();
87 $this->objectManagerMock = $this->createPartialMock(
91 $this->messageManagerMock = $this->createMock(\
Magento\Framework\Message\Manager::class);
92 $this->customerCollectionMock =
94 ->disableOriginalConstructor()
96 $this->customerCollectionFactoryMock =
98 ->disableOriginalConstructor()
99 ->setMethods([
'create'])
101 $redirectMock = $this->getMockBuilder(\
Magento\Backend\Model\View\Result\Redirect::class)
102 ->disableOriginalConstructor()
105 $resultFactoryMock = $this->getMockBuilder(\
Magento\Framework\Controller\ResultFactory::class)
106 ->disableOriginalConstructor()
108 $resultFactoryMock->expects($this->any())
110 ->with(\
Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
111 ->willReturn($redirectMock);
112 $this->subscriberMock = $this->createMock(\
Magento\Newsletter\Model\Subscriber::class);
113 $subscriberFactoryMock = $this->getMockBuilder(\
Magento\Newsletter\Model\SubscriberFactory::class)
114 ->setMethods([
'create'])
115 ->disableOriginalConstructor()
117 $subscriberFactoryMock->expects($this->any())
119 ->willReturn($this->subscriberMock);
121 $this->resultRedirectMock = $this->createMock(\
Magento\Backend\Model\View\Result\Redirect::class);
122 $resultRedirectFactory->expects($this->any())->method(
'create')->willReturn($this->resultRedirectMock);
124 $this->contextMock->expects($this->once())->method(
'getMessageManager')->willReturn($this->messageManagerMock);
125 $this->contextMock->expects($this->once())->method(
'getRequest')->willReturn($this->requestMock);
126 $this->contextMock->expects($this->once())->method(
'getResponse')->willReturn($this->responseMock);
127 $this->contextMock->expects($this->once())->method(
'getObjectManager')->willReturn($this->objectManagerMock);
128 $this->contextMock->expects($this->any())
129 ->method(
'getResultRedirectFactory')
130 ->willReturn($resultRedirectFactory);
131 $this->contextMock->expects($this->any())
132 ->method(
'getResultFactory')
133 ->willReturn($resultFactoryMock);
135 $this->filterMock = $this->createMock(\
Magento\Ui\Component\MassAction\Filter::class);
136 $this->filterMock->expects($this->once())
137 ->method(
'getCollection')
138 ->with($this->customerCollectionMock)
139 ->willReturnArgument(0);
140 $this->customerCollectionFactoryMock->expects($this->once())
142 ->willReturn($this->customerCollectionMock);
143 $this->customerRepositoryMock = $this->getMockBuilder(\
Magento\Customer\Api\CustomerRepositoryInterface::class)
144 ->getMockForAbstractClass();
145 $this->massAction = $objectManagerHelper->getObject(
146 \
Magento\Customer\Controller\Adminhtml\Index\MassUnsubscribe::class,
148 'context' => $this->contextMock,
149 'filter' => $this->filterMock,
150 'collectionFactory' => $this->customerCollectionFactoryMock,
151 'customerRepository' => $this->customerRepositoryMock,
152 'subscriberFactory' => $subscriberFactoryMock,
159 $customersIds = [10, 11, 12];
161 $this->customerCollectionMock->expects($this->any())
162 ->method(
'getAllIds')
163 ->willReturn($customersIds);
165 $this->customerRepositoryMock->expects($this->any())
167 ->willReturnMap([[10,
true], [11,
true], [12,
true]]);
169 $this->subscriberMock->expects($this->any())
170 ->method(
'unsubscribeCustomerById')
171 ->willReturnMap([[10,
true], [11,
true], [12,
true]]);
173 $this->messageManagerMock->expects($this->once())
174 ->method(
'addSuccess')
175 ->with(
__(
'A total of %1 record(s) were updated.', count($customersIds)));
177 $this->resultRedirectMock->expects($this->any())
179 ->with(
'customer/*/index')
182 $this->massAction->execute();
187 $customersIds = [10, 11, 12];
189 $this->customerCollectionMock->expects($this->any())
190 ->method(
'getAllIds')
191 ->willReturn($customersIds);
193 $this->customerRepositoryMock->expects($this->any())
195 ->willThrowException(
new \Exception(
'Some message.'));
197 $this->messageManagerMock->expects($this->once())
199 ->with(
'Some message.');
201 $this->massAction->execute();
$customerCollectionFactoryMock
testExecuteWithException()