Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassUnsubscribeTest.php
Go to the documentation of this file.
1 <?php
8 
11 
16 class MassUnsubscribeTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $massAction;
22 
26  protected $contextMock;
27 
32 
36  protected $requestMock;
37 
41  protected $responseMock;
42 
47 
51  protected $objectManagerMock;
52 
57 
62 
66  protected $filterMock;
67 
72 
76  protected $subscriberMock;
77 
78  protected function setUp()
79  {
80  $objectManagerHelper = new ObjectManagerHelper($this);
81 
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(
88  \Magento\Framework\ObjectManager\ObjectManager::class,
89  ['create']
90  );
91  $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
92  $this->customerCollectionMock =
93  $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
94  ->disableOriginalConstructor()
95  ->getMock();
96  $this->customerCollectionFactoryMock =
97  $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class)
98  ->disableOriginalConstructor()
99  ->setMethods(['create'])
100  ->getMock();
101  $redirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
102  ->disableOriginalConstructor()
103  ->getMock();
104 
105  $resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  $resultFactoryMock->expects($this->any())
109  ->method('create')
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()
116  ->getMock();
117  $subscriberFactoryMock->expects($this->any())
118  ->method('create')
119  ->willReturn($this->subscriberMock);
120 
121  $this->resultRedirectMock = $this->createMock(\Magento\Backend\Model\View\Result\Redirect::class);
122  $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirectMock);
123 
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);
134 
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())
141  ->method('create')
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,
147  [
148  'context' => $this->contextMock,
149  'filter' => $this->filterMock,
150  'collectionFactory' => $this->customerCollectionFactoryMock,
151  'customerRepository' => $this->customerRepositoryMock,
152  'subscriberFactory' => $subscriberFactoryMock,
153  ]
154  );
155  }
156 
157  public function testExecute()
158  {
159  $customersIds = [10, 11, 12];
160 
161  $this->customerCollectionMock->expects($this->any())
162  ->method('getAllIds')
163  ->willReturn($customersIds);
164 
165  $this->customerRepositoryMock->expects($this->any())
166  ->method('getById')
167  ->willReturnMap([[10, true], [11, true], [12, true]]);
168 
169  $this->subscriberMock->expects($this->any())
170  ->method('unsubscribeCustomerById')
171  ->willReturnMap([[10, true], [11, true], [12, true]]);
172 
173  $this->messageManagerMock->expects($this->once())
174  ->method('addSuccess')
175  ->with(__('A total of %1 record(s) were updated.', count($customersIds)));
176 
177  $this->resultRedirectMock->expects($this->any())
178  ->method('setPath')
179  ->with('customer/*/index')
180  ->willReturnSelf();
181 
182  $this->massAction->execute();
183  }
184 
185  public function testExecuteWithException()
186  {
187  $customersIds = [10, 11, 12];
188 
189  $this->customerCollectionMock->expects($this->any())
190  ->method('getAllIds')
191  ->willReturn($customersIds);
192 
193  $this->customerRepositoryMock->expects($this->any())
194  ->method('getById')
195  ->willThrowException(new \Exception('Some message.'));
196 
197  $this->messageManagerMock->expects($this->once())
198  ->method('addError')
199  ->with('Some message.');
200 
201  $this->massAction->execute();
202  }
203 }
__()
Definition: __.php:13