Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Util/SubjectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@ public static function getDoctrineProperties(object $subject): mixed
{
$class = get_class($subject);
try {
$ids = self::$registry?->getManagerForClass($class)?->getClassMetadata($class)?->getIdentifierValues($subject);
if (is_array($ids) && 1 === count($ids)) {
return $ids[array_key_first($ids)];
$ids = self::$registry
?->getManagerForClass($class)
?->getClassMetadata($class)
?->getIdentifierValues($subject);
if (is_array($ids)) {
if ([] === array_filter($ids, static fn (mixed $value): bool => null !== $value)) {
return null;
}
if (1 === count($ids)) {
return $ids[array_key_first($ids)];
}
}
return $ids;
} catch (\Throwable) {
Expand Down
25 changes: 25 additions & 0 deletions tests/Fixtures/Doctrine/GeneratedIdEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace WeDevelop\Audit\Tests\Fixtures\Doctrine;

use Doctrine\ORM\Mapping as ORM;

/**
* A mapped entity whose id is database-generated, so it stays null (and absent
* from the metadata identifier values) until the unit of work is flushed. Used
* to exercise the persisted-but-not-flushed identifier path.
*/
#[ORM\Entity]
#[ORM\Table(name: 'generated_id_entity')]
class GeneratedIdEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

public function getId(): ?int
{
return $this->id;
}
}
14 changes: 13 additions & 1 deletion tests/Integration/Util/SubjectHelperDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace WeDevelop\Audit\Tests\Integration\Util;

use WeDevelop\Audit\Entity\Subject;
use WeDevelop\Audit\Tests\Fixtures\Doctrine\CompositeIdEntity;
use WeDevelop\Audit\Tests\Fixtures\Doctrine\GeneratedIdEntity;
use WeDevelop\Audit\Tests\Fixtures\Doctrine\IntegrationEntity;
use WeDevelop\Audit\Tests\Support\DoctrineTestCase;
use WeDevelop\Audit\Util\SubjectHelper;
Expand All @@ -12,7 +14,7 @@ final class SubjectHelperDoctrineTest extends DoctrineTestCase
protected function setUp(): void
{
parent::setUp();
$this->createSchema([IntegrationEntity::class, CompositeIdEntity::class]);
$this->createSchema([IntegrationEntity::class, CompositeIdEntity::class, GeneratedIdEntity::class]);
SubjectHelper::setManagerRegistry($this->registryReturningEntityManager());
}

Expand All @@ -34,6 +36,16 @@ public function testReadsCompositeIdentifierFromDoctrineMetadata(): void
self::assertSame(['first' => 1, 'second' => 'abc'], SubjectHelper::getObjectIdentifier($entity));
}

public function testUnflushedGeneratedIdentifierResolvesToNull(): void
{
$entity = new GeneratedIdEntity();
$this->em->persist($entity);
// Intentionally not flushed: the generated id is not yet assigned, so
// getIdentifierValues() returns an empty array.
self::assertNull(SubjectHelper::getObjectIdentifier($entity));
self::assertNull(Subject::fromObject($entity)->identifier);
}

public function testResolvesConcreteClassFromADoctrineProxy(): void
{
$this->em->persist(new IntegrationEntity(5));
Expand Down
Loading