Skip to content

Commit

Permalink
test: add ObjectCache tests
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Feb 19, 2024
1 parent 7a6c521 commit 8c0cd89
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/IntegrationTest/ObjectCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\IntegrationTest;

use Rekalogika\Mapper\ObjectCache\Exception\CircularReferenceException;
use Rekalogika\Mapper\ObjectCache\ObjectCache;
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties;
use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithScalarPropertiesDto;
use Rekalogika\Mapper\TypeResolver\TypeResolverInterface;
use Rekalogika\Mapper\Util\TypeFactory;

class ObjectCacheTest extends FrameworkTestCase
{
private function createObjectCache(): ObjectCache
{
$typeResolver = $this->get('rekalogika.mapper.type_resolver');
$this->assertInstanceOf(TypeResolverInterface::class, $typeResolver);

return new ObjectCache($typeResolver);
}

public function testPreCache(): void
{
$objectCache = $this->createObjectCache();

$source = new ObjectWithScalarProperties();
$targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
$target = new ObjectWithScalarPropertiesDto();

$objectCache->preCache($source, $targetType);

$this->expectException(CircularReferenceException::class);
$objectCache->getTarget($source, $targetType);
}

public function testSaveGetTargetWithoutPreCache(): void
{
$objectCache = $this->createObjectCache();

$source = new ObjectWithScalarProperties();
$targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
$target = new ObjectWithScalarPropertiesDto();

$objectCache->saveTarget($source, $targetType, $target);

$this->assertSame($target, $objectCache->getTarget($source, $targetType));
}

public function testSaveGetTargetWithPreCache(): void
{
$objectCache = $this->createObjectCache();

$source = new ObjectWithScalarProperties();
$targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class);
$target = new ObjectWithScalarPropertiesDto();

$objectCache->preCache($source, $targetType);
$objectCache->saveTarget($source, $targetType, $target);

$this->assertSame($target, $objectCache->getTarget($source, $targetType));
}
}

0 comments on commit 8c0cd89

Please sign in to comment.