Skip to content

Commit

Permalink
Merge pull request #446 from magento-ogre/PR_Branch
Browse files Browse the repository at this point in the history
[Ogres] Bug Fixes
  • Loading branch information
Kopylova,Olga(okopylova) committed Jul 11, 2015
2 parents e239e1d + 42c438b commit 8fa3fab
Show file tree
Hide file tree
Showing 18 changed files with 441 additions and 385 deletions.
18 changes: 10 additions & 8 deletions app/code/Magento/Cron/Console/Command/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class CronCommand extends Command
const INPUT_KEY_GROUP = 'group';

/**
* Object Manager
* Object manager factory
*
* @var ObjectManagerInterface
* @var ObjectManagerFactory
*/
protected $objectManager;
private $objectManagerFactory;

/**
* Constructor
Expand All @@ -42,10 +42,7 @@ class CronCommand extends Command
*/
public function __construct(ObjectManagerFactory $objectManagerFactory)
{
$params = $_SERVER;
$params[StoreManager::PARAM_RUN_CODE] = 'admin';
$params[Store::CUSTOM_ENTRY_POINT_PARAM] = true;
$this->objectManager = $objectManagerFactory->create($params);
$this->objectManagerFactory = $objectManagerFactory;
parent::__construct();
}

Expand Down Expand Up @@ -80,6 +77,11 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$omParams = $_SERVER;
$omParams[StoreManager::PARAM_RUN_CODE] = 'admin';
$omParams[Store::CUSTOM_ENTRY_POINT_PARAM] = true;
$objectManager = $this->objectManagerFactory->create($omParams);

$params[self::INPUT_KEY_GROUP] = $input->getOption(self::INPUT_KEY_GROUP);
$params[Observer::STANDALONE_PROCESS_STARTED] = '0';
$bootstrap = $input->getOption(Cli::INPUT_KEY_BOOTSTRAP);
Expand All @@ -94,7 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
/** @var \Magento\Framework\App\Cron $cronObserver */
$cronObserver = $this->objectManager->create('Magento\Framework\App\Cron', ['parameters' => $params]);
$cronObserver = $objectManager->create('Magento\Framework\App\Cron', ['parameters' => $params]);
$cronObserver->launch();
$output->writeln('<info>' . 'Ran jobs by schedule.' . '</info>');
}
Expand Down
7 changes: 6 additions & 1 deletion app/code/Magento/Cron/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,

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

call_user_func_array($callback, [$schedule]);
try {
call_user_func_array($callback, [$schedule]);
} catch (\Exception $e) {
$schedule->setStatus(Schedule::STATUS_ERROR);
throw $e;
}

$schedule->setStatus(Schedule::STATUS_SUCCESS)->setFinishedAt(strftime(
'%Y-%m-%d %H:%M:%S',
Expand Down
19 changes: 19 additions & 0 deletions app/code/Magento/Cron/Test/Unit/Model/CronJobException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Class CronJobException used to check that cron handles execution exception
* Please see \Magento\Cron\Test\Unit\Model\ObserverTest
*/
namespace Magento\Cron\Test\Unit\Model;

class CronJobException
{
public function execute()
{
throw new \Exception('Test exception');
}
}
73 changes: 44 additions & 29 deletions app/code/Magento/Cron/Test/Unit/Model/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,37 +293,36 @@ public function testDispatchExceptionNoCallback()
}

/**
* Test case catch exception if callback exists but can't be executed
* Test case catch exception if callback is not callable or throws exception
*
* @param string $cronJobType
* @param mixed $cronJobObject
* @param string $exceptionMessage
* @param int $saveCalls
*
* @dataProvider dispatchExceptionInCallbackDataProvider
*/
public function testDispatchExceptionNotExecutable()
public function testDispatchExceptionInCallback($cronJobType, $cronJobObject, $exceptionMessage, $saveCalls)
{
$jobConfig = [
'test_group' => [
'test_job1' => ['instance' => 'Not_Existed_Class', 'method' => 'notExistedMethod'],
'test_job1' => ['instance' => $cronJobType, 'method' => 'execute'],
],
];

$exceptionMessage = 'Invalid callback: Not_Existed_Class::notExistedMethod can\'t be called';
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
$schedule = $this->getMockBuilder(
'Magento\Cron\Model\Schedule'
)->setMethods(
['getJobCode', 'tryLockJob', 'getScheduledAt', 'save', 'setStatus', 'setMessages', '__wakeup']
)->disableOriginalConstructor()->getMock();
$schedule = $this->getMockBuilder('Magento\Cron\Model\Schedule')
->setMethods(['getJobCode', 'tryLockJob', 'getScheduledAt', 'save', 'setStatus', 'setMessages', '__wakeup'])
->disableOriginalConstructor()->getMock();
$schedule->expects($this->any())->method('getJobCode')->will($this->returnValue('test_job1'));
$schedule->expects($this->once())->method('getScheduledAt')->will($this->returnValue('-1 day'));
$schedule->expects($this->once())->method('tryLockJob')->will($this->returnValue(true));
$schedule->expects(
$this->once()
)->method(
'setStatus'
)->with(
$this->equalTo(\Magento\Cron\Model\Schedule::STATUS_ERROR)
)->will(
$this->returnSelf()
);
$schedule->expects($this->once())
->method('setStatus')
->with($this->equalTo(\Magento\Cron\Model\Schedule::STATUS_ERROR))
->will($this->returnSelf());
$schedule->expects($this->once())->method('setMessages')->with($this->equalTo($exceptionMessage));
$schedule->expects($this->once())->method('save');
$schedule->expects($this->exactly($saveCalls))->method('save');

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

Expand All @@ -336,25 +335,41 @@ public function testDispatchExceptionNotExecutable()
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
$this->_scheduleFactory->expects($this->once())->method('create')->will($this->returnValue($scheduleMock));
$this->_objectManager->expects(
$this->once()
)->method(
'create'
)->with(
$this->equalTo('Not_Existed_Class')
)->will(
$this->returnValue('')
);
$this->_objectManager
->expects($this->once())
->method('create')
->with($this->equalTo($cronJobType))
->will($this->returnValue($cronJobObject));

$this->_observer->dispatch('');
}

/**
* @return array
*/
public function dispatchExceptionInCallbackDataProvider()
{
return [
'non-callable callback' => [
'Not_Existed_Class',
'',
'Invalid callback: Not_Existed_Class::execute can\'t be called',
1
],
'exception in execution' => [
'CronJobException',
new \Magento\Cron\Test\Unit\Model\CronJobException(),
'Test exception',
2
],
];
}

/**
* Test case, successfully run job
*/
public function testDispatchRunJob()
{
require_once __DIR__ . '/CronJob.php';
$testCronJob = new \Magento\Cron\Test\Unit\Model\CronJob();

$jobConfig = [
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,11 @@
"components/jquery": [
"lib/web/jquery.js",
"lib/web/jquery/jquery.min.js",
"lib/web/jquery/jquery-migrate.js",
"lib/web/jquery/jquery-migrate.min.js"
"lib/web/jquery/jquery-migrate.js"
],
"blueimp/jquery-file-upload": "lib/web/jquery/fileUploader",
"components/jqueryui": [
"lib/web/jquery/jquery-ui.js",
"lib/web/jquery/jquery-ui.min.js"
"lib/web/jquery/jquery-ui.js"
],
"twbs/bootstrap": [
"lib/web/jquery/jquery.tabs.js"
Expand Down
Loading

0 comments on commit 8fa3fab

Please sign in to comment.