From 2b2a77f0f7f69899cfc25d06a6fb554767b7b053 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo Date: Fri, 5 Jan 2024 05:13:14 +0700 Subject: [PATCH] Tests for inheritance --- tests/AutoMapperTest.php | 30 ++++++++++++++++++++ tests/Fixtures/Inheritance/Dto/AnimalDto.php | 10 +++++++ tests/Fixtures/Inheritance/Dto/CatDto.php | 11 +++++++ tests/Fixtures/Inheritance/Dto/DogDto.php | 11 +++++++ tests/Fixtures/Inheritance/Dto/FishDto.php | 11 +++++++ tests/Fixtures/Inheritance/Entity/Animal.php | 10 +++++++ tests/Fixtures/Inheritance/Entity/Cat.php | 11 +++++++ tests/Fixtures/Inheritance/Entity/Dog.php | 11 +++++++ tests/Fixtures/Inheritance/Entity/Fish.php | 11 +++++++ 9 files changed, 116 insertions(+) create mode 100644 tests/Fixtures/Inheritance/Dto/AnimalDto.php create mode 100644 tests/Fixtures/Inheritance/Dto/CatDto.php create mode 100644 tests/Fixtures/Inheritance/Dto/DogDto.php create mode 100644 tests/Fixtures/Inheritance/Dto/FishDto.php create mode 100644 tests/Fixtures/Inheritance/Entity/Animal.php create mode 100644 tests/Fixtures/Inheritance/Entity/Cat.php create mode 100644 tests/Fixtures/Inheritance/Entity/Dog.php create mode 100644 tests/Fixtures/Inheritance/Entity/Fish.php diff --git a/tests/AutoMapperTest.php b/tests/AutoMapperTest.php index b6c27665..82fd71a9 100644 --- a/tests/AutoMapperTest.php +++ b/tests/AutoMapperTest.php @@ -27,6 +27,11 @@ use AutoMapper\Tests\Fixtures\HasDateTimeInterfaceWithMutableInstance; use AutoMapper\Tests\Fixtures\HasDateTimeInterfaceWithNullValue; use AutoMapper\Tests\Fixtures\HasDateTimeWithNullValue; +use AutoMapper\Tests\Fixtures\Inheritance\Dto\AnimalDto; +use AutoMapper\Tests\Fixtures\Inheritance\Dto\CatDto; +use AutoMapper\Tests\Fixtures\Inheritance\Dto\DogDto; +use AutoMapper\Tests\Fixtures\Inheritance\Dto\FishDto; +use AutoMapper\Tests\Fixtures\Inheritance\Entity; use AutoMapper\Tests\Fixtures\ObjectWithDateTime; use AutoMapper\Tests\Fixtures\Order; use AutoMapper\Tests\Fixtures\PetOwner; @@ -1266,4 +1271,29 @@ public function testItCanMapFromArrayWithMissingNullableProperty(): void $this->autoMapper->map(['foo' => 1], ClassWithNullablePropertyInConstructor::class) ); } + + public function testInheritance(): void + { + $this->buildAutoMapper(); + + $dog = new Entity\Dog(); + $cat = new Entity\Cat(); + $fish = new Entity\Fish(); + + $dogDto = $this->autoMapper->map($dog, AnimalDto::class); + $catDto = $this->autoMapper->map($cat, AnimalDto::class); + $fishDto = $this->autoMapper->map($fish, AnimalDto::class); + + self::assertInstanceOf(DogDto::class, $dogDto); + self::assertInstanceOf(CatDto::class, $catDto); + self::assertInstanceOf(FishDto::class, $fishDto); + + $dog = $this->autoMapper->map($dogDto, Entity\Animal::class); + $cat = $this->autoMapper->map($catDto, Entity\Animal::class); + $fish = $this->autoMapper->map($fishDto, Entity\Animal::class); + + self::assertInstanceOf(Entity\Dog::class, $dog); + self::assertInstanceOf(Entity\Cat::class, $cat); + self::assertInstanceOf(Entity\Fish::class, $fish); + } } diff --git a/tests/Fixtures/Inheritance/Dto/AnimalDto.php b/tests/Fixtures/Inheritance/Dto/AnimalDto.php new file mode 100644 index 00000000..120368f3 --- /dev/null +++ b/tests/Fixtures/Inheritance/Dto/AnimalDto.php @@ -0,0 +1,10 @@ + */ + public int $meowLoudness; +} diff --git a/tests/Fixtures/Inheritance/Dto/DogDto.php b/tests/Fixtures/Inheritance/Dto/DogDto.php new file mode 100644 index 00000000..8a94373a --- /dev/null +++ b/tests/Fixtures/Inheritance/Dto/DogDto.php @@ -0,0 +1,11 @@ + */ + public int $barkLoudness; +} diff --git a/tests/Fixtures/Inheritance/Dto/FishDto.php b/tests/Fixtures/Inheritance/Dto/FishDto.php new file mode 100644 index 00000000..d2cae9fc --- /dev/null +++ b/tests/Fixtures/Inheritance/Dto/FishDto.php @@ -0,0 +1,11 @@ + */ + public int $swimSpeed; +} diff --git a/tests/Fixtures/Inheritance/Entity/Animal.php b/tests/Fixtures/Inheritance/Entity/Animal.php new file mode 100644 index 00000000..a390cd71 --- /dev/null +++ b/tests/Fixtures/Inheritance/Entity/Animal.php @@ -0,0 +1,10 @@ + */ + public int $meowLoudness = 5; +} diff --git a/tests/Fixtures/Inheritance/Entity/Dog.php b/tests/Fixtures/Inheritance/Entity/Dog.php new file mode 100644 index 00000000..b9835bcd --- /dev/null +++ b/tests/Fixtures/Inheritance/Entity/Dog.php @@ -0,0 +1,11 @@ + */ + public int $barkLoudness = 5; +} diff --git a/tests/Fixtures/Inheritance/Entity/Fish.php b/tests/Fixtures/Inheritance/Entity/Fish.php new file mode 100644 index 00000000..83e808e5 --- /dev/null +++ b/tests/Fixtures/Inheritance/Entity/Fish.php @@ -0,0 +1,11 @@ + */ + public int $swimSpeed = 5; +}