Skip to content

Commit

Permalink
Add test for invalid hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
olsavmic committed Sep 9, 2024
1 parent 25ba92c commit 22de13c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/Compiler/Mapper/Object/MapDiscriminatedObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ShipMonkTests\InputMapper\Compiler\Mapper\Object;

use ShipMonk\InputMapper\Compiler\Exception\CannotCompileMapperException;
use ShipMonk\InputMapper\Compiler\Mapper\MapperCompiler;
use ShipMonk\InputMapper\Compiler\Mapper\Object\DelegateMapperCompiler;
use ShipMonk\InputMapper\Compiler\Mapper\Object\MapDiscriminatedObject;
Expand All @@ -11,6 +12,7 @@
use ShipMonk\InputMapper\Compiler\Mapper\Scalar\MapString;
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\MapOptional;
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
use ShipMonk\InputMapper\Runtime\Mapper;
use ShipMonk\InputMapper\Runtime\Optional;
use ShipMonkTests\InputMapper\Compiler\Mapper\MapperCompilerTestCase;
use ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data\HierarchicalChildOneInput;
Expand All @@ -19,6 +21,7 @@
use ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data\HierarchicalWithEnumChildInput;
use ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data\HierarchicalWithEnumParentInput;
use ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data\HierarchicalWithEnumType;
use ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data\MovieInput;

class MapDiscriminatedObjectTest extends MapperCompilerTestCase
{
Expand Down Expand Up @@ -137,6 +140,24 @@ public function testCompileWithEnumAsType(): void
);
}

public function testCompileWithSubtypesFromDifferentHierarchies(): void
{
$mapperCompiler = new MapDiscriminatedObject(
HierarchicalParentInput::class,
'type',
[
'childOne' => new DelegateMapperCompiler(HierarchicalChildOneInput::class),
'childTwo' => new DelegateMapperCompiler(MovieInput::class),
],
);

self::assertException(
CannotCompileMapperException::class,
'Cannot compile mapper ShipMonk\InputMapper\Compiler\Mapper\Object\DelegateMapperCompiler as subtype (#[Discriminator]) mapper, because its output type \'ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data\MovieInput\' is not super type of \'ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data\HierarchicalParentInput\'',
fn(): Mapper => $this->compileMapper('InvalidHierarchyMapper', $mapperCompiler),
);
}

private function createParentInputMapperCompiler(): MapperCompiler
{
return new MapDiscriminatedObject(
Expand Down

0 comments on commit 22de13c

Please sign in to comment.