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