Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash when generating ids that have native type info #37

Merged
merged 4 commits into from
Jul 17, 2023
Merged
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
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