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

Test interactions with readonly ids #38

Merged
merged 6 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
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);
}
}
Loading