From 6664f7e319610cac7492efaa533e38e8d00574ed Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 21 Jan 2024 11:50:23 +0100 Subject: [PATCH] Make impossible to merge two schema definitions that are pointing to a different entry --- src/core/etl/src/Flow/ETL/Row/Schema/Definition.php | 5 +++++ .../Flow/ETL/Tests/Unit/Row/Schema/DefinitionTest.php | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php b/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php index ccc57aa39..74e9cb8d8 100644 --- a/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php +++ b/src/core/etl/src/Flow/ETL/Row/Schema/Definition.php @@ -17,6 +17,7 @@ use function Flow\ETL\DSL\type_xml; use function Flow\ETL\DSL\type_xml_node; use Flow\ETL\Exception\InvalidArgumentException; +use Flow\ETL\Exception\RuntimeException; use Flow\ETL\PHP\Type\Logical\ListType; use Flow\ETL\PHP\Type\Logical\MapType; use Flow\ETL\PHP\Type\Logical\StructureType; @@ -239,6 +240,10 @@ public function matches(Entry $entry) : bool public function merge(self $definition) : self { + if (!$this->ref->is($definition->ref)) { + throw new RuntimeException(\sprintf('Cannot merge different definitions, %s and %s', $this->ref->name(), $definition->ref->name())); + } + $constraint = new Any($this->constraint, $definition->constraint); if ($this->constraint instanceof VoidConstraint) { diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Schema/DefinitionTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Schema/DefinitionTest.php index 3f4cc9125..aaa285be4 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Schema/DefinitionTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Schema/DefinitionTest.php @@ -17,6 +17,7 @@ use function Flow\ETL\DSL\type_map; use function Flow\ETL\DSL\type_string; use Flow\ETL\Exception\InvalidArgumentException; +use Flow\ETL\Exception\RuntimeException; use Flow\ETL\PHP\Type\Logical\List\ListElement; use Flow\ETL\PHP\Type\Logical\ListType; use Flow\ETL\PHP\Type\Logical\StructureType; @@ -170,6 +171,14 @@ public function test_merging_anything_with_null() : void ); } + public function test_merging_different_entries() : void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Cannot merge different definitions, int and string'); + + Definition::integer('int')->merge(Definition::string('string')); + } + public function test_merging_numeric_types() : void { $this->assertEquals(