-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use minute period and one-time-trigger
- Loading branch information
1 parent
aa92d42
commit 7780494
Showing
4 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Runtime\Check\Service; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use Symfony\Component\Scheduler\Trigger\TriggerInterface; | ||
|
||
class OneTimeTrigger implements TriggerInterface | ||
{ | ||
private ?DateTimeImmutable $run = null; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __toString(): string | ||
{ | ||
if ($this->run === null) { | ||
return 'one time'; | ||
} | ||
return 'one time (already running)'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable | ||
{ | ||
if (null === $this->run) { | ||
$this->run = $run; | ||
return $run; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Runtime\Check\Test\Service; | ||
|
||
use Atoolo\Runtime\Check\Service\OneTimeTrigger; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(OneTimeTrigger::class)] | ||
class OneTimeTriggerTest extends TestCase | ||
{ | ||
public function testToString(): void | ||
{ | ||
$trigger = new OneTimeTrigger(); | ||
|
||
$this->assertEquals( | ||
'one time', | ||
(string) $trigger | ||
); | ||
} | ||
|
||
public function testToStringAlreadyRunning(): void | ||
{ | ||
$trigger = new OneTimeTrigger(); | ||
$trigger->getNextRunDate(new \DateTimeImmutable()); | ||
|
||
$this->assertEquals( | ||
'one time (already running)', | ||
(string) $trigger | ||
); | ||
} | ||
|
||
public function testGetNextRunDate(): void | ||
{ | ||
$trigger = new OneTimeTrigger(); | ||
|
||
$run = new \DateTimeImmutable(); | ||
$this->assertEquals( | ||
$run, | ||
$trigger->getNextRunDate($run), | ||
'The first call to getNextRunDate should return the same date' | ||
); | ||
} | ||
|
||
public function testSecondGetNextRunDate(): void | ||
{ | ||
$trigger = new OneTimeTrigger(); | ||
$run = new \DateTimeImmutable(); | ||
$trigger->getNextRunDate($run); | ||
|
||
$this->assertNull( | ||
$trigger->getNextRunDate($run), | ||
'The second call to getNextRunDate should return null' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters