Skip to content

Commit

Permalink
use $this->db directly in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrashoff committed Sep 8, 2023
1 parent 03e3925 commit 653ca32
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 103 deletions.
56 changes: 26 additions & 30 deletions tests/ExecutionLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -39,7 +40,7 @@ public function testDurationIsLogged()
]
);

$executor = new Executor($persistence);
$executor = new Executor($this->db);
$executor->run($testTime);

$scheduler1->reload();
Expand All @@ -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,
Expand All @@ -68,7 +68,7 @@ public function testExecutionSuccessIsLogged(): void


$scheduler2 = ExecutorTest::getScheduler(
$persistence,
$this->db,
[
'cronjob_class' => SomeCronJobWithExceptionInExecute::class,
'interval' => 'YEARLY',
Expand All @@ -78,7 +78,7 @@ public function testExecutionSuccessIsLogged(): void
]
);

$executor = new Executor($persistence);
$executor = new Executor($this->db);
$executor->run($testTime);

self::assertTrue(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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();
Expand Down
Loading

0 comments on commit 653ca32

Please sign in to comment.