Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add ObjectCache tests #15

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading