From 83ed9ef96de4fbeba6ba7c215f81cc8dd7d8e7a7 Mon Sep 17 00:00:00 2001 From: Andrej Rypo Date: Sun, 4 Feb 2024 20:52:27 +0100 Subject: [PATCH] Node child key collision test --- tests/nodes.phpt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/nodes.phpt b/tests/nodes.phpt index e08a46e..87b0acc 100644 --- a/tests/nodes.phpt +++ b/tests/nodes.phpt @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Dakujem\Test; use Dakujem\Oliva\Exceptions\AcceptsDebugContext; +use Dakujem\Oliva\Exceptions\ChildKeyCollision; use Dakujem\Oliva\Exceptions\Context; use Dakujem\Oliva\Exceptions\InvalidInputData; use Dakujem\Oliva\Exceptions\InvalidNodeFactoryReturnValue; @@ -284,3 +285,20 @@ require_once __DIR__ . '/setup.php'; }, InvalidNodeFactoryReturnValue::class, 'The node factory must return a movable node instance (Dakujem\Oliva\MovableNodeContract).'); })(); + +// A node does not allow colliding child keys +(function () { + $node = new Node(null); + $node->addChild(new Node(null), 'key'); + Assert::throws(function () use ($node) { + $node->addChild(new Node(null), 'key'); + }, ChildKeyCollision::class, 'Collision not allowed: key'); + + // The current implementation does not allow this even when the same node is being added with the same key. + // This is intentional, for simplicity. Tree::link covers this cases without hassle. + $child = new Node(null); + $node->addChild($child, 'another'); + Assert::throws(function () use ($node, $child) { + $node->addChild($child, 'another'); + }, ChildKeyCollision::class, 'Collision not allowed: another'); +})();