From d3d11b4e2993fee792b01a1e6530abeceeffa144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 1 Jul 2023 17:33:15 +0200 Subject: [PATCH] TypeRegistry registry constructor must check for duplicate instances --- src/Types/TypeRegistry.php | 5 ++++- tests/Types/TypeRegistryTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Types/TypeRegistry.php b/src/Types/TypeRegistry.php index b5e800ad1b4..762cc3f8672 100644 --- a/src/Types/TypeRegistry.php +++ b/src/Types/TypeRegistry.php @@ -21,7 +21,10 @@ final class TypeRegistry /** @param array $instances */ public function __construct(array $instances = []) { - $this->instances = $instances; + $this->instances = []; + foreach ($instances as $name => $type) { + $this->register($name, $type); + } } /** diff --git a/tests/Types/TypeRegistryTest.php b/tests/Types/TypeRegistryTest.php index 42d57b69a56..0a5904252fe 100644 --- a/tests/Types/TypeRegistryTest.php +++ b/tests/Types/TypeRegistryTest.php @@ -99,6 +99,16 @@ public function testRegisterWithAlreadyRegisteredInstance(): void $this->registry->register('other', $newType); } + public function testConstructorWithDuplicateInstance(): void + { + $newType = new TextType(); + + new TypeRegistry(['a' => $newType]); + + $this->expectException(Exception::class); + new TypeRegistry(['a' => $newType, 'b' => $newType]); + } + public function testOverride(): void { $baseType = new TextType();