Skip to content

Commit e42b8eb

Browse files
author
Olga Kopylova
committed
MAGETWO-39778: Reindex by cron cannot finish: "Area code not set"
- fixed unit tests
1 parent 5f1f9fd commit e42b8eb

File tree

3 files changed

+69
-31
lines changed

3 files changed

+69
-31
lines changed

app/code/Magento/Cron/Model/Observer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ public function dispatch($observer)
182182
}
183183
} catch (\Exception $e) {
184184
$schedule->setMessages($e->getMessage());
185-
$schedule->setStatus(Schedule::STATUS_ERROR);
186185
}
187186
$schedule->save();
188187
}
@@ -230,7 +229,12 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
230229

231230
$schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))->save();
232231

233-
call_user_func_array($callback, [$schedule]);
232+
try {
233+
call_user_func_array($callback, [$schedule]);
234+
} catch (\Exception $e) {
235+
$schedule->setStatus(Schedule::STATUS_ERROR);
236+
throw $e;
237+
}
234238

235239
$schedule->setStatus(Schedule::STATUS_SUCCESS)->setFinishedAt(strftime(
236240
'%Y-%m-%d %H:%M:%S',
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Class CronJobException used to check that cron handles execution exception
9+
* Please see \Magento\Cron\Test\Unit\Model\ObserverTest
10+
*/
11+
namespace Magento\Cron\Test\Unit\Model;
12+
13+
class CronJobException
14+
{
15+
public function execute()
16+
{
17+
throw new \Exception('Test exception');
18+
}
19+
}

app/code/Magento/Cron/Test/Unit/Model/ObserverTest.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -293,37 +293,36 @@ public function testDispatchExceptionNoCallback()
293293
}
294294

295295
/**
296-
* Test case catch exception if callback exists but can't be executed
296+
* Test case catch exception if callback is not callable or throws exception
297+
*
298+
* @param string $cronJobType
299+
* @param mixed $cronJobObject
300+
* @param string $exceptionMessage
301+
* @param int $saveCalls
302+
*
303+
* @dataProvider dispatchExceptionInCallbackDataProvider
297304
*/
298-
public function testDispatchExceptionNotExecutable()
305+
public function testDispatchExceptionInCallback($cronJobType, $cronJobObject, $exceptionMessage, $saveCalls)
299306
{
300307
$jobConfig = [
301308
'test_group' => [
302-
'test_job1' => ['instance' => 'Not_Existed_Class', 'method' => 'notExistedMethod'],
309+
'test_job1' => ['instance' => $cronJobType, 'method' => 'execute'],
303310
],
304311
];
305312

306-
$exceptionMessage = 'Invalid callback: Not_Existed_Class::notExistedMethod can\'t be called';
307313
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
308-
$schedule = $this->getMockBuilder(
309-
'Magento\Cron\Model\Schedule'
310-
)->setMethods(
311-
['getJobCode', 'tryLockJob', 'getScheduledAt', 'save', 'setStatus', 'setMessages', '__wakeup']
312-
)->disableOriginalConstructor()->getMock();
314+
$schedule = $this->getMockBuilder('Magento\Cron\Model\Schedule')
315+
->setMethods(['getJobCode', 'tryLockJob', 'getScheduledAt', 'save', 'setStatus', 'setMessages', '__wakeup'])
316+
->disableOriginalConstructor()->getMock();
313317
$schedule->expects($this->any())->method('getJobCode')->will($this->returnValue('test_job1'));
314318
$schedule->expects($this->once())->method('getScheduledAt')->will($this->returnValue('-1 day'));
315319
$schedule->expects($this->once())->method('tryLockJob')->will($this->returnValue(true));
316-
$schedule->expects(
317-
$this->once()
318-
)->method(
319-
'setStatus'
320-
)->with(
321-
$this->equalTo(\Magento\Cron\Model\Schedule::STATUS_ERROR)
322-
)->will(
323-
$this->returnSelf()
324-
);
320+
$schedule->expects($this->once())
321+
->method('setStatus')
322+
->with($this->equalTo(\Magento\Cron\Model\Schedule::STATUS_ERROR))
323+
->will($this->returnSelf());
325324
$schedule->expects($this->once())->method('setMessages')->with($this->equalTo($exceptionMessage));
326-
$schedule->expects($this->once())->method('save');
325+
$schedule->expects($this->exactly($saveCalls))->method('save');
327326

328327
$this->_collection->addItem($schedule);
329328

@@ -336,25 +335,41 @@ public function testDispatchExceptionNotExecutable()
336335
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
337336
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
338337
$this->_scheduleFactory->expects($this->once())->method('create')->will($this->returnValue($scheduleMock));
339-
$this->_objectManager->expects(
340-
$this->once()
341-
)->method(
342-
'create'
343-
)->with(
344-
$this->equalTo('Not_Existed_Class')
345-
)->will(
346-
$this->returnValue('')
347-
);
338+
$this->_objectManager
339+
->expects($this->once())
340+
->method('create')
341+
->with($this->equalTo($cronJobType))
342+
->will($this->returnValue($cronJobObject));
348343

349344
$this->_observer->dispatch('');
350345
}
351346

347+
/**
348+
* @return array
349+
*/
350+
public function dispatchExceptionInCallbackDataProvider()
351+
{
352+
return [
353+
'non-callable callback' => [
354+
'Not_Existed_Class',
355+
'',
356+
'Invalid callback: Not_Existed_Class::execute can\'t be called',
357+
1
358+
],
359+
'exception in execution' => [
360+
'CronJobException',
361+
new \Magento\Cron\Test\Unit\Model\CronJobException(),
362+
'Test exception',
363+
2
364+
],
365+
];
366+
}
367+
352368
/**
353369
* Test case, successfully run job
354370
*/
355371
public function testDispatchRunJob()
356372
{
357-
require_once __DIR__ . '/CronJob.php';
358373
$testCronJob = new \Magento\Cron\Test\Unit\Model\CronJob();
359374

360375
$jobConfig = [

0 commit comments

Comments
 (0)