Skip to content

Commit e5de6c8

Browse files
committed
create from date time interface for other setters
1 parent 28940f8 commit e5de6c8

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/Entity/CronJob.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Shapecode\Bundle\CronBundle\Entity;
66

77
use Cron\CronExpression;
8+
use DateTime;
89
use DateTimeInterface;
910
use Doctrine\Common\Collections\ArrayCollection;
1011
use Doctrine\Common\Collections\Collection;
@@ -43,7 +44,7 @@ class CronJob extends AbstractEntity
4344
private DateTimeInterface $nextRun;
4445

4546
/** @var Collection<int, CronJobResult>*/
46-
#[ORM\OneToMany(mappedBy: 'cronJob', targetEntity: CronJobResult::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
47+
#[ORM\OneToMany(targetEntity: CronJobResult::class, mappedBy: 'cronJob', cascade: ['persist', 'remove'], orphanRemoval: true)]
4748
private Collection $results;
4849

4950
#[ORM\Column(type: Types::BOOLEAN, options: ['default' => true])]
@@ -169,14 +170,14 @@ public function getLastUse(): DateTimeInterface|null
169170

170171
public function setLastUse(DateTimeInterface $lastUse): self
171172
{
172-
$this->lastUse = $lastUse;
173+
$this->lastUse = DateTime::createFromInterface($lastUse);
173174

174175
return $this;
175176
}
176177

177178
public function setNextRun(DateTimeInterface $nextRun): self
178179
{
179-
$this->nextRun = $nextRun;
180+
$this->nextRun = DateTime::createFromInterface($nextRun);
180181

181182
return $this;
182183
}

tests/Entity/CronJobResultTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shapecode\Bundle\CronBundle\Tests\Entity;
6+
7+
use DateTimeZone;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
use Shapecode\Bundle\CronBundle\Entity\CronJob;
11+
use Shapecode\Bundle\CronBundle\Entity\CronJobResult;
12+
use Symfony\Component\Clock\DatePoint;
13+
14+
#[CoversClass(CronJobResult::class)]
15+
class CronJobResultTest extends TestCase
16+
{
17+
public function testCreation(): void
18+
{
19+
$result = new CronJobResult(
20+
new CronJob('command', '@daily'),
21+
0.0,
22+
0,
23+
null,
24+
new DatePoint('2025-03-02 19:06:00', new DateTimeZone('UTC')),
25+
);
26+
27+
self::assertSame('command - 02.03.2025 19:06 +00:00', $result->__toString());
28+
}
29+
}

tests/Entity/CronJobTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shapecode\Bundle\CronBundle\Tests\Entity;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use Shapecode\Bundle\CronBundle\Entity\CronJob;
10+
use Symfony\Component\Clock\DatePoint;
11+
12+
#[CoversClass(CronJob::class)]
13+
class CronJobTest extends TestCase
14+
{
15+
public function testCreation(): void
16+
{
17+
$job = new CronJob('command', '@daily');
18+
$job->setLastUse(new DatePoint());
19+
$job->calculateNextRun();
20+
21+
self::assertSame('command', $job->getCommand());
22+
}
23+
}

0 commit comments

Comments
 (0)