Inject CronTrigger into Initializer
[MAILPOET-1823]
This commit is contained in:
committed by
M. Shull
parent
096359741d
commit
6048ccba85
@ -52,6 +52,9 @@ class Initializer {
|
|||||||
/** @var Menu */
|
/** @var Menu */
|
||||||
private $menu;
|
private $menu;
|
||||||
|
|
||||||
|
/** @var CronTrigger */
|
||||||
|
private $cron_trigger;
|
||||||
|
|
||||||
const INITIALIZED = 'MAILPOET_INITIALIZED';
|
const INITIALIZED = 'MAILPOET_INITIALIZED';
|
||||||
|
|
||||||
function __construct(
|
function __construct(
|
||||||
@ -64,7 +67,8 @@ class Initializer {
|
|||||||
Router\Router $router,
|
Router\Router $router,
|
||||||
Hooks $hooks,
|
Hooks $hooks,
|
||||||
Changelog $changelog,
|
Changelog $changelog,
|
||||||
Menu $menu
|
Menu $menu,
|
||||||
|
CronTrigger $cron_trigger
|
||||||
) {
|
) {
|
||||||
$this->container = $container;
|
$this->container = $container;
|
||||||
$this->renderer_factory = $renderer_factory;
|
$this->renderer_factory = $renderer_factory;
|
||||||
@ -76,6 +80,7 @@ class Initializer {
|
|||||||
$this->hooks = $hooks;
|
$this->hooks = $hooks;
|
||||||
$this->changelog = $changelog;
|
$this->changelog = $changelog;
|
||||||
$this->menu = $menu;
|
$this->menu = $menu;
|
||||||
|
$this->cron_trigger = $cron_trigger;
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
@ -253,8 +258,7 @@ class Initializer {
|
|||||||
function setupCronTrigger() {
|
function setupCronTrigger() {
|
||||||
// setup cron trigger only outside of cli environment
|
// setup cron trigger only outside of cli environment
|
||||||
if (php_sapi_name() !== 'cli') {
|
if (php_sapi_name() !== 'cli') {
|
||||||
$cron_trigger = $this->container->get(CronTrigger::class);
|
$this->cron_trigger->init();
|
||||||
$cron_trigger->init();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,9 @@ use MailPoet\Settings\SettingsController;
|
|||||||
if (!defined('ABSPATH')) exit;
|
if (!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class CronTrigger {
|
class CronTrigger {
|
||||||
public $current_method;
|
/** @var SettingsController */
|
||||||
|
private $settings;
|
||||||
|
|
||||||
public static $available_methods = array(
|
public static $available_methods = array(
|
||||||
'mailpoet' => 'MailPoet',
|
'mailpoet' => 'MailPoet',
|
||||||
'wordpress' => 'WordPress',
|
'wordpress' => 'WordPress',
|
||||||
@ -16,13 +18,14 @@ class CronTrigger {
|
|||||||
const DEFAULT_METHOD = 'WordPress';
|
const DEFAULT_METHOD = 'WordPress';
|
||||||
const SETTING_NAME = 'cron_trigger';
|
const SETTING_NAME = 'cron_trigger';
|
||||||
|
|
||||||
function __construct(SettingsController $settingsController) {
|
function __construct(SettingsController $settings) {
|
||||||
$this->current_method = $settingsController->get(self::SETTING_NAME . '.method');
|
$this->settings = $settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
|
$current_method = $this->settings->get(self::SETTING_NAME . '.method');
|
||||||
try {
|
try {
|
||||||
$trigger_class = __NAMESPACE__ . '\Triggers\\' . $this->current_method;
|
$trigger_class = __NAMESPACE__ . '\Triggers\\' . $current_method;
|
||||||
return (class_exists($trigger_class)) ?
|
return (class_exists($trigger_class)) ?
|
||||||
$trigger_class::run() :
|
$trigger_class::run() :
|
||||||
false;
|
false;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Test\Cron;
|
namespace MailPoet\Test\Cron;
|
||||||
|
|
||||||
|
use Codeception\Stub;
|
||||||
use MailPoet\Cron\CronTrigger;
|
use MailPoet\Cron\CronTrigger;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
@ -32,31 +33,35 @@ class CronTriggerTest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanConstruct() {
|
|
||||||
expect($this->cron_trigger->current_method)
|
|
||||||
->equals(CronTrigger::DEFAULT_METHOD);
|
|
||||||
}
|
|
||||||
|
|
||||||
function testItCanReturnAvailableMethods() {
|
function testItCanReturnAvailableMethods() {
|
||||||
expect($this->cron_trigger->getAvailableMethods())
|
expect($this->cron_trigger->getAvailableMethods())
|
||||||
->equals(CronTrigger::$available_methods);
|
->equals(CronTrigger::$available_methods);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanInitializeCronTriggerMethod() {
|
function testItCanInitializeCronTriggerMethod() {
|
||||||
$cron_trigger = $this->cron_trigger;
|
$settings_mock = Stub::makeEmpty(
|
||||||
$cron_trigger->current_method = 'CronTriggerMockMethod';
|
SettingsController::class,
|
||||||
|
['get' => 'CronTriggerMockMethod']
|
||||||
|
);
|
||||||
|
$cron_trigger = new CronTrigger($settings_mock);
|
||||||
expect($cron_trigger->init())->true();
|
expect($cron_trigger->init())->true();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItReturnsFalseWhenItCantInitializeCronTriggerMethod() {
|
function testItReturnsFalseWhenItCantInitializeCronTriggerMethod() {
|
||||||
$cron_trigger = $this->cron_trigger;
|
$settings_mock = Stub::makeEmpty(
|
||||||
$cron_trigger->current_method = 'MockInvalidMethod';
|
SettingsController::class,
|
||||||
|
['get' => 'MockInvalidMethod']
|
||||||
|
);
|
||||||
|
$cron_trigger = new CronTrigger($settings_mock);
|
||||||
expect($cron_trigger->init())->false();
|
expect($cron_trigger->init())->false();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItIgnoresExceptionsThrownFromCronTriggerMethods() {
|
function testItIgnoresExceptionsThrownFromCronTriggerMethods() {
|
||||||
$cron_trigger = $this->cron_trigger;
|
$settings_mock = Stub::makeEmpty(
|
||||||
$cron_trigger->current_method = 'CronTriggerMockMethodWithException';
|
SettingsController::class,
|
||||||
|
['get' => 'CronTriggerMockMethodWithException']
|
||||||
|
);
|
||||||
|
$cron_trigger = new CronTrigger($settings_mock);
|
||||||
expect($cron_trigger->init())->null();
|
expect($cron_trigger->init())->null();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user