Skip to content

Commit

Permalink
Test interactions with readonly ids (#38)
Browse files Browse the repository at this point in the history
In newer PHP versions (which this expands the test matrix to cover),
using `readonly` id properties is an option and, generally, a good one.
This adds tests to ensure it mimics the expected behavior.
  • Loading branch information
Firehed authored Jul 17, 2023
1 parent 32f886d commit 4a1fda1
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jobs:
php:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'

steps:
- name: Check out code
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": "^7.4 || ^8.0",
"doctrine/annotations": "^1.10",
"doctrine/collections": "^1.6",
"doctrine/collections": "^1.6.8",
"doctrine/orm": "^2.9",
"doctrine/persistence": "^1.3 || ^2.0",
"symfony/polyfill-php80": "^1.20"
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ parameters:
count: 1
path: tests/Entities/GrabBag.php

-
message: "#^Class Firehed\\\\Mocktrine\\\\Entities\\\\ReadonlyGeneratedId has an uninitialized readonly property \\$id\\. Assign it in the constructor\\.$#"
count: 1
path: tests/Entities/ReadonlyGeneratedId.php

-
message: "#^Property Firehed\\\\Mocktrine\\\\Entities\\\\StringId\\:\\:\\$id is never written, only read\\.$#"
count: 1
Expand Down
30 changes: 30 additions & 0 deletions tests/Entities/ReadonlyConstructorId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Firehed\Mocktrine\Entities;

use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="readonly_constructor_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'readonly_constructor_ids')]
class ReadonlyConstructorId
{
/**
* @Id
* @Column
*/
#[Mapping\Id]
#[Mapping\Column]
public readonly string $id;

public function __construct()
{
// Normally this would be a UUID, ULID, etc.
$this->id = bin2hex(random_bytes(10));
}
}
26 changes: 26 additions & 0 deletions tests/Entities/ReadonlyGeneratedId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Firehed\Mocktrine\Entities;

use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="readonly_generated_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'readonly_generated_ids')]
class ReadonlyGeneratedId
{
/**
* @Id
* @Column
* @GeneratedValue
*/
#[Mapping\Id]
#[Mapping\Column]
#[Mapping\GeneratedValue]
public readonly int $id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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\ReadonlyConstructorId" table="readonly_constructor_ids">

<id name="id" type="string" column="id" />

</entity>

</doctrine-mapping>
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\ReadonlyGeneratedId" table="readonly_generated_ids">

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

</entity>

</doctrine-mapping>
24 changes: 24 additions & 0 deletions tests/InMemoryEntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,28 @@ public function testInteractionWithMultipleEntities(): void
$foundStringId = $em->find(Entities\StringId::class, $stringId->getId());
$this->assertSame($foundStringId, $stringId);
}

public function testGeneratedReadonlyIdWorks(): void
{
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
$this->markTestSkipped('Readonly properties need 8.1+');
}
$rgid = new Entities\ReadonlyGeneratedId();
$em = $this->getEntityManager();
$em->persist($rgid);
$em->flush();
$this->assertIsInt($rgid->id);
}

public function testConstructorAssignedReadonlyIdWorks(): void
{
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
$this->markTestSkipped('Readonly properties need 8.1+');
}
$rgid = new Entities\ReadonlyConstructorId();
$em = $this->getEntityManager();
$em->persist($rgid);
$em->flush();
$this->assertIsString($rgid->id);
}
}

0 comments on commit 4a1fda1

Please sign in to comment.