Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassOnTheFlyTest.php
Go to the documentation of this file.
1 <?php
8 
12 class MassOnTheFlyTest 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(
113  \Magento\Framework\App\ViewInterface::class,
114  [
115  'loadLayout',
116  'getPage',
117  'getConfig',
118  'getTitle',
119  'renderLayout',
120  'loadLayoutUpdates',
121  'getDefaultLayoutHandle',
122  'addPageLayoutHandles',
123  'generateLayoutBlocks',
124  'generateLayoutXml',
125  'getLayout',
126  'addActionLayoutHandles',
127  'setIsLayoutLoaded',
128  'isLayoutLoaded'
129  ]
130  );
131 
132  $this->session = $this->createPartialMock(\Magento\Backend\Model\Session::class, ['setIsUrlNotice']);
133  $this->session->expects($this->any())->method('setIsUrlNotice')->willReturn($this->objectManager);
134  $this->actionFlag = $this->createPartialMock(\Magento\Framework\App\ActionFlag::class, ['get']);
135  $this->actionFlag->expects($this->any())->method("get")->willReturn($this->objectManager);
136  $this->objectManager = $this->createPartialMock(
137  \Magento\Framework\TestFramework\Unit\Helper\ObjectManager::class,
138  ['get']
139  );
140  $this->request = $this->getMockForAbstractClass(
141  \Magento\Framework\App\RequestInterface::class,
142  ['getParam', 'getRequest'],
143  '',
144  false
145  );
146 
147  $this->response->expects($this->any())->method("setRedirect")->willReturn(1);
148  $this->page = $this->createMock(\Magento\Framework\View\Result\Page::class);
149  $this->config = $this->createMock(\Magento\Framework\View\Result\Page::class);
150  $this->title = $this->createMock(\Magento\Framework\View\Page\Title::class);
151  $this->messageManager = $this->getMockForAbstractClass(
152  \Magento\Framework\Message\ManagerInterface::class,
153  ['addError', 'addSuccess'],
154  '',
155  false
156  );
157 
158  $this->indexReg = $this->createPartialMock(
159  \Magento\Framework\Indexer\IndexerRegistry::class,
160  ['get', 'setScheduled']
161  );
162  $this->helper = $this->createPartialMock(\Magento\Backend\Helper\Data::class, ['getUrl']);
163  $this->contextMock->expects($this->any())->method("getObjectManager")->willReturn($this->objectManager);
164  $this->contextMock->expects($this->any())->method("getRequest")->willReturn($this->request);
165  $this->contextMock->expects($this->any())->method("getResponse")->willReturn($this->response);
166  $this->contextMock->expects($this->any())->method("getMessageManager")->willReturn($this->messageManager);
167  $this->contextMock->expects($this->any())->method("getSession")->willReturn($this->session);
168  $this->contextMock->expects($this->any())->method("getActionFlag")->willReturn($this->actionFlag);
169  $this->contextMock->expects($this->any())->method("getHelper")->willReturn($this->helper);
170  }
171 
178  public function testExecute($indexerIds, $exception, $expectsExceptionValues)
179  {
180  $this->model = new \Magento\Indexer\Controller\Adminhtml\Indexer\MassOnTheFly($this->contextMock);
181  $this->request->expects($this->any())
182  ->method('getParam')->with('indexer_ids')
183  ->will($this->returnValue($indexerIds));
184 
185  if (!is_array($indexerIds)) {
186  $this->messageManager->expects($this->once())
187  ->method('addError')->with(__('Please select indexers.'))
188  ->will($this->returnValue(1));
189  } else {
190  $this->objectManager->expects($this->any())
191  ->method('get')->with(\Magento\Framework\Indexer\IndexerRegistry::class)
192  ->will($this->returnValue($this->indexReg));
193  $indexerInterface = $this->getMockForAbstractClass(
194  \Magento\Framework\Indexer\IndexerInterface::class,
195  ['setScheduled'],
196  '',
197  false
198  );
199  $this->indexReg->expects($this->any())
200  ->method('get')->with(1)
201  ->will($this->returnValue($indexerInterface));
202 
203  if ($exception !== null) {
204  $indexerInterface->expects($this->any())
205  ->method('setScheduled')->with(false)->will($this->throwException($exception));
206  } else {
207  $indexerInterface->expects($this->any())
208  ->method('setScheduled')->with(false)->will($this->returnValue(1));
209  }
210 
211  $this->messageManager->expects($this->any())->method('addSuccess')->will($this->returnValue(1));
212 
213  if ($exception !== null) {
214  $this->messageManager->expects($this->exactly($expectsExceptionValues[2]))
215  ->method('addError')
216  ->with($exception->getMessage());
217  $this->messageManager->expects($this->exactly($expectsExceptionValues[1]))
218  ->method('addException')
219  ->with($exception, "We couldn't change indexer(s)' mode because of an error.");
220  }
221  }
222 
223  $this->helper->expects($this->any())->method("getUrl")->willReturn("magento.com");
224  $this->response->expects($this->any())->method("setRedirect")->willReturn(1);
225 
226  $result = $this->model->execute();
227  $this->assertNull($result);
228  }
229 
233  public function executeDataProvider()
234  {
235  return [
236  'set1' => [
237  'idexers' => 1,
238  "exception" => null,
239  "expectsValues" => [0, 0, 0]
240  ],
241  'set2' => [
242  'idexers' => [1],
243  "exception" => null,
244  "expectsException" => [1, 0, 0]
245  ],
246  'set3' => [
247  'idexers' => [1],
248  "exception" => new \Magento\Framework\Exception\LocalizedException(__('Test Phrase')),
249  "expectsException" => [0, 0, 1]
250  ],
251  'set4' => [
252  'idexers' => [1],
253  "exception" => new \Exception(),
254  "expectsException" => [0, 1, 0]
255  ]
256  ];
257  }
258 }
__()
Definition: __.php:13
testExecute($indexerIds, $exception, $expectsExceptionValues)