Skip to content

Commit

Permalink
Merge pull request #66 from 4lxndr/fix/65-datetime-immutable
Browse files Browse the repository at this point in the history
Fixes wrong datetime type usage
  • Loading branch information
nicklog authored Oct 26, 2024
2 parents 1341c31 + e43e762 commit 81f3b5d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Command/CronRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Shapecode\Bundle\CronBundle\Command;

use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Clock\ClockInterface;
use Shapecode\Bundle\CronBundle\Collection\CronJobRunningCollection;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(
protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new CronStyle($input, $output);
$now = $this->clock->now();
$now = DateTime::createFromImmutable($this->clock->now());

$jobsToRun = $this->cronJobRepository->findAll();

Expand Down
3 changes: 2 additions & 1 deletion src/EventListener/EntitySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Shapecode\Bundle\CronBundle\EventListener;

use DateTime;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
Expand Down Expand Up @@ -41,7 +42,7 @@ private function setDates(LifecycleEventArgs $args): void
return;
}

$now = $this->clock->now();
$now = DateTime::createFromImmutable($this->clock->now());

if ($entity->getCreatedAt() === null) {
$entity->setCreatedAt($now);
Expand Down
86 changes: 86 additions & 0 deletions tests/EventListener/EntitySubscriberTest.php
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

View workflow job for this annotation

GitHub Actions / build (8.2)

Call to an undefined method Psr\Clock\ClockInterface::method().

Check failure on line 32 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Call to an undefined method Psr\Clock\ClockInterface::method().

$entity = $this->createMock(AbstractEntity::class);

$entity->expects($this->once())

Check failure on line 36 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Dynamic call to static method PHPUnit\Framework\TestCase::once().

Check failure on line 36 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Dynamic call to static method PHPUnit\Framework\TestCase::once().
->method('setCreatedAt')
->with($this->isInstanceOf(DateTime::class));

Check failure on line 38 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Dynamic call to static method PHPUnit\Framework\Assert::isInstanceOf().

Check failure on line 38 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Dynamic call to static method PHPUnit\Framework\Assert::isInstanceOf().

$entity->expects($this->once())

Check failure on line 40 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Dynamic call to static method PHPUnit\Framework\TestCase::once().

Check failure on line 40 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Dynamic call to static method PHPUnit\Framework\TestCase::once().
->method('setUpdatedAt')
->with($this->isInstanceOf(DateTime::class));

Check failure on line 42 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Dynamic call to static method PHPUnit\Framework\Assert::isInstanceOf().

Check failure on line 42 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Dynamic call to static method PHPUnit\Framework\Assert::isInstanceOf().

$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

View workflow job for this annotation

GitHub Actions / build (8.2)

Call to an undefined method Psr\Clock\ClockInterface::method().

Check failure on line 55 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Call to an undefined method Psr\Clock\ClockInterface::method().

$entity = $this->createMock(AbstractEntity::class);

$entity->expects($this->never())

Check failure on line 59 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Dynamic call to static method PHPUnit\Framework\TestCase::never().

Check failure on line 59 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Dynamic call to static method PHPUnit\Framework\TestCase::never().
->method('setCreatedAt');

$entity->expects($this->once())

Check failure on line 62 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Dynamic call to static method PHPUnit\Framework\TestCase::once().

Check failure on line 62 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Dynamic call to static method PHPUnit\Framework\TestCase::once().
->method('setUpdatedAt')
->with($this->isInstanceOf(DateTime::class));

Check failure on line 64 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Dynamic call to static method PHPUnit\Framework\Assert::isInstanceOf().

Check failure on line 64 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Dynamic call to static method PHPUnit\Framework\Assert::isInstanceOf().

$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

View workflow job for this annotation

GitHub Actions / build (8.2)

Call to method PHPUnit\Framework\Assert::assertTrue() with true will always evaluate to true.

Check failure on line 84 in tests/EventListener/EntitySubscriberTest.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Call to method PHPUnit\Framework\Assert::assertTrue() with true will always evaluate to true.
}
}

0 comments on commit 81f3b5d

Please sign in to comment.