- Adds unit tests for Cron Trigger class

This commit is contained in:
Vlad
2016-08-31 20:30:27 -04:00
parent 2986cdba85
commit bcf1b37c6a
5 changed files with 84 additions and 4 deletions

View File

@ -192,8 +192,11 @@ class Initializer {
}
function setupCronTrigger() {
$cron_trigger = new CronTrigger();
$cron_trigger->init();
// setup cron trigger only outside of cli environment
if(php_sapi_name() !== 'cli') {
$cron_trigger = new CronTrigger();
$cron_trigger->init();
}
}
function setupImages() {

View File

@ -20,8 +20,6 @@ class CronTrigger {
function init() {
try {
// configure cron trigger only outside of cli environment
if(php_sapi_name() === 'cli') return;
$trigger_class = __NAMESPACE__ . '\Triggers\\' . $this->current_method;
return (class_exists($trigger_class)) ?
$trigger_class::run() :

View File

@ -0,0 +1,9 @@
<?php
namespace MailPoet\Cron\Triggers;
class MockMethod {
static function run() {
return true;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace MailPoet\Cron\Triggers;
class MockMethodWithException {
static function run() {
throw new \Exception('Exception thrown');
}
}

View 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);
}
}