From e63670643136c80e2db0590e28a87b36770fdc27 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Mon, 28 Aug 2023 15:51:30 -0400 Subject: [PATCH] Making sure the object hashes are always clean --- src/MicroMapper.php | 4 ++++ tests/MicroMapperTest.php | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/MicroMapper.php b/src/MicroMapper.php index c051319..d184558 100644 --- a/src/MicroMapper.php +++ b/src/MicroMapper.php @@ -78,6 +78,10 @@ public function map(object $from, string $toClass, array $context = []): object return $toObject; } + $this->objectHashes = []; + $this->currentDepth = 0; + $this->maxDepth = null; + throw new \Exception(sprintf('No mapper found for %s -> %s', $from::class, $toClass)); } } diff --git a/tests/MicroMapperTest.php b/tests/MicroMapperTest.php index 69cc396..adb1e7d 100644 --- a/tests/MicroMapperTest.php +++ b/tests/MicroMapperTest.php @@ -22,13 +22,8 @@ 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'; @@ -40,7 +35,8 @@ public function testMap() $dinosaur2->region = $region; $region->dinosaurs = [$dinosaur1, $dinosaur2]; - $dto = $this->createMapper()->map($region, DinoRegionDto::class); + $mapper = $this->createMapper(); + $dto = $mapper->map($region, DinoRegionDto::class); $this->assertInstanceOf(DinoRegionDto::class, $dto); $this->assertSame(1, $dto->id); $this->assertSame('North America', $dto->name); @@ -57,6 +53,10 @@ public function testMap() // 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); + + $reflectionObject = new \ReflectionObject($mapper); + $objectHashesProperty = $reflectionObject->getProperty('objectHashes'); + $this->assertEmpty($objectHashesProperty->getValue($mapper)); } private function createMapper(): MicroMapperInterface