From bcf1b37c6a2fc3ad586fc1cee4dc534c1f8da78b Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 31 Aug 2016 20:30:27 -0400 Subject: [PATCH] - Adds unit tests for Cron Trigger class --- lib/Config/Initializer.php | 7 ++- lib/Cron/CronTrigger.php | 2 - tests/unit/Cron/CronTriggerMockMethod.php | 9 +++ .../CronTriggerMockMethodWithException.php | 9 +++ tests/unit/Cron/CronTriggerTest.php | 61 +++++++++++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 tests/unit/Cron/CronTriggerMockMethod.php create mode 100644 tests/unit/Cron/CronTriggerMockMethodWithException.php create mode 100644 tests/unit/Cron/CronTriggerTest.php diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index f7328ed89b..15bf0024af 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -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() { diff --git a/lib/Cron/CronTrigger.php b/lib/Cron/CronTrigger.php index 9d5f88aec8..11b011be14 100644 --- a/lib/Cron/CronTrigger.php +++ b/lib/Cron/CronTrigger.php @@ -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() : diff --git a/tests/unit/Cron/CronTriggerMockMethod.php b/tests/unit/Cron/CronTriggerMockMethod.php new file mode 100644 index 0000000000..26bd8b1f8a --- /dev/null +++ b/tests/unit/Cron/CronTriggerMockMethod.php @@ -0,0 +1,9 @@ +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); + } +} \ No newline at end of file