-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21d3a18
commit 7c3f666
Showing
14 changed files
with
384 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfonycasts\MicroMapper\MicroMapperInterface; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinoRegion; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinoRegionDto; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\Dinosaur; | ||
|
||
class IntegrationTest extends KernelTestCase | ||
{ | ||
public function testBundleIntegration() | ||
{ | ||
$region = new DinoRegion(); | ||
$region->id = 1; | ||
$region->name = 'North America'; | ||
$region->climate = 'temperate'; | ||
$dinosaur1 = new Dinosaur(3, 'Velociraptor', 'mongoliensis'); | ||
$dinosaur1->region = $region; | ||
$region->dinosaurs = [$dinosaur1]; | ||
|
||
$microMapper = self::getContainer()->get('public.micro_mapper'); | ||
assert($microMapper instanceof MicroMapperInterface); | ||
$dto = $microMapper->map($region, DinoRegionDto::class); | ||
$this->assertInstanceOf(DinoRegionDto::class, $dto); | ||
$this->assertSame(1, $dto->id); | ||
$this->assertCount(1, $dto->dinosaursMappedShallow); | ||
$this->assertSame(3, $dto->dinosaursMappedShallow[0]->id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfonycasts\MicroMapper\MapperConfig; | ||
use Symfonycasts\MicroMapper\MapperInterface; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinoRegion; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinoRegionDto; | ||
|
||
class MapperConfigTest extends TestCase | ||
{ | ||
public function testSupports() | ||
{ | ||
$config = new MapperConfig( | ||
fromClass: DinoRegion::class, | ||
toClass: DinoRegionDto::class, | ||
mapper: fn() => $this->createMock(MapperInterface::class), | ||
); | ||
|
||
$this->assertTrue($config->supports(new DinoRegion(), DinoRegionDto::class)); | ||
$this->assertFalse($config->supports(new \stdClass(), DinoRegionDto::class)); | ||
$this->assertFalse($config->supports(new DinoRegion(), \stdClass::class)); | ||
$this->assertFalse($config->supports(new DinoRegionDto(), DinoRegion::class)); | ||
} | ||
|
||
public function testGetMapper() | ||
{ | ||
$mockMapper = $this->createMock(MapperInterface::class); | ||
$config = new MapperConfig( | ||
fromClass: DinoRegion::class, | ||
toClass: DinoRegionDto::class, | ||
mapper: fn() => $mockMapper, | ||
); | ||
|
||
$this->assertSame($mockMapper, $config->getMapper()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfonycasts\MicroMapper\MapperConfig; | ||
use Symfonycasts\MicroMapper\MicroMapper; | ||
use Symfonycasts\MicroMapper\MicroMapperInterface; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinoRegion; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinoRegionDto; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinoRegionToDtoMapper; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\Dinosaur; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto; | ||
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurToDtoMapper; | ||
|
||
class MicroMapperTest extends TestCase | ||
{ | ||
// calls correct mapper | ||
// respects MAX_DEPTH (and only calls init) | ||
// throws on circular reference | ||
|
||
public function testMap() | ||
{ | ||
$this->createMapper(); | ||
$region = new DinoRegion(); | ||
$region->id = 1; | ||
$region->name = 'North America'; | ||
$region->climate = 'temperate'; | ||
|
||
$dinosaur1 = new Dinosaur(3, 'Velociraptor', 'mongoliensis'); | ||
$dinosaur1->region = $region; | ||
$dinosaur2 = new Dinosaur(4, 'Triceratops', 'horridus'); | ||
$dinosaur2->region = $region; | ||
$region->dinosaurs = [$dinosaur1, $dinosaur2]; | ||
|
||
$dto = $this->createMapper()->map($region, DinoRegionDto::class); | ||
$this->assertInstanceOf(DinoRegionDto::class, $dto); | ||
$this->assertSame(1, $dto->id); | ||
$this->assertSame('North America', $dto->name); | ||
$this->assertSame('temperate', $dto->climate); | ||
$this->assertCount(2, $dto->dinosaursMappedShallow); | ||
$this->assertCount(2, $dto->dinosaursMappedDeep); | ||
|
||
// id is mapped for both deep and shallow | ||
$this->assertSame(3, $dto->dinosaursMappedShallow[0]->id); | ||
$this->assertSame(3, $dto->dinosaursMappedDeep[0]->id); | ||
// further properties are only in the deep | ||
$this->assertNull($dto->dinosaursMappedShallow[0]->genus); | ||
$this->assertSame('Velociraptor', $dto->dinosaursMappedDeep[0]->genus); | ||
// the deep will have a region, but it will be shallow | ||
$this->assertSame($dto->dinosaursMappedDeep[0]->region->id, 1); | ||
$this->assertNull($dto->dinosaursMappedDeep[0]->region->name); | ||
} | ||
|
||
private function createMapper(): MicroMapperInterface | ||
{ | ||
$microMapper = new MicroMapper([]); | ||
$microMapper->addMapperConfig(new MapperConfig( | ||
DinoRegion::class, | ||
DinoRegionDto::class, | ||
fn() => new DinoRegionToDtoMapper($microMapper) | ||
)); | ||
$microMapper->addMapperConfig(new MapperConfig( | ||
Dinosaur::class, | ||
DinosaurDto::class, | ||
fn() => new DinosaurToDtoMapper($microMapper) | ||
)); | ||
|
||
return $microMapper; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests\fixtures; | ||
|
||
class DinoRegion | ||
{ | ||
public ?int $id = null; | ||
public ?string $name = null; | ||
public ?string $climate = null; | ||
public array $dinosaurs = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests\fixtures; | ||
|
||
class DinoRegionDto | ||
{ | ||
public ?int $id = null; | ||
public ?string $name = null; | ||
public ?string $climate = null; | ||
/** | ||
* @var array DinosaurDto[] | ||
*/ | ||
public array $dinosaursMappedShallow = []; | ||
/** | ||
* @var array DinosaurDto[] | ||
*/ | ||
public array $dinosaursMappedDeep = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests\fixtures; | ||
|
||
use Symfonycasts\MicroMapper\AsMapper; | ||
use Symfonycasts\MicroMapper\MapperInterface; | ||
use Symfonycasts\MicroMapper\MicroMapperInterface; | ||
|
||
#[AsMapper(from: DinoRegion::class, to: DinoRegionDto::class)] | ||
class DinoRegionToDtoMapper implements MapperInterface | ||
{ | ||
public function __construct(private MicroMapperInterface $microMapper) | ||
{ | ||
|
||
} | ||
|
||
public function init(object $from, string $toClass, array $context): object | ||
{ | ||
$dto = new $toClass(); | ||
$dto->id = $from->id; | ||
|
||
return $dto; | ||
} | ||
|
||
public function populate(object $from, object $to, array $context): object | ||
{ | ||
assert($from instanceof DinoRegion); | ||
assert($to instanceof DinoRegionDto); | ||
|
||
$to->name = $from->name; | ||
$to->climate = $from->climate; | ||
$shallowDinosaurDtos = []; | ||
foreach ($from->dinosaurs as $dino) { | ||
$shallowDinosaurDtos[] = $this->microMapper->map($dino, DinosaurDto::class, [ | ||
MicroMapperInterface::MAX_DEPTH => 0, | ||
]); | ||
} | ||
$to->dinosaursMappedShallow = $shallowDinosaurDtos; | ||
|
||
$deepDinosaurDtos = []; | ||
foreach ($from->dinosaurs as $dino) { | ||
$deepDinosaurDtos[] = $this->microMapper->map($dino, DinosaurDto::class, [ | ||
MicroMapperInterface::MAX_DEPTH => 1, | ||
]); | ||
} | ||
$to->dinosaursMappedDeep = $deepDinosaurDtos; | ||
|
||
return $to; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests\fixtures; | ||
|
||
class Dinosaur | ||
{ | ||
public function __construct( | ||
public ?int $id = null, | ||
public ?string $genus = null, | ||
public ?string $species = null, | ||
public ?DinoRegion $region = null, | ||
) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests\fixtures; | ||
|
||
class DinosaurDto | ||
{ | ||
public ?int $id = null; | ||
public ?string $genus = null; | ||
public ?string $species = null; | ||
public ?DinoRegionDto $region = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Symfonycasts\MicroMapper\Tests\fixtures; | ||
|
||
use Symfonycasts\MicroMapper\AsMapper; | ||
use Symfonycasts\MicroMapper\MapperInterface; | ||
use Symfonycasts\MicroMapper\MicroMapperInterface; | ||
|
||
#[AsMapper(from: Dinosaur::class, to: DinosaurDto::class)] | ||
class DinosaurToDtoMapper implements MapperInterface | ||
{ | ||
public function __construct(private MicroMapperInterface $microMapper) | ||
{ | ||
|
||
} | ||
|
||
public function init(object $from, string $toClass, array $context): object | ||
{ | ||
$dto = new $toClass(); | ||
$dto->id = $from->id; | ||
|
||
return $dto; | ||
} | ||
|
||
public function populate(object $from, object $to, array $context): object | ||
{ | ||
assert($from instanceof Dinosaur); | ||
assert($to instanceof DinosaurDto); | ||
|
||
$to->genus = $from->genus; | ||
$to->species = $from->species; | ||
$to->region = $this->microMapper->map($from->region, DinoRegionDto::class, [ | ||
MicroMapperInterface::MAX_DEPTH => 0, | ||
]); | ||
|
||
return $to; | ||
} | ||
} |
Oops, something went wrong.