Skip to content

Commit

Permalink
Prevent crash when generating ids that have native type info (#37)
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
Firehed authored Jul 17, 2023
1 parent bfca1d2 commit 32f886d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ parameters:
count: 1
path: tests/Entities/StringId.php

-
message: "#^Property Firehed\\\\Mocktrine\\\\Entities\\\\TypedId\\:\\:\\$id is never written, only read\\.$#"
count: 1
path: tests/Entities/TypedId.php

-
message: "#^Property Firehed\\\\Mocktrine\\\\Entities\\\\UnspecifiedId\\:\\:\\$id is never written, only read\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion src/InMemoryEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function flush()
$rp = new \ReflectionProperty($className, $idField);
$rp->setAccessible(true);
foreach ($entities as $entity) {
if ($rp->getValue($entity) === null) {
if (!$rp->isInitialized($entity) || $rp->getValue($entity) === null) {
$id = random_int(0, PHP_INT_MAX);
if ($idType === 'string') {
$id = (string) $id;
Expand Down
31 changes: 31 additions & 0 deletions tests/Entities/TypedId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Firehed\Mocktrine\Entities;

use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="typed_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'typed_ids')]
class TypedId
{
/**
* @Id
* @Column
* @GeneratedValue
*/
#[Mapping\Id]
#[Mapping\Column]
#[Mapping\GeneratedValue]
private int $id;

public function getId(): int
{
return $this->id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="https://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://doctrine-project.org/schemas/orm/doctrine-mapping
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Firehed\Mocktrine\Entities\TypedId" table="typed_ids">

<id name="id" type="int" column="id">
<generator strategy="AUTO"/>
</id>

</entity>

</doctrine-mapping>
9 changes: 9 additions & 0 deletions tests/InMemoryEntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ public function testStringIdIsGenerated(): void
$this->assertIsString($sid->getId());
}

public function testTypedIdIsGenerated(): void
{
$tsid = new Entities\TypedId();
$em = $this->getEntityManager();
$em->persist($tsid);
$em->flush();
$this->assertIsInt($tsid->getId());
}

public function testUnspecifiedIdIsString(): void
{
$sid = new Entities\UnspecifiedId();
Expand Down

0 comments on commit 32f886d

Please sign in to comment.