- Adds unit tests for Cron Trigger class
This commit is contained in:
@ -192,8 +192,11 @@ class Initializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setupCronTrigger() {
|
function setupCronTrigger() {
|
||||||
$cron_trigger = new CronTrigger();
|
// setup cron trigger only outside of cli environment
|
||||||
$cron_trigger->init();
|
if(php_sapi_name() !== 'cli') {
|
||||||
|
$cron_trigger = new CronTrigger();
|
||||||
|
$cron_trigger->init();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupImages() {
|
function setupImages() {
|
||||||
|
@ -20,8 +20,6 @@ class CronTrigger {
|
|||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
try {
|
try {
|
||||||
// configure cron trigger only outside of cli environment
|
|
||||||
if(php_sapi_name() === 'cli') return;
|
|
||||||
$trigger_class = __NAMESPACE__ . '\Triggers\\' . $this->current_method;
|
$trigger_class = __NAMESPACE__ . '\Triggers\\' . $this->current_method;
|
||||||
return (class_exists($trigger_class)) ?
|
return (class_exists($trigger_class)) ?
|
||||||
$trigger_class::run() :
|
$trigger_class::run() :
|
||||||
|
9
tests/unit/Cron/CronTriggerMockMethod.php
Normal file
9
tests/unit/Cron/CronTriggerMockMethod.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Cron\Triggers;
|
||||||
|
|
||||||
|
class MockMethod {
|
||||||
|
static function run() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
9
tests/unit/Cron/CronTriggerMockMethodWithException.php
Normal file
9
tests/unit/Cron/CronTriggerMockMethodWithException.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Cron\Triggers;
|
||||||
|
|
||||||
|
class MockMethodWithException {
|
||||||
|
static function run() {
|
||||||
|
throw new \Exception('Exception thrown');
|
||||||
|
}
|
||||||
|
}
|
61
tests/unit/Cron/CronTriggerTest.php
Normal file
61
tests/unit/Cron/CronTriggerTest.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MailPoet\Cron\CronTrigger;
|
||||||
|
use MailPoet\Models\Setting;
|
||||||
|
|
||||||
|
require_once('CronTriggerMockMethod.php');
|
||||||
|
require_once('CronTriggerMockMethodWithException.php');
|
||||||
|
|
||||||
|
class CronTriggerTest extends MailPoetTest {
|
||||||
|
function _before() {
|
||||||
|
$this->cron_trigger = new CronTrigger();
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanDefineConstants() {
|
||||||
|
expect(CronTrigger::DEFAULT_METHOD)->equals('WordPress');
|
||||||
|
expect(CronTrigger::SETTING_NAME)->equals('cron_trigger');
|
||||||
|
expect(CronTrigger::$available_methods)->equals(
|
||||||
|
array(
|
||||||
|
'mailpoet' => 'MailPoet',
|
||||||
|
'wordpress' => 'WordPress'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanConstruct() {
|
||||||
|
expect($this->cron_trigger->current_method)
|
||||||
|
->equals(CronTrigger::DEFAULT_METHOD);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanGetCurrentMethod() {
|
||||||
|
Setting::setValue(CronTrigger::SETTING_NAME, array('method' => 'MockMethod'));
|
||||||
|
expect($this->cron_trigger->getCurrentMethod())->equals('MockMethod');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanReturnAvailableMethods() {
|
||||||
|
expect($this->cron_trigger->getAvailableMethods())
|
||||||
|
->equals(CronTrigger::$available_methods);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanInitializeCronTriggerMethod() {
|
||||||
|
$cron_trigger = $this->cron_trigger;
|
||||||
|
$cron_trigger->current_method = 'MockMethod';
|
||||||
|
expect($cron_trigger->init())->true();
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItReturnsFalseWhenItCantInitializeCronTriggerMethod() {
|
||||||
|
$cron_trigger = $this->cron_trigger;
|
||||||
|
$cron_trigger->current_method = 'MockInvalidMethod';
|
||||||
|
expect($cron_trigger->init())->false();
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItIgnoresExceptionsThrownFromCronTriggerMethods() {
|
||||||
|
$cron_trigger = $this->cron_trigger;
|
||||||
|
$cron_trigger->current_method = 'MockMethodWithException';
|
||||||
|
expect($cron_trigger->init())->null();
|
||||||
|
}
|
||||||
|
|
||||||
|
function _after() {
|
||||||
|
ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user