|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of rekalogika/mapper package. |
| 7 | + * |
| 8 | + * (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE file |
| 11 | + * that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Rekalogika\Mapper\Tests\IntegrationTest; |
| 15 | + |
| 16 | +use Rekalogika\Mapper\ObjectCache\Exception\CircularReferenceException; |
| 17 | +use Rekalogika\Mapper\ObjectCache\ObjectCache; |
| 18 | +use Rekalogika\Mapper\Tests\Common\FrameworkTestCase; |
| 19 | +use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties; |
| 20 | +use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithScalarPropertiesDto; |
| 21 | +use Rekalogika\Mapper\TypeResolver\TypeResolverInterface; |
| 22 | +use Rekalogika\Mapper\Util\TypeFactory; |
| 23 | + |
| 24 | +class ObjectCacheTest extends FrameworkTestCase |
| 25 | +{ |
| 26 | + private function createObjectCache(): ObjectCache |
| 27 | + { |
| 28 | + $typeResolver = $this->get('rekalogika.mapper.type_resolver'); |
| 29 | + $this->assertInstanceOf(TypeResolverInterface::class, $typeResolver); |
| 30 | + |
| 31 | + return new ObjectCache($typeResolver); |
| 32 | + } |
| 33 | + |
| 34 | + public function testPreCache(): void |
| 35 | + { |
| 36 | + $objectCache = $this->createObjectCache(); |
| 37 | + |
| 38 | + $source = new ObjectWithScalarProperties(); |
| 39 | + $targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class); |
| 40 | + $target = new ObjectWithScalarPropertiesDto(); |
| 41 | + |
| 42 | + $objectCache->preCache($source, $targetType); |
| 43 | + |
| 44 | + $this->expectException(CircularReferenceException::class); |
| 45 | + $objectCache->getTarget($source, $targetType); |
| 46 | + } |
| 47 | + |
| 48 | + public function testSaveGetTargetWithoutPreCache(): void |
| 49 | + { |
| 50 | + $objectCache = $this->createObjectCache(); |
| 51 | + |
| 52 | + $source = new ObjectWithScalarProperties(); |
| 53 | + $targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class); |
| 54 | + $target = new ObjectWithScalarPropertiesDto(); |
| 55 | + |
| 56 | + $objectCache->saveTarget($source, $targetType, $target); |
| 57 | + |
| 58 | + $this->assertSame($target, $objectCache->getTarget($source, $targetType)); |
| 59 | + } |
| 60 | + |
| 61 | + public function testSaveGetTargetWithPreCache(): void |
| 62 | + { |
| 63 | + $objectCache = $this->createObjectCache(); |
| 64 | + |
| 65 | + $source = new ObjectWithScalarProperties(); |
| 66 | + $targetType = TypeFactory::objectOfClass(ObjectWithScalarProperties::class); |
| 67 | + $target = new ObjectWithScalarPropertiesDto(); |
| 68 | + |
| 69 | + $objectCache->preCache($source, $targetType); |
| 70 | + $objectCache->saveTarget($source, $targetType, $target); |
| 71 | + |
| 72 | + $this->assertSame($target, $objectCache->getTarget($source, $targetType)); |
| 73 | + } |
| 74 | +} |
0 commit comments