-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
Fixes wrong datetime type usage
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shapecode\Bundle\CronBundle\Tests\EventListener; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\Persistence\Event\LifecycleEventArgs; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Clock\ClockInterface; | ||
use Shapecode\Bundle\CronBundle\Entity\AbstractEntity; | ||
use Shapecode\Bundle\CronBundle\EventListener\EntitySubscriber; | ||
use stdClass; | ||
|
||
class EntitySubscriberTest extends TestCase | ||
{ | ||
private ClockInterface $clock; | ||
|
||
private EntitySubscriber $subscriber; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->clock = $this->createMock(ClockInterface::class); | ||
$this->subscriber = new EntitySubscriber($this->clock); | ||
} | ||
|
||
public function testPrePersistSetsCreatedAtAndUpdatedAtWhenNull(): void | ||
{ | ||
$now = new DateTimeImmutable('2024-10-10 12:00:00'); | ||
$this->clock->method('now')->willReturn($now); | ||
Check failure on line 32 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
|
||
$entity = $this->createMock(AbstractEntity::class); | ||
|
||
$entity->expects($this->once()) | ||
Check failure on line 36 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
->method('setCreatedAt') | ||
->with($this->isInstanceOf(DateTime::class)); | ||
Check failure on line 38 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
|
||
$entity->expects($this->once()) | ||
Check failure on line 40 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
->method('setUpdatedAt') | ||
->with($this->isInstanceOf(DateTime::class)); | ||
Check failure on line 42 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
|
||
$entity->method('getCreatedAt')->willReturn(null); | ||
|
||
$entityManager = $this->createMock(EntityManagerInterface::class); | ||
$args = new LifecycleEventArgs($entity, $entityManager); | ||
|
||
$this->subscriber->prePersist($args); | ||
} | ||
|
||
public function testPreUpdateSetsUpdatedAt(): void | ||
{ | ||
$now = new DateTimeImmutable('2024-10-10 12:00:00'); | ||
$this->clock->method('now')->willReturn($now); | ||
Check failure on line 55 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
|
||
$entity = $this->createMock(AbstractEntity::class); | ||
|
||
$entity->expects($this->never()) | ||
Check failure on line 59 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
->method('setCreatedAt'); | ||
|
||
$entity->expects($this->once()) | ||
Check failure on line 62 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
->method('setUpdatedAt') | ||
->with($this->isInstanceOf(DateTime::class)); | ||
Check failure on line 64 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
|
||
$entity->method('getCreatedAt')->willReturn(new DateTime()); | ||
|
||
$entityManager = $this->createMock(EntityManagerInterface::class); | ||
$args = new LifecycleEventArgs($entity, $entityManager); | ||
|
||
$this->subscriber->preUpdate($args); | ||
} | ||
|
||
public function testEntityNotInstanceOfAbstractEntity(): void | ||
{ | ||
$nonEntity = new stdClass(); | ||
|
||
$entityManager = $this->createMock(EntityManagerInterface::class); | ||
$args = new LifecycleEventArgs($nonEntity, $entityManager); | ||
|
||
$this->subscriber->prePersist($args); | ||
$this->subscriber->preUpdate($args); | ||
|
||
$this->assertTrue(true); | ||
Check failure on line 84 in tests/EventListener/EntitySubscriberTest.php GitHub Actions / build (8.2)
|
||
} | ||
} |