From 653ca32dfe4a2e67f465f1834440b8b603e2edb2 Mon Sep 17 00:00:00 2001 From: Philipp Reisigl Date: Fri, 8 Sep 2023 15:31:35 +0200 Subject: [PATCH] use $this->db directly in tests --- tests/ExecutionLogTest.php | 56 ++++++------ tests/ExecutorTest.php | 136 ++++++++++++++---------------- tests/Testclasses/SomeCronJob.php | 3 +- 3 files changed, 92 insertions(+), 103 deletions(-) diff --git a/tests/ExecutionLogTest.php b/tests/ExecutionLogTest.php index 7f50da2..e973db3 100644 --- a/tests/ExecutionLogTest.php +++ b/tests/ExecutionLogTest.php @@ -6,6 +6,7 @@ use Atk4\Data\Persistence\Sql; use Atk4\Data\Schema\TestCase; +use DateTime; use PhilippR\Atk4\Cron\ExecutionLog; use PhilippR\Atk4\Cron\Executor; use PhilippR\Atk4\Cron\Scheduler; @@ -22,14 +23,14 @@ protected function setUp(): void $this->createMigrator(new Scheduler($this->db))->create(); $this->createMigrator(new ExecutionLog($this->db))->create(); } + public function testDurationIsLogged() { - $persistence = $this->db; - $testTime = new \DateTime('2020-05-05'); + $testTime = new DateTime('2020-05-05'); $testTime->setTime(3, 3); $scheduler1 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -39,7 +40,7 @@ public function testDurationIsLogged() ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -53,12 +54,11 @@ public function testDurationIsLogged() public function testExecutionSuccessIsLogged(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-09-07'); + $testTime = new DateTime('2020-09-07'); $testTime->setTime(3, 3); $scheduler1 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -68,7 +68,7 @@ public function testExecutionSuccessIsLogged(): void $scheduler2 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'cronjob_class' => SomeCronJobWithExceptionInExecute::class, 'interval' => 'YEARLY', @@ -78,7 +78,7 @@ public function testExecutionSuccessIsLogged(): void ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); self::assertTrue( @@ -91,12 +91,11 @@ public function testExecutionSuccessIsLogged(): void public function testLoggingOptionNoLogging(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-09-07'); + $testTime = new DateTime('2020-09-07'); $testTime->setTime(3, 3); $scheduler1 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -105,23 +104,22 @@ public function testLoggingOptionNoLogging(): void ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); self::assertSame( 0, - (int)(new ExecutionLog($persistence))->action('count')->getOne() + (int)(new ExecutionLog($this->db))->action('count')->getOne() ); } public function testLoggingOptionOnlyIfLogOutput(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-09-07'); + $testTime = new DateTime('2020-09-07'); $testTime->setTime(3, 3); $scheduler1 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -130,7 +128,7 @@ public function testLoggingOptionOnlyIfLogOutput(): void ); //does not produce any output, should not produce log $scheduler2 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'cronjob_class' => SomeOtherCronJob::class, 'interval' => 'YEARLY', @@ -139,12 +137,12 @@ public function testLoggingOptionOnlyIfLogOutput(): void ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); self::assertSame( 1, - (int)(new ExecutionLog($persistence))->action('count')->getOne() + (int)(new ExecutionLog($this->db))->action('count')->getOne() ); self::assertSame( 1, @@ -158,12 +156,11 @@ public function testLoggingOptionOnlyIfLogOutput(): void public function testLoggingOptionAlwaysLog(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-09-07'); + $testTime = new DateTime('2020-09-07'); $testTime->setTime(3, 3); $scheduler1 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -172,7 +169,7 @@ public function testLoggingOptionAlwaysLog(): void ); //does not produce any output, should not produce log $scheduler2 = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'cronjob_class' => SomeOtherCronJob::class, 'interval' => 'YEARLY', @@ -182,12 +179,12 @@ public function testLoggingOptionAlwaysLog(): void ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); self::assertSame( 2, - (int)(new ExecutionLog($persistence))->action('count')->getOne() + (int)(new ExecutionLog($this->db))->action('count')->getOne() ); self::assertSame( 1, @@ -201,18 +198,17 @@ public function testLoggingOptionAlwaysLog(): void public function testLastExecutedSaved(): void { - $persistence = $this->db; - $dateTime = new \DateTime(); + $dateTime = new DateTime(); //this one should be executed $entity = ExecutorTest::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_MINUTE', ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run(); $entity->reload(); diff --git a/tests/ExecutorTest.php b/tests/ExecutorTest.php index af78f30..e8c55e7 100644 --- a/tests/ExecutorTest.php +++ b/tests/ExecutorTest.php @@ -7,15 +7,16 @@ use Atk4\Data\Persistence; use Atk4\Data\Persistence\Sql; use Atk4\Data\Schema\TestCase; +use DateTime; +use PhilippR\Atk4\Cron\ExecutionLog; use PhilippR\Atk4\Cron\Executor; use PhilippR\Atk4\Cron\Scheduler; use PhilippR\Atk4\Cron\Tests\Testclasses\SomeCronJob; use PhilippR\Atk4\Cron\Tests\Testclasses\SomeCronJobWithExceptionInExecute; -use PhilippR\Atk4\Cron\ExecutionLog; class ExecutorTest extends TestCase { - + protected function setUp(): void { parent::setUp(); @@ -23,15 +24,14 @@ protected function setUp(): void $this->createMigrator(new Scheduler($this->db))->create(); $this->createMigrator(new ExecutionLog($this->db))->create(); } - + public function testRunYearly(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-11-05'); + $testTime = new DateTime('2020-11-05'); $testTime->setTime(3, 11); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -39,7 +39,7 @@ public function testRunYearly(): void ] ); $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -47,7 +47,7 @@ public function testRunYearly(): void ] ); $scheduler3 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -55,7 +55,7 @@ public function testRunYearly(): void ] ); $scheduler4 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => (clone $testTime)->modify('+1 Day'), @@ -64,7 +64,7 @@ public function testRunYearly(): void ); //only one should be executed - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -81,19 +81,18 @@ public function testRunYearly(): void public function testSkipYearlyIfNoDateYearlySet(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-05-05'); + $testTime = new DateTime('2020-05-05'); $testTime->setTime(3, 3); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'time_yearly' => $testTime, ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -102,12 +101,11 @@ public function testSkipYearlyIfNoDateYearlySet(): void public function testRunMonthly(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-08-05'); + $testTime = new DateTime('2020-08-05'); $testTime->setTime(3, 14); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MONTHLY', 'day_monthly' => (int)$testTime->format('d'), @@ -115,7 +113,7 @@ public function testRunMonthly(): void ] ); $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MONTHLY', 'day_monthly' => (int)$testTime->format('d'), @@ -123,7 +121,7 @@ public function testRunMonthly(): void ] ); $scheduler3 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MONTHLY', 'day_monthly' => (int)$testTime->format('d'), @@ -131,7 +129,7 @@ public function testRunMonthly(): void ] ); $scheduler4 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MONTHLY', 'day_monthly' => (int)(clone $testTime)->modify('+1 Day')->format('d'), @@ -139,7 +137,7 @@ public function testRunMonthly(): void ] ); $scheduler5 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MONTHLY', 'day_monthly' => (int)(clone $testTime)->modify('+1 Day')->format('d'), @@ -148,7 +146,7 @@ public function testRunMonthly(): void ); //only one should be executed - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -166,19 +164,18 @@ public function testRunMonthly(): void public function testSkipMonthlyIfNoTimeSet(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-05-05'); + $testTime = new DateTime('2020-05-05'); $testTime->setTime(3, 3); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MONTHLY', 'day_monthly' => 5, ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -187,12 +184,11 @@ public function testSkipMonthlyIfNoTimeSet(): void public function testRunWeekly(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-06-03'); + $testTime = new DateTime('2020-06-03'); $testTime->setTime(4, 34); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'WEEKLY', 'weekday_weekly' => (int)$testTime->format('N'), @@ -200,7 +196,7 @@ public function testRunWeekly(): void ] ); $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'WEEKLY', 'weekday_weekly' => (int)$testTime->format('N'), @@ -208,7 +204,7 @@ public function testRunWeekly(): void ] ); $scheduler3 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'WEEKLY', 'weekday_weekly' => (int)$testTime->format('N'), @@ -216,7 +212,7 @@ public function testRunWeekly(): void ] ); $scheduler4 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'WEEKLY', 'weekday_weekly' => (int)(clone $testTime)->modify('-1 Day')->format('N'), @@ -224,7 +220,7 @@ public function testRunWeekly(): void ] ); $scheduler5 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'WEEKLY', 'weekday_weekly' => (int)(clone $testTime)->modify('+1 Day')->format('N'), @@ -233,7 +229,7 @@ public function testRunWeekly(): void ); //only one should be executed - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -251,27 +247,26 @@ public function testRunWeekly(): void public function testRunDaily(): void { - $persistence = $this->db; - $testTime = new \DateTime(); + $testTime = new DateTime(); $testTime->setTime(15, 3); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'DAILY', 'time_daily' => $testTime, ] ); $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'DAILY', 'time_daily' => (clone $testTime)->modify('-1 Minute') ] ); $scheduler3 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'DAILY', 'time_daily' => (clone $testTime)->modify('+1 Minute') @@ -279,7 +274,7 @@ public function testRunDaily(): void ); //only one should be executed - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -293,33 +288,32 @@ public function testRunDaily(): void public function testRunHourly(): void { - $persistence = $this->db; - $testTime = new \DateTime(); + $testTime = new DateTime(); $testTime->setTime(14, 35); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'HOURLY', 'minute_hourly' => (int)$testTime->format('i') ] ); $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'HOURLY', 'minute_hourly' => (int)(clone $testTime)->modify('+1 Minute')->format('i') ] ); $scheduler3 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'HOURLY', 'minute_hourly' => (int)(clone $testTime)->modify('-1 Minute')->format('i') ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -333,33 +327,32 @@ public function testRunHourly(): void public function testRunMinutely(): void { - $persistence = $this->db; - $testTime = new \DateTime(); + $testTime = new DateTime(); $testTime->setTime(3, 16); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_MINUTE', ] ); $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTH_MINUTE', ] ); $scheduler3 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTEENTH_MINUTE', ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -373,13 +366,12 @@ public function testRunMinutely(): void public function testRunMinutelyOffset(): void { - $persistence = $this->db; - $testTime = new \DateTime(); + $testTime = new DateTime(); $testTime->setTime(3, 18); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTH_MINUTE', @@ -388,7 +380,7 @@ public function testRunMinutelyOffset(): void ); //this one should be executed, too $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTEENTH_MINUTE', @@ -396,21 +388,21 @@ public function testRunMinutelyOffset(): void ] ); $scheduler3 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTH_MINUTE', ] ); $scheduler4 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MINUTELY', 'interval_minutely' => 'EVERY_FIFTEENTH_MINUTE', ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -426,12 +418,11 @@ public function testRunMinutelyOffset(): void public function testNonActiveCronInRun(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-05-05'); + $testTime = new DateTime('2020-05-05'); $testTime->setTime(3, 3); //this one should be executed $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'MONTHLY', 'day_monthly' => 5, @@ -441,7 +432,7 @@ public function testNonActiveCronInRun(): void $scheduler1->set('is_active', 0); $scheduler1->save(); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -449,7 +440,7 @@ public function testNonActiveCronInRun(): void $scheduler1->set('is_active', 1); $scheduler1->save(); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -458,12 +449,11 @@ public function testNonActiveCronInRun(): void public function testExceptionInExecuteDoesNotStopExecutionOfOthers(): void { - $persistence = $this->db; - $testTime = new \DateTime('2020-05-05'); + $testTime = new DateTime('2020-05-05'); $testTime->setTime(3, 3); $scheduler1 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'YEARLY', 'date_yearly' => $testTime, @@ -474,14 +464,14 @@ public function testExceptionInExecuteDoesNotStopExecutionOfOthers(): void $scheduler1->save(); $scheduler2 = self::getScheduler( - $persistence, + $this->db, [ 'interval' => 'DAILY', 'time_daily' => $testTime, ] ); - $executor = new Executor($persistence); + $executor = new Executor($this->db); $executor->run($testTime); $scheduler1->reload(); @@ -493,7 +483,7 @@ public function testExceptionInExecuteDoesNotStopExecutionOfOthers(): void public static function getScheduler(Persistence $persistence, array $set = []): Scheduler { - $scheduler= (new Scheduler($persistence))->createEntity(); + $scheduler = (new Scheduler($persistence))->createEntity(); $scheduler->set('cronjob_class', SomeCronJob::class); $scheduler->set('is_active', 1); @@ -504,8 +494,10 @@ public static function getScheduler(Persistence $persistence, array $set = []): return $scheduler; } - public static function getLastExecutionLog(Scheduler $scheduler): ?ExecutionLog - { + public + static function getLastExecutionLog( + Scheduler $scheduler + ): ?ExecutionLog { $executionLog = $scheduler->ref(ExecutionLog::class); return $executionLog->tryLoadAny(); } diff --git a/tests/Testclasses/SomeCronJob.php b/tests/Testclasses/SomeCronJob.php index 079280f..93bacff 100644 --- a/tests/Testclasses/SomeCronJob.php +++ b/tests/Testclasses/SomeCronJob.php @@ -5,6 +5,7 @@ namespace PhilippR\Atk4\Cron\Tests\Testclasses; use PhilippR\Atk4\Cron\BaseCronJob; +use stdClass; class SomeCronJob extends BaseCronJob { @@ -17,7 +18,7 @@ public function execute(): void //dummy output for tests, here as string $this->executionLog[] = 'SomeModel With ID=3 deleted'; //dummy log as stdclass; more info can be added here - $stdClassLog = new \stdClass(); + $stdClassLog = new stdClass(); $stdClassLog->message = 'SomeModel deleted'; $stdClassLog->id = 35; $stdClassLog->name = 'SomeName';