From 7315a384382c8e5a7a3418501ce52a31a3af3387 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Mon, 17 Jul 2023 11:06:32 -0700 Subject: [PATCH 1/6] Add the models --- tests/Entities/ReadonlyConstructorId.php | 30 ++++++++++++++++++++++++ tests/Entities/ReadonlyGeneratedId.php | 26 ++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/Entities/ReadonlyConstructorId.php create mode 100644 tests/Entities/ReadonlyGeneratedId.php diff --git a/tests/Entities/ReadonlyConstructorId.php b/tests/Entities/ReadonlyConstructorId.php new file mode 100644 index 0000000..f45189a --- /dev/null +++ b/tests/Entities/ReadonlyConstructorId.php @@ -0,0 +1,30 @@ +id = bin2hex(random_bytes(10)); + } +} diff --git a/tests/Entities/ReadonlyGeneratedId.php b/tests/Entities/ReadonlyGeneratedId.php new file mode 100644 index 0000000..ca67bde --- /dev/null +++ b/tests/Entities/ReadonlyGeneratedId.php @@ -0,0 +1,26 @@ + Date: Mon, 17 Jul 2023 11:10:09 -0700 Subject: [PATCH 2/6] Add tests for readonly id entities --- tests/InMemoryEntityManagerTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/InMemoryEntityManagerTest.php b/tests/InMemoryEntityManagerTest.php index f5d7c4c..40f944a 100644 --- a/tests/InMemoryEntityManagerTest.php +++ b/tests/InMemoryEntityManagerTest.php @@ -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); + } } From 3d52a77ae6477cea92c5fca424ebba3158ceb3a9 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Mon, 17 Jul 2023 11:12:26 -0700 Subject: [PATCH 3/6] Add xml mappings --- ...cktrine.Entities.ReadonlyConstructorId.dcm.xml | 13 +++++++++++++ ...Mocktrine.Entities.ReadonlyGeneratedId.dcm.xml | 15 +++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyConstructorId.dcm.xml create mode 100644 tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyGeneratedId.dcm.xml diff --git a/tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyConstructorId.dcm.xml b/tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyConstructorId.dcm.xml new file mode 100644 index 0000000..fab31fc --- /dev/null +++ b/tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyConstructorId.dcm.xml @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyGeneratedId.dcm.xml b/tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyGeneratedId.dcm.xml new file mode 100644 index 0000000..95b2398 --- /dev/null +++ b/tests/Entities/XmlMappings/Firehed.Mocktrine.Entities.ReadonlyGeneratedId.dcm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + From 7084715224d741a9404d92674a1b69f5ff706a63 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Mon, 17 Jul 2023 11:13:01 -0700 Subject: [PATCH 4/6] Widen test matrix --- .github/workflows/test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8ed17d8..45de81d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,6 +22,9 @@ jobs: php: - '7.4' - '8.0' + - '8.1' + - '8.2' + - '8.3' steps: - name: Check out code From 632b3fb21120fed84460529677cbf8936d7fa165 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Mon, 17 Jul 2023 11:13:28 -0700 Subject: [PATCH 5/6] Ignore id warning --- phpstan-baseline.neon | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e5f3ee0..50ed9d2 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 From 6723f2baad0e5ef020164c90d530a211ee0df46d Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Mon, 17 Jul 2023 11:20:55 -0700 Subject: [PATCH 6/6] Reqire collections patch for php8.1+ --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 19db2bc..050fbaa 100644 --- a/composer.json +++ b/composer.json @@ -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"