Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Inheritance support #30

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions tests/Fixtures/Inheritance/Dto/AnimalDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Dto;

abstract class AnimalDto
{
public string $name;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Inheritance/Dto/CatDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Dto;

class CatDto extends AnimalDto
{
/** @var int<0,10> */
public int $meowLoudness;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Inheritance/Dto/DogDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Dto;

class DogDto extends AnimalDto
{
/** @var int<0,10> */
public int $barkLoudness;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Inheritance/Dto/FishDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Dto;

class FishDto extends AnimalDto
{
/** @var int<0,10> */
public int $swimSpeed;
}
10 changes: 10 additions & 0 deletions tests/Fixtures/Inheritance/Entity/Animal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Entity;

abstract class Animal
{
public string $name;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Inheritance/Entity/Cat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Entity;

class Cat extends Animal
{
/** @var int<0,10> */
public int $meowLoudness = 5;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Inheritance/Entity/Dog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Entity;

class Dog extends Animal
{
/** @var int<0,10> */
public int $barkLoudness = 5;
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Inheritance/Entity/Fish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\Inheritance\Entity;

class Fish extends Animal
{
/** @var int<0,10> */
public int $swimSpeed = 5;
}
Loading