diff --git a/README.md b/README.md index c943239..81a718f 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ of many different cronjobs. Many different schedule options from yearly to minut The logic of these cronjobs has to be implemented separately. The repository consists of these classes: -* BaseCronJob: This is a base for all real cronjobs -* CronJobExecutor: This class contains all the logic to decide when which cronjob should be executed. -* CronJobModel: This is used to persist each wanted execution. It contains the info which cronjob should be executed when. -* CronJobloader: This is a small helper. As all Cronjobs are stored as PHP classes, this class checks given directories for available cronjob implementations. -* CronJobExecutionLog: In here, execution results are logged. If execution results should be logged can be defined per CronJobModel. \ No newline at end of file +* BaseCronJob: This is a base for all real cronjobs. The actual logic for a cronjob is implemented in a class extending BaseCronJob. +* Scheduler: This is used to persist each wanted execution. It contains the info which BaseCronJob child should be executed when. +* Executor: This class contains all the logic to decide when which Scheduler (and hence the corresponding BaseCronJob child) should be executed. +* ExecutionLog: In here, execution results are logged. If execution results should be logged can be defined per CronJobModel. +* CronJobLoader: This is a small helper. As all Cronjobs are stored as PHP classes, this class checks given directories for available BaseCronJob implementations. diff --git a/config.php b/config.php deleted file mode 100644 index f59f854..0000000 --- a/config.php +++ /dev/null @@ -1,19 +0,0 @@ - 'Montag', - 2 => 'Dienstag', - 3 => 'Mittwoch', - 4 => 'Donnerstag', - 5 => 'Freitag', - 6 => 'Samstag', - 7 => 'Sonntag', - ] -); \ No newline at end of file diff --git a/coverage/phpunit.cov b/coverage/phpunit.cov index 3875841..d13a9a6 100644 Binary files a/coverage/phpunit.cov and b/coverage/phpunit.cov differ diff --git a/src/CronJobExecutionLog.php b/src/ExecutionLog.php similarity index 91% rename from src/CronJobExecutionLog.php rename to src/ExecutionLog.php index 1531c4b..730cad3 100644 --- a/src/CronJobExecutionLog.php +++ b/src/ExecutionLog.php @@ -7,7 +7,7 @@ use Atk4\Core\Exception; use Atk4\Data\Model; -class CronJobExecutionLog extends Model +class ExecutionLog extends Model { public $table = 'cronjob_execution_log'; @@ -55,7 +55,7 @@ protected function init(): void ] ); - $this->hasOne('cronjob_id', ['model' => [CronJobModel::class]]); + $this->hasOne('cronjob_id', ['model' => [Scheduler::class]]); $this->setOrder(['execution_datetime' => 'desc']); } } \ No newline at end of file diff --git a/src/CronJobExecutor.php b/src/Executor.php similarity index 82% rename from src/CronJobExecutor.php rename to src/Executor.php index c0e046a..c493874 100644 --- a/src/CronJobExecutor.php +++ b/src/Executor.php @@ -11,7 +11,7 @@ use Throwable; -class CronJobExecutor +class Executor { protected Persistence $persistence; @@ -47,8 +47,8 @@ public function run(DateTime $dateTime = null): void $this->currentMinute = (int)$dateTime->format('i'); //execute yearly first, minutely last. - foreach (CronJobModel::$intervalSettings as $interval => $intervalName) { - $cronJobModels = new CronJobModel($this->persistence); + foreach (Scheduler::$intervalSettings as $interval => $intervalName) { + $cronJobModels = new Scheduler($this->persistence); $cronJobModels->addCondition('interval', $interval); $cronJobModels->addCondition('is_active', 1); @@ -59,12 +59,12 @@ public function run(DateTime $dateTime = null): void } /** - * @param CronJobModel $cronJobEntity + * @param Scheduler $cronJobEntity * @return void * @throws \Atk4\Core\Exception * @throws Exception */ - protected function executeCronIfScheduleMatches(CronJobModel $cronJobEntity): void + protected function executeCronIfScheduleMatches(Scheduler $cronJobEntity): void { $entityInterval = $cronJobEntity->get('interval'); if ($entityInterval === 'YEARLY' && $this->checkYearlyExecutionIsNow($cronJobEntity)) { @@ -83,10 +83,10 @@ protected function executeCronIfScheduleMatches(CronJobModel $cronJobEntity): vo } /** - * @param CronJobModel $cronJobEntity + * @param Scheduler $cronJobEntity * @return bool */ - protected function checkYearlyExecutionIsNow(CronJobModel $cronJobEntity): bool + protected function checkYearlyExecutionIsNow(Scheduler $cronJobEntity): bool { if ( !$cronJobEntity->get('date_yearly') instanceof DateTimeInterface @@ -106,10 +106,10 @@ protected function checkYearlyExecutionIsNow(CronJobModel $cronJobEntity): bool } /** - * @param CronJobModel $cronJobEntity + * @param Scheduler $cronJobEntity * @return bool */ - protected function checkMonthlyExecutionIsNow(CronJobModel $cronJobEntity): bool + protected function checkMonthlyExecutionIsNow(Scheduler $cronJobEntity): bool { if ( $cronJobEntity->get('day_monthly') < 1 @@ -128,10 +128,10 @@ protected function checkMonthlyExecutionIsNow(CronJobModel $cronJobEntity): bool } /** - * @param CronJobModel $cronJobEntity + * @param Scheduler $cronJobEntity * @return bool */ - protected function checkWeeklyExecutionIsNow(CronJobModel $cronJobEntity): bool + protected function checkWeeklyExecutionIsNow(Scheduler $cronJobEntity): bool { if ( $cronJobEntity->get('weekday_weekly') !== $this->currentWeekday @@ -144,10 +144,10 @@ protected function checkWeeklyExecutionIsNow(CronJobModel $cronJobEntity): bool } /** - * @param CronJobModel $cronJobEntity + * @param Scheduler $cronJobEntity * @return bool */ - protected function checkDailyExecutionIsNow(CronJobModel $cronJobEntity): bool + protected function checkDailyExecutionIsNow(Scheduler $cronJobEntity): bool { if ( !$cronJobEntity->get('time_daily') @@ -159,19 +159,19 @@ protected function checkDailyExecutionIsNow(CronJobModel $cronJobEntity): bool } /** - * @param CronJobModel $cronJobEntity + * @param Scheduler $cronJobEntity * @return bool */ - protected function checkHourlyExecutionIsNow(CronJobModel $cronJobEntity): bool + protected function checkHourlyExecutionIsNow(Scheduler $cronJobEntity): bool { return $this->currentMinute === $cronJobEntity->get('minute_hourly'); } /** - * @param CronJobModel $cronJobEntity + * @param Scheduler $cronJobEntity * @return bool */ - protected function checkMinutelyExecutionIsNow(CronJobModel $cronJobEntity): bool + protected function checkMinutelyExecutionIsNow(Scheduler $cronJobEntity): bool { if ($cronJobEntity->get('interval_minutely') == 'EVERY_MINUTE') { return true; @@ -190,14 +190,14 @@ protected function checkMinutelyExecutionIsNow(CronJobModel $cronJobEntity): boo } /** - * @param CronJobModel $entity + * @param Scheduler $entity * @return void * @throws \Atk4\Core\Exception * @throws Exception */ - protected function executeCronJob(CronJobModel $entity): void + protected function executeCronJob(Scheduler $entity): void { - $executionLog = (new CronJobExecutionLog($this->persistence))->createEntity(); + $executionLog = (new ExecutionLog($this->persistence))->createEntity(); $executionLog->set('cronjob_id', $entity->getId()); $executionLog->set('execution_datetime', new DateTime()); $startOfCron = microtime(true); @@ -232,10 +232,10 @@ protected function executeCronJob(CronJobModel $entity): void * This function can be implemented in child classes which extend this class in order you want some custom reporting * (e.g. an Email), per successful cronjob run * - * @param CronJobExecutionLog $cronJobExecutionLog + * @param ExecutionLog $cronJobExecutionLog * @return void */ - protected function reportSuccess(CronJobExecutionLog $cronJobExecutionLog): void + protected function reportSuccess(ExecutionLog $cronJobExecutionLog): void { } @@ -243,11 +243,11 @@ protected function reportSuccess(CronJobExecutionLog $cronJobExecutionLog): void * This function can be implemented in child classes which extend this class in order you want some custom reporting * (e.g. an Email), per successful cronjob run * - * @param CronJobExecutionLog $cronJobExecutionLog + * @param ExecutionLog $cronJobExecutionLog * @param Throwable $e * @return void */ - protected function reportFailure(CronJobExecutionLog $cronJobExecutionLog, Throwable $e): void + protected function reportFailure(ExecutionLog $cronJobExecutionLog, Throwable $e): void { } } \ No newline at end of file diff --git a/src/CronJobModel.php b/src/Scheduler.php similarity index 96% rename from src/CronJobModel.php rename to src/Scheduler.php index 93d840b..3944311 100644 --- a/src/CronJobModel.php +++ b/src/Scheduler.php @@ -7,7 +7,7 @@ use Atk4\Data\Model; -class CronJobModel extends Model +class Scheduler extends Model { public $table = 'cronjob'; @@ -188,13 +188,13 @@ protected function init(): void ); $this->hasMany( - CronJobExecutionLog::class, - ['model' => [CronJobExecutionLog::class], 'theirField' => 'cronjob_id'] + ExecutionLog::class, + ['model' => [ExecutionLog::class], 'theirField' => 'cronjob_id'] ); $this->addExpression( 'last_executed', [ - 'expr' => $this->refLink(CronJobExecutionLog::class) + 'expr' => $this->refLink(ExecutionLog::class) ->action('field', ['execution_datetime']) ->limit(1), 'type' => 'datetime', diff --git a/tests/CronJobModelTest.php b/tests/CronJobModelTest.php deleted file mode 100644 index 0cf9d60..0000000 --- a/tests/CronJobModelTest.php +++ /dev/null @@ -1,58 +0,0 @@ -getSqliteTestPersistence()))->createEntity(); - $cronJobModel->set('cronjob_class', SomeCronJob::class); - $cronJobModel->save(); - self::assertSame( - 'SomeNameForThisCron', - $cronJobModel->get('name') - ); - self::assertSame( - 'SomeDescriptionExplainingWhatThisIsDoing', - $cronJobModel->get('description') - ); - } - - public function testNameAndDescriptionNotChangedIfAlreadySet() - { - $cronJobModel = (new CronJobModel($this->getSqliteTestPersistence()))->createEntity(); - $cronJobModel->set('cronjob_class', SomeCronJob::class); - $cronJobModel->set('name', 'someName'); - $cronJobModel->set('description', 'someDescription'); - $cronJobModel->save(); - self::assertSame( - 'someName', - $cronJobModel->get('name') - ); - self::assertSame( - 'someDescription', - $cronJobModel->get('description') - ); - } - - public function testNameAndDescriptionNotLoadedIfNoCronJobModelSet() - { - $cronJobModel = (new CronJobModel($this->getSqliteTestPersistence()))->createEntity(); - $cronJobModel->save(); - self::assertNull($cronJobModel->get('name')); - self::assertNull($cronJobModel->get('description')); - } - -} \ No newline at end of file diff --git a/tests/CronJobExecutorLoggingTest.php b/tests/ExecutionLogTest.php similarity index 66% rename from tests/CronJobExecutorLoggingTest.php rename to tests/ExecutionLogTest.php index b6b07ec..69f6596 100644 --- a/tests/CronJobExecutorLoggingTest.php +++ b/tests/ExecutionLogTest.php @@ -2,22 +2,23 @@ declare(strict_types=1); +namespace cronforatk\tests; use Atk4\Data\Persistence; use atkextendedtestcase\TestCase; -use cronforatk\CronJobExecutionLog; -use cronforatk\CronJobExecutor; -use cronforatk\CronJobModel; +use cronforatk\ExecutionLog; +use cronforatk\Executor; +use cronforatk\Scheduler; use cronforatk\tests\testclasses\SomeCronJob; use cronforatk\tests\testclasses\SomeCronJobWithExceptionInExecute; use cronforatk\tests\testclasses2\SomeOtherCronJob; -class CronJobExecutorLoggingTest extends TestCase +class ExecutionLogTest extends TestCase { protected array $sqlitePersistenceModels = [ - CronJobModel::class, - CronJobExecutionLog::class + Scheduler::class, + ExecutionLog::class ]; public function testDurationIsLogged() @@ -26,7 +27,7 @@ public function testDurationIsLogged() $testTime = new \DateTime('2020-05-05'); $testTime->setTime(3, 3); - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -35,14 +36,14 @@ public function testDurationIsLogged() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); + $scheduler1->reload(); self::assertEqualsWithDelta( 1.0, - $this->getLastExecutionLog($cronJobModel1)->get('execution_duration'), + $this->getLastExecutionLog($scheduler1)->get('execution_duration'), 0.05 ); } @@ -53,7 +54,7 @@ public function testExecutionSuccessIsLogged(): void $testTime = new \DateTime('2020-09-07'); $testTime->setTime(3, 3); - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -63,7 +64,7 @@ public function testExecutionSuccessIsLogged(): void ); - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'cronjob_class' => SomeCronJobWithExceptionInExecute::class, @@ -74,14 +75,14 @@ public function testExecutionSuccessIsLogged(): void ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); self::assertTrue( - $this->getLastExecutionLog($cronJobModel1)->get('execution_successful') + $this->getLastExecutionLog($scheduler1)->get('execution_successful') ); self::assertFalse( - $this->getLastExecutionLog($cronJobModel2)->get('execution_successful') + $this->getLastExecutionLog($scheduler2)->get('execution_successful') ); } @@ -91,7 +92,7 @@ public function testLoggingOptionNoLogging(): void $testTime = new \DateTime('2020-09-07'); $testTime->setTime(3, 3); - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -101,12 +102,12 @@ public function testLoggingOptionNoLogging(): void ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); self::assertSame( 0, - (int)(new CronJobExecutionLog($persistence))->action('count')->getOne() + (int)(new ExecutionLog($persistence))->action('count')->getOne() ); } @@ -116,7 +117,7 @@ public function testLoggingOptionOnlyIfLogOutput(): void $testTime = new \DateTime('2020-09-07'); $testTime->setTime(3, 3); - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -125,7 +126,7 @@ public function testLoggingOptionOnlyIfLogOutput(): void ] ); //does not produce any output, should not produce log - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'cronjob_class' => SomeOtherCronJob::class, @@ -135,20 +136,20 @@ public function testLoggingOptionOnlyIfLogOutput(): void ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); self::assertSame( 1, - (int)(new CronJobExecutionLog($persistence))->action('count')->getOne() + (int)(new ExecutionLog($persistence))->action('count')->getOne() ); self::assertSame( 1, - (int)$cronJobModel1->ref(CronJobExecutionLog::class)->action('count')->getOne() + (int)$scheduler1->ref(ExecutionLog::class)->action('count')->getOne() ); self::assertSame( 0, - (int)$cronJobModel2->ref(CronJobExecutionLog::class)->action('count')->getOne() + (int)$scheduler2->ref(ExecutionLog::class)->action('count')->getOne() ); } @@ -158,7 +159,7 @@ public function testLoggingOptionAlwaysLog(): void $testTime = new \DateTime('2020-09-07'); $testTime->setTime(3, 3); - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -167,7 +168,7 @@ public function testLoggingOptionAlwaysLog(): void ] ); //does not produce any output, should not produce log - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'cronjob_class' => SomeOtherCronJob::class, @@ -178,20 +179,20 @@ public function testLoggingOptionAlwaysLog(): void ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); self::assertSame( 2, - (int)(new CronJobExecutionLog($persistence))->action('count')->getOne() + (int)(new ExecutionLog($persistence))->action('count')->getOne() ); self::assertSame( 1, - (int)$cronJobModel1->ref(CronJobExecutionLog::class)->action('count')->getOne() + (int)$scheduler1->ref(ExecutionLog::class)->action('count')->getOne() ); self::assertSame( 1, - (int)$cronJobModel2->ref(CronJobExecutionLog::class)->action('count')->getOne() + (int)$scheduler2->ref(ExecutionLog::class)->action('count')->getOne() ); } @@ -200,7 +201,7 @@ public function testLastExecutedSaved() $persistence = $this->getSqliteTestPersistence(); $dateTime = new \DateTime(); //this one should be executed - $entity = $this->_getRecord( + $entity = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', @@ -208,8 +209,8 @@ public function testLastExecutedSaved() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run(); + $executor = new Executor($persistence); + $executor->run(); $entity->reload(); @@ -223,9 +224,9 @@ public function testLastExecutedSaved() ); } - private function _getRecord(Persistence $persistence, array $set = []): CronJobModel + private function _getScheduler(Persistence $persistence, array $set = []): Scheduler { - $entity = (new CronJobModel($persistence))->createEntity(); + $entity = (new Scheduler($persistence))->createEntity(); $entity->set('cronjob_class', SomeCronJob::class); $entity->set('is_active', 1); @@ -236,9 +237,9 @@ private function _getRecord(Persistence $persistence, array $set = []): CronJobM return $entity; } - private function getLastExecutionLog(CronJobModel $cronJobModel): CronJobExecutionLog + private function getLastExecutionLog(Scheduler $scheduler): ExecutionLog { - $executionLog = $cronJobModel->ref(CronJobExecutionLog::class); + $executionLog = $scheduler->ref(ExecutionLog::class); $executionLog = $executionLog->loadAny(); return $executionLog; diff --git a/tests/CronJobExecutorTest.php b/tests/ExecutorTest.php similarity index 57% rename from tests/CronJobExecutorTest.php rename to tests/ExecutorTest.php index 73076c7..2cd2e5b 100644 --- a/tests/CronJobExecutorTest.php +++ b/tests/ExecutorTest.php @@ -6,18 +6,18 @@ use Atk4\Data\Persistence; use atkextendedtestcase\TestCase; -use cronforatk\CronJobExecutionLog; -use cronforatk\CronJobExecutor; -use cronforatk\CronJobModel; +use cronforatk\ExecutionLog; +use cronforatk\Executor; +use cronforatk\Scheduler; use cronforatk\tests\testclasses\SomeCronJob; use cronforatk\tests\testclasses\SomeCronJobWithExceptionInExecute; -class CronJobExecutorTest extends TestCase +class ExecutorTest extends TestCase { protected array $sqlitePersistenceModels = [ - CronJobModel::class, - CronJobExecutionLog::class + Scheduler::class, + ExecutionLog::class ]; @@ -27,7 +27,7 @@ public function testRunYearly(): void $testTime = new \DateTime('2020-11-05'); $testTime->setTime(3, 11); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -35,7 +35,7 @@ public function testRunYearly(): void 'time_yearly' => $testTime ] ); - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -43,7 +43,7 @@ public function testRunYearly(): void 'time_yearly' => (clone $testTime)->modify('-1 Minute'), ] ); - $cronJobModel3 = $this->_getRecord( + $scheduler3 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -51,7 +51,7 @@ public function testRunYearly(): void 'time_yearly' => (clone $testTime)->modify('+1 Minute'), ] ); - $cronJobModel4 = $this->_getRecord( + $scheduler4 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -61,18 +61,18 @@ public function testRunYearly(): void ); //only one should be executed - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); - - $cronJobModel1->reload(); - $cronJobModel2->reload(); - $cronJobModel3->reload(); - $cronJobModel4->reload(); - - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); - self::assertNull($cronJobModel2->get('last_executed')); - self::assertNull($cronJobModel3->get('last_executed')); - self::assertNull($cronJobModel4->get('last_executed')); + $executor = new Executor($persistence); + $executor->run($testTime); + + $scheduler1->reload(); + $scheduler2->reload(); + $scheduler3->reload(); + $scheduler4->reload(); + + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); + self::assertNull($scheduler2->get('last_executed')); + self::assertNull($scheduler3->get('last_executed')); + self::assertNull($scheduler4->get('last_executed')); } public function testSkipYearlyIfNoDateYearlySet() @@ -81,7 +81,7 @@ public function testSkipYearlyIfNoDateYearlySet() $testTime = new \DateTime('2020-05-05'); $testTime->setTime(3, 3); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -89,11 +89,11 @@ public function testSkipYearlyIfNoDateYearlySet() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - self::assertNull($cronJobModel1->get('last_executed')); + $scheduler1->reload(); + self::assertNull($scheduler1->get('last_executed')); } public function testRunMonthly(): void @@ -102,7 +102,7 @@ public function testRunMonthly(): void $testTime = new \DateTime('2020-08-05'); $testTime->setTime(3, 14); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'MONTHLY', @@ -110,7 +110,7 @@ public function testRunMonthly(): void 'time_monthly' => $testTime, ] ); - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'MONTHLY', @@ -118,7 +118,7 @@ public function testRunMonthly(): void 'time_monthly' => (clone $testTime)->modify('-1 Minute'), ] ); - $cronJobModel3 = $this->_getRecord( + $scheduler3 = $this->_getScheduler( $persistence, [ 'interval' => 'MONTHLY', @@ -126,7 +126,7 @@ public function testRunMonthly(): void 'time_monthly' => (clone $testTime)->modify('+1 Minute'), ] ); - $cronJobModel4 = $this->_getRecord( + $scheduler4 = $this->_getScheduler( $persistence, [ 'interval' => 'MONTHLY', @@ -134,7 +134,7 @@ public function testRunMonthly(): void 'time_monthly' => $testTime ] ); - $cronJobModel5 = $this->_getRecord( + $scheduler5 = $this->_getScheduler( $persistence, [ 'interval' => 'MONTHLY', @@ -144,20 +144,20 @@ public function testRunMonthly(): void ); //only one should be executed - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); - - $cronJobModel1->reload(); - $cronJobModel2->reload(); - $cronJobModel3->reload(); - $cronJobModel4->reload(); - $cronJobModel5->reload(); - - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); - self::assertNull($cronJobModel2->get('last_executed')); - self::assertNull($cronJobModel3->get('last_executed')); - self::assertNull($cronJobModel4->get('last_executed')); - self::assertNull($cronJobModel5->get('last_executed')); + $executor = new Executor($persistence); + $executor->run($testTime); + + $scheduler1->reload(); + $scheduler2->reload(); + $scheduler3->reload(); + $scheduler4->reload(); + $scheduler5->reload(); + + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); + self::assertNull($scheduler2->get('last_executed')); + self::assertNull($scheduler3->get('last_executed')); + self::assertNull($scheduler4->get('last_executed')); + self::assertNull($scheduler5->get('last_executed')); } public function testSkipMonthlyIfNoTimeSet() @@ -166,7 +166,7 @@ public function testSkipMonthlyIfNoTimeSet() $testTime = new \DateTime('2020-05-05'); $testTime->setTime(3, 3); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'MONTHLY', @@ -174,11 +174,11 @@ public function testSkipMonthlyIfNoTimeSet() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - self::assertNull($cronJobModel1->get('last_executed')); + $scheduler1->reload(); + self::assertNull($scheduler1->get('last_executed')); } public function testRunWeekly(): void @@ -187,7 +187,7 @@ public function testRunWeekly(): void $testTime = new \DateTime('2020-06-03'); $testTime->setTime(4, 34); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'WEEKLY', @@ -195,7 +195,7 @@ public function testRunWeekly(): void 'time_weekly' => $testTime, ] ); - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'WEEKLY', @@ -203,7 +203,7 @@ public function testRunWeekly(): void 'time_weekly' => (clone $testTime)->modify('-1 Minute'), ] ); - $cronJobModel3 = $this->_getRecord( + $scheduler3 = $this->_getScheduler( $persistence, [ 'interval' => 'WEEKLY', @@ -211,7 +211,7 @@ public function testRunWeekly(): void 'time_weekly' => (clone $testTime)->modify('+1 Minute'), ] ); - $cronJobModel4 = $this->_getRecord( + $scheduler4 = $this->_getScheduler( $persistence, [ 'interval' => 'WEEKLY', @@ -219,7 +219,7 @@ public function testRunWeekly(): void 'time_weekly' => $testTime, ] ); - $cronJobModel5 = $this->_getRecord( + $scheduler5 = $this->_getScheduler( $persistence, [ 'interval' => 'WEEKLY', @@ -229,20 +229,20 @@ public function testRunWeekly(): void ); //only one should be executed - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); - - $cronJobModel1->reload(); - $cronJobModel2->reload(); - $cronJobModel3->reload(); - $cronJobModel4->reload(); - $cronJobModel5->reload(); - - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); - self::assertNull($cronJobModel2->get('last_executed')); - self::assertNull($cronJobModel3->get('last_executed')); - self::assertNull($cronJobModel4->get('last_executed')); - self::assertNull($cronJobModel5->get('last_executed')); + $executor = new Executor($persistence); + $executor->run($testTime); + + $scheduler1->reload(); + $scheduler2->reload(); + $scheduler3->reload(); + $scheduler4->reload(); + $scheduler5->reload(); + + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); + self::assertNull($scheduler2->get('last_executed')); + self::assertNull($scheduler3->get('last_executed')); + self::assertNull($scheduler4->get('last_executed')); + self::assertNull($scheduler5->get('last_executed')); } public function testRunDaily(): void @@ -252,21 +252,21 @@ public function testRunDaily(): void $testTime->setTime(15, 3); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'DAILY', 'time_daily' => $testTime, ] ); - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'DAILY', 'time_daily' => (clone $testTime)->modify('-1 Minute') ] ); - $cronJobModel3 = $this->_getRecord( + $scheduler3 = $this->_getScheduler( $persistence, [ 'interval' => 'DAILY', @@ -275,16 +275,16 @@ public function testRunDaily(): void ); //only one should be executed - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - $cronJobModel2->reload(); - $cronJobModel3->reload(); + $scheduler1->reload(); + $scheduler2->reload(); + $scheduler3->reload(); - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); - self::assertNull($cronJobModel2->get('last_executed')); - self::assertNull($cronJobModel3->get('last_executed')); + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); + self::assertNull($scheduler2->get('last_executed')); + self::assertNull($scheduler3->get('last_executed')); } public function testRunHourly() @@ -293,22 +293,22 @@ public function testRunHourly() $testTime = new \DateTime(); $testTime->setTime(14, 35); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'HOURLY', 'minute_hourly' => (int)$testTime->format('i') ] ); - var_dump($cronJobModel1->get('minute_hourly')); - $cronJobModel2 = $this->_getRecord( + var_dump($scheduler1->get('minute_hourly')); + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'HOURLY', 'minute_hourly' => (int)(clone $testTime)->modify('+1 Minute')->format('i') ] ); - $cronJobModel3 = $this->_getRecord( + $scheduler3 = $this->_getScheduler( $persistence, [ 'interval' => 'HOURLY', @@ -316,16 +316,16 @@ public function testRunHourly() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - $cronJobModel2->reload(); - $cronJobModel3->reload(); + $scheduler1->reload(); + $scheduler2->reload(); + $scheduler3->reload(); - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); - self::assertNull($cronJobModel2->get('last_executed')); - self::assertNull($cronJobModel3->get('last_executed')); + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); + self::assertNull($scheduler2->get('last_executed')); + self::assertNull($scheduler3->get('last_executed')); } public function testRunMinutely() @@ -334,21 +334,21 @@ public function testRunMinutely() $testTime = new \DateTime(); $testTime->setTime(3, 16); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_MINUTE', ] ); - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTH_MINUTE', ] ); - $cronJobModel3 = $this->_getRecord( + $scheduler3 = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', @@ -356,16 +356,16 @@ public function testRunMinutely() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - $cronJobModel2->reload(); - $cronJobModel3->reload(); + $scheduler1->reload(); + $scheduler2->reload(); + $scheduler3->reload(); - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); - self::assertNull($cronJobModel2->get('last_executed')); - self::assertNull($cronJobModel3->get('last_executed')); + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); + self::assertNull($scheduler2->get('last_executed')); + self::assertNull($scheduler3->get('last_executed')); } public function testRunMinutelyOffset() @@ -375,7 +375,7 @@ public function testRunMinutelyOffset() $testTime->setTime(3, 18); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', @@ -384,7 +384,7 @@ public function testRunMinutelyOffset() ] ); //this one should be executed, too - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', @@ -392,14 +392,14 @@ public function testRunMinutelyOffset() 'offset_minutely' => 3, ] ); - $cronJobModel3 = $this->_getRecord( + $scheduler3 = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTH_MINUTE', ] ); - $cronJobModel4 = $this->_getRecord( + $scheduler4 = $this->_getScheduler( $persistence, [ 'interval' => 'MINUTELY', @@ -407,18 +407,18 @@ public function testRunMinutelyOffset() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - $cronJobModel2->reload(); - $cronJobModel3->reload(); - $cronJobModel4->reload(); + $scheduler1->reload(); + $scheduler2->reload(); + $scheduler3->reload(); + $scheduler4->reload(); - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); - self::assertInstanceOf(\Datetime::class, $cronJobModel2->get('last_executed')); - self::assertNull($cronJobModel3->get('last_executed')); - self::assertNull($cronJobModel4->get('last_executed')); + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); + self::assertInstanceOf(\Datetime::class, $scheduler2->get('last_executed')); + self::assertNull($scheduler3->get('last_executed')); + self::assertNull($scheduler4->get('last_executed')); } public function testNonActiveCronInRun() @@ -427,7 +427,7 @@ public function testNonActiveCronInRun() $testTime = new \DateTime('2020-05-05'); $testTime->setTime(3, 3); //this one should be executed - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'MONTHLY', @@ -436,21 +436,21 @@ public function testNonActiveCronInRun() ] ); - $cronJobModel1->set('is_active', 0); - $cronJobModel1->save(); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $scheduler1->set('is_active', 0); + $scheduler1->save(); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - self::assertNull($cronJobModel1->get('last_executed')); + $scheduler1->reload(); + self::assertNull($scheduler1->get('last_executed')); - $cronJobModel1->set('is_active', 1); - $cronJobModel1->save(); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $scheduler1->set('is_active', 1); + $scheduler1->save(); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - self::assertInstanceOf(\Datetime::class, $cronJobModel1->get('last_executed')); + $scheduler1->reload(); + self::assertInstanceOf(\Datetime::class, $scheduler1->get('last_executed')); } public function testExceptionInExecuteDoesNotStopExecutionOfOthers() @@ -459,7 +459,7 @@ public function testExceptionInExecuteDoesNotStopExecutionOfOthers() $testTime = new \DateTime('2020-05-05'); $testTime->setTime(3, 3); - $cronJobModel1 = $this->_getRecord( + $scheduler1 = $this->_getScheduler( $persistence, [ 'interval' => 'YEARLY', @@ -467,10 +467,10 @@ public function testExceptionInExecuteDoesNotStopExecutionOfOthers() 'time_yearly' => $testTime, ] ); - $cronJobModel1->set('cronjob_class', SomeCronJobWithExceptionInExecute::class); - $cronJobModel1->save(); + $scheduler1->set('cronjob_class', SomeCronJobWithExceptionInExecute::class); + $scheduler1->save(); - $cronJobModel2 = $this->_getRecord( + $scheduler2 = $this->_getScheduler( $persistence, [ 'interval' => 'DAILY', @@ -478,26 +478,26 @@ public function testExceptionInExecuteDoesNotStopExecutionOfOthers() ] ); - $cronJobExecutor = new CronJobExecutor($persistence); - $cronJobExecutor->run($testTime); + $executor = new Executor($persistence); + $executor->run($testTime); - $cronJobModel1->reload(); - $cronJobModel2->reload(); + $scheduler1->reload(); + $scheduler2->reload(); - self::assertInstanceOf(\DateTime::class, $cronJobModel2->get('last_executed')); - self::assertInstanceOf(\DateTime::class, $cronJobModel1->get('last_executed')); + self::assertInstanceOf(\DateTime::class, $scheduler2->get('last_executed')); + self::assertInstanceOf(\DateTime::class, $scheduler1->get('last_executed')); } - private function _getRecord(Persistence $persistence, array $set = []): CronJobModel + private function _getScheduler(Persistence $persistence, array $set = []): Scheduler { - $cronJobModel = (new CronJobModel($persistence))->createEntity(); + $scheduler= (new Scheduler($persistence))->createEntity(); - $cronJobModel->set('cronjob_class', SomeCronJob::class); - $cronJobModel->set('is_active', 1); - $cronJobModel->setMulti($set); + $scheduler->set('cronjob_class', SomeCronJob::class); + $scheduler->set('is_active', 1); + $scheduler->setMulti($set); - $cronJobModel->save(); + $scheduler->save(); - return $cronJobModel; + return $scheduler; } } \ No newline at end of file diff --git a/tests/SchedulerTest.php b/tests/SchedulerTest.php new file mode 100644 index 0000000..01a1a4c --- /dev/null +++ b/tests/SchedulerTest.php @@ -0,0 +1,59 @@ +getSqliteTestPersistence()))->createEntity(); + $scheduler->set('cronjob_class', SomeCronJob::class); + $scheduler->save(); + self::assertSame( + 'SomeNameForThisCron', + $scheduler->get('name') + ); + self::assertSame( + 'SomeDescriptionExplainingWhatThisIsDoing', + $scheduler->get('description') + ); + } + + public function testNameAndDescriptionNotChangedIfAlreadySet() + { + $scheduler = (new Scheduler($this->getSqliteTestPersistence()))->createEntity(); + $scheduler->set('cronjob_class', SomeCronJob::class); + $scheduler->set('name', 'someName'); + $scheduler->set('description', 'someDescription'); + $scheduler->save(); + self::assertSame( + 'someName', + $scheduler->get('name') + ); + self::assertSame( + 'someDescription', + $scheduler->get('description') + ); + } + + public function testNameAndDescriptionNotLoadedIfNoCronJobModelSet() + { + $scheduler = (new Scheduler($this->getSqliteTestPersistence()))->createEntity(); + $scheduler->save(); + self::assertNull($scheduler->get('name')); + self::assertNull($scheduler->get('description')); + } + +} \ No newline at end of file