Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassChangelogTest.php
Go to the documentation of this file.
1 <?php
8 
12 class MassChangelogTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
22  protected $contextMock;
23 
27  protected $view;
28 
32  protected $page;
33 
37  protected $config;
38 
42  protected $title;
43 
47  protected $request;
48 
52  protected $objectManager;
53 
57  protected $messageManager;
58 
62  protected $indexReg;
63 
67  protected $response;
68 
72  protected $actionFlag;
73 
77  protected $helper;
78 
82  protected $session;
83 
88  protected function setUp()
89  {
90  $this->contextMock = $this->createPartialMock(\Magento\Backend\App\Action\Context::class, [
91  'getAuthorization',
92  'getSession',
93  'getActionFlag',
94  'getAuth',
95  'getView',
96  'getHelper',
97  'getBackendUrl',
98  'getFormKeyValidator',
99  'getLocaleResolver',
100  'getCanUseBaseUrl',
101  'getRequest',
102  'getResponse',
103  'getObjectManager',
104  'getMessageManager'
105  ]);
106 
107  $this->response = $this->createPartialMock(
108  \Magento\Framework\App\ResponseInterface::class,
109  ['setRedirect', 'sendResponse']
110  );
111 
112  $this->view = $this->createPartialMock(\Magento\Framework\App\ViewInterface::class, [
113  'loadLayout',
114  'getPage',
115  'getConfig',
116  'getTitle',
117  'renderLayout',
118  'loadLayoutUpdates',
119  'getDefaultLayoutHandle',
120  'addPageLayoutHandles',
121  'generateLayoutBlocks',
122  'generateLayoutXml',
123  'getLayout',
124  'addActionLayoutHandles',
125  'setIsLayoutLoaded',
126  'isLayoutLoaded'
127  ]);
128 
129  $this->session = $this->createPartialMock(\Magento\Backend\Model\Session::class, ['setIsUrlNotice']);
130  $this->session->expects($this->any())->method('setIsUrlNotice')->willReturn($this->objectManager);
131  $this->actionFlag = $this->createPartialMock(\Magento\Framework\App\ActionFlag::class, ['get']);
132  $this->actionFlag->expects($this->any())->method("get")->willReturn($this->objectManager);
133  $this->objectManager = $this->createPartialMock(
134  \Magento\Framework\TestFramework\Unit\Helper\ObjectManager::class,
135  ['get']
136  );
137  $this->request = $this->getMockForAbstractClass(
138  \Magento\Framework\App\RequestInterface::class,
139  ['getParam', 'getRequest'],
140  '',
141  false
142  );
143 
144  $this->response->expects($this->any())->method("setRedirect")->willReturn(1);
145  $this->page = $this->createMock(\Magento\Framework\View\Result\Page::class);
146  $this->config = $this->createMock(\Magento\Framework\View\Result\Page::class);
147  $this->title = $this->createMock(\Magento\Framework\View\Page\Title::class);
148  $this->messageManager = $this->getMockForAbstractClass(
149  \Magento\Framework\Message\ManagerInterface::class,
150  ['addError', 'addSuccess'],
151  '',
152  false
153  );
154 
155  $this->indexReg = $this->createPartialMock(
156  \Magento\Framework\Indexer\IndexerRegistry::class,
157  ['get', 'setScheduled']
158  );
159  $this->helper = $this->createPartialMock(\Magento\Backend\Helper\Data::class, ['getUrl']);
160  $this->contextMock->expects($this->any())->method("getObjectManager")->willReturn($this->objectManager);
161  $this->contextMock->expects($this->any())->method("getRequest")->willReturn($this->request);
162  $this->contextMock->expects($this->any())->method("getResponse")->willReturn($this->response);
163  $this->contextMock->expects($this->any())->method("getMessageManager")->willReturn($this->messageManager);
164  $this->contextMock->expects($this->any())->method("getSession")->willReturn($this->session);
165  $this->contextMock->expects($this->any())->method("getActionFlag")->willReturn($this->actionFlag);
166  $this->contextMock->expects($this->any())->method("getHelper")->willReturn($this->helper);
167  }
168 
175  public function testExecute($indexerIds, $exception, $expectsExceptionValues)
176  {
177  $this->model = new \Magento\Indexer\Controller\Adminhtml\Indexer\MassChangelog($this->contextMock);
178  $this->request->expects($this->any())
179  ->method('getParam')->with('indexer_ids')
180  ->will($this->returnValue($indexerIds));
181 
182  if (!is_array($indexerIds)) {
183  $this->messageManager->expects($this->once())
184  ->method('addError')->with(__('Please select indexers.'))
185  ->will($this->returnValue(1));
186  } else {
187  $this->objectManager->expects($this->any())
188  ->method('get')->with(\Magento\Framework\Indexer\IndexerRegistry::class)
189  ->will($this->returnValue($this->indexReg));
190  $indexerInterface = $this->getMockForAbstractClass(
191  \Magento\Framework\Indexer\IndexerInterface::class,
192  ['setScheduled'],
193  '',
194  false
195  );
196  $this->indexReg->expects($this->any())
197  ->method('get')->with(1)
198  ->will($this->returnValue($indexerInterface));
199 
200  if ($exception !== null) {
201  $indexerInterface->expects($this->any())
202  ->method('setScheduled')->with(true)->will($this->throwException($exception));
203  } else {
204  $indexerInterface->expects($this->any())
205  ->method('setScheduled')->with(true)->will($this->returnValue(1));
206  }
207 
208  $this->messageManager->expects($this->any())->method('addSuccess')->will($this->returnValue(1));
209 
210  if ($exception !== null) {
211  $this->messageManager
212  ->expects($this->exactly($expectsExceptionValues[2]))
213  ->method('addError')
214  ->with($exception->getMessage());
215  $this->messageManager->expects($this->exactly($expectsExceptionValues[1]))
216  ->method('addException')
217  ->with($exception, "We couldn't change indexer(s)' mode because of an error.");
218  }
219  }
220 
221  $this->helper->expects($this->any())->method("getUrl")->willReturn("magento.com");
222  $this->response->expects($this->any())->method("setRedirect")->willReturn(1);
223 
224  $result = $this->model->execute();
225  $this->assertNull($result);
226  }
227 
231  public function executeDataProvider()
232  {
233  return [
234  'set1' => [
235  'idexers' => 1,
236  "exception" => null,
237  "expectsValues" => [0, 0, 0]
238  ],
239  'set2' => [
240  'idexers' => [1],
241  "exception" => null,
242  "expectsException" => [1, 0, 0]
243  ],
244  'set3' => [
245  'idexers' => [1],
246  "exception" => new \Magento\Framework\Exception\LocalizedException(__('Test Phrase')),
247  "expectsException" => [0, 0, 1]
248  ],
249  'set4' => [
250  'idexers' => [1],
251  "exception" => new \Exception(),
252  "expectsException" => [0, 1, 0]
253  ]
254  ];
255  }
256 }
__()
Definition: __.php:13
testExecute($indexerIds, $exception, $expectsExceptionValues)