Skip to content

Commit

Permalink
TypeRegistry registry constructor must check for duplicate instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 1, 2023
1 parent 90469f5 commit d3d11b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Types/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ final class TypeRegistry
/** @param array<string, Type> $instances */
public function __construct(array $instances = [])
{
$this->instances = $instances;
$this->instances = [];
foreach ($instances as $name => $type) {
$this->register($name, $type);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Types/TypeRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d3d11b4

Please sign in to comment.