Skip to content

Commit ac66363

Browse files
committed
bug #2 Making sure the object hashes are always clean (weaverryan)
This PR was merged into the main branch. Discussion ---------- Making sure the object hashes are always clean Commits ------- e636706 Making sure the object hashes are always clean
2 parents 258fd65 + e636706 commit ac66363

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/MicroMapper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public function map(object $from, string $toClass, array $context = []): object
7878
return $toObject;
7979
}
8080

81+
$this->objectHashes = [];
82+
$this->currentDepth = 0;
83+
$this->maxDepth = null;
84+
8185
throw new \Exception(sprintf('No mapper found for %s -> %s', $from::class, $toClass));
8286
}
8387
}

tests/MicroMapperTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222

2323
class MicroMapperTest extends TestCase
2424
{
25-
// calls correct mapper
26-
// respects MAX_DEPTH (and only calls init)
27-
// throws on circular reference
28-
2925
public function testMap()
3026
{
31-
$this->createMapper();
3227
$region = new DinoRegion();
3328
$region->id = 1;
3429
$region->name = 'North America';
@@ -40,7 +35,8 @@ public function testMap()
4035
$dinosaur2->region = $region;
4136
$region->dinosaurs = [$dinosaur1, $dinosaur2];
4237

43-
$dto = $this->createMapper()->map($region, DinoRegionDto::class);
38+
$mapper = $this->createMapper();
39+
$dto = $mapper->map($region, DinoRegionDto::class);
4440
$this->assertInstanceOf(DinoRegionDto::class, $dto);
4541
$this->assertSame(1, $dto->id);
4642
$this->assertSame('North America', $dto->name);
@@ -57,6 +53,10 @@ public function testMap()
5753
// the deep will have a region, but it will be shallow
5854
$this->assertSame($dto->dinosaursMappedDeep[0]->region->id, 1);
5955
$this->assertNull($dto->dinosaursMappedDeep[0]->region->name);
56+
57+
$reflectionObject = new \ReflectionObject($mapper);
58+
$objectHashesProperty = $reflectionObject->getProperty('objectHashes');
59+
$this->assertEmpty($objectHashesProperty->getValue($mapper));
6060
}
6161

6262
private function createMapper(): MicroMapperInterface

0 commit comments

Comments
 (0)