Skip to content

Commit

Permalink
bug #2 Making sure the object hashes are always clean (weaverryan)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
weaverryan committed Aug 28, 2023
2 parents 258fd65 + e636706 commit ac66363
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/MicroMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
12 changes: 6 additions & 6 deletions tests/MicroMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit ac66363

Please sign in to comment.