diff --git a/CHANGELOG.md b/CHANGELOG.md
index b30ef549..dc2621f6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@
* fix(`MainTransformer`): Make sure the target has the same type as the target
type.
* feat(`TraversableToArrayAccessTransformer`): Now supports `ArrayCollection` & `ArrayIterator`.
+* test: Assorted tests.
## 0.5.10
diff --git a/psalm.xml b/psalm.xml
index 3950d0f1..c0521895 100644
--- a/psalm.xml
+++ b/psalm.xml
@@ -31,5 +31,10 @@
+
+
+
+
+
diff --git a/tests/IntegrationTest/MappingTest.php b/tests/IntegrationTest/MappingTest.php
index c09ee667..569f3565 100644
--- a/tests/IntegrationTest/MappingTest.php
+++ b/tests/IntegrationTest/MappingTest.php
@@ -13,26 +13,289 @@
namespace Rekalogika\Mapper\Tests\IntegrationTest;
+use Rekalogika\Mapper\Attribute\InheritanceMap;
use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest;
+use Rekalogika\Mapper\Tests\Fixtures\EnumAndStringable\ObjectImplementingStringable;
+use Rekalogika\Mapper\Tests\Fixtures\EnumAndStringable\SomeBackedEnum;
+use Rekalogika\Mapper\Tests\Fixtures\EnumAndStringable\SomeEnum;
+use Rekalogika\Mapper\Transformer\Contracts\MixedType;
+use Rekalogika\Mapper\Transformer\CopyTransformer;
+use Rekalogika\Mapper\Transformer\DateTimeTransformer;
+use Rekalogika\Mapper\Transformer\InheritanceMapTransformer;
+use Rekalogika\Mapper\Transformer\ObjectToStringTransformer;
use Rekalogika\Mapper\Transformer\ScalarToScalarTransformer;
+use Rekalogika\Mapper\Transformer\StringToBackedEnumTransformer;
use Rekalogika\Mapper\Util\TypeFactory;
+use Symfony\Component\Clock\DatePoint;
+use Symfony\Component\PropertyInfo\Type;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
+use Rekalogika\Mapper\Transformer\TraversableToArrayAccessTransformer;
class MappingTest extends AbstractIntegrationTest
{
- public function testScalar(): void
- {
+ /**
+ * Testing mapping against default mapping table
+ * @dataProvider mappingTestProvider
+ * @param array $sources
+ * @param array $targets
+ * @param class-string $transformerClass
+ */
+ public function testMapping(
+ array $sources,
+ array $targets,
+ string $transformerClass
+ ): void {
$searchResult = $this->transformerRegistry->findBySourceAndTargetTypes(
- sourceTypes: [
- TypeFactory::int(),
- ],
- targetTypes: [
- TypeFactory::int(),
- ],
+ sourceTypes: $sources,
+ targetTypes: $targets,
);
+ $this->assertNotEmpty($searchResult);
+
$this->assertInstanceOf(
- ScalarToScalarTransformer::class,
+ $transformerClass,
$searchResult[0]?->getTransformer()
);
}
+
+ /**
+ * @return iterable>
+ */
+ public function mappingTestProvider(): iterable
+ {
+ //
+ // scalar
+ //
+
+ yield [
+ [
+ TypeFactory::int()
+ ],
+ [
+ TypeFactory::int()
+ ],
+ ScalarToScalarTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::int()
+ ],
+ [
+ TypeFactory::float()
+ ],
+ ScalarToScalarTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::int()
+ ],
+ [
+ TypeFactory::string()
+ ],
+ ScalarToScalarTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::int()
+ ],
+ [
+ TypeFactory::bool()
+ ],
+ ScalarToScalarTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::int()
+ ],
+ [
+ TypeFactory::resource()
+ ],
+ CopyTransformer::class,
+ ];
+
+ //
+ // datetime
+ //
+
+ yield [
+ [
+ TypeFactory::objectOfClass(\DateTimeInterface::class)
+ ],
+ [
+ TypeFactory::objectOfClass(\DateTimeImmutable::class)
+ ],
+ DateTimeTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(\DateTimeInterface::class)
+ ],
+ [
+ TypeFactory::objectOfClass(\DateTime::class)
+ ],
+ DateTimeTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(\DateTimeInterface::class)
+ ],
+ [
+ TypeFactory::objectOfClass(DatePoint::class)
+ ],
+ DateTimeTransformer::class,
+ ];
+
+ //
+ // stringable
+ //
+
+ yield [
+ [
+ TypeFactory::objectOfClass(ObjectImplementingStringable::class)
+ ],
+ [
+ TypeFactory::string()
+ ],
+ ObjectToStringTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(SomeBackedEnum::class)
+ ],
+ [
+ TypeFactory::string()
+ ],
+ ObjectToStringTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(SomeEnum::class)
+ ],
+ [
+ TypeFactory::string()
+ ],
+ ObjectToStringTransformer::class,
+ ];
+
+ //
+ // string to enum
+ //
+
+ yield [
+ [
+ TypeFactory::string()
+ ],
+ [
+ TypeFactory::objectOfClass(SomeBackedEnum::class)
+ ],
+ StringToBackedEnumTransformer::class,
+ ];
+
+ //
+ // inheritance
+ //
+
+ yield [
+ [
+ TypeFactory::object(),
+ ],
+ [
+ TypeFactory::objectOfClass(InheritanceMap::class)
+ ],
+ InheritanceMapTransformer::class,
+ ];
+
+ //
+ // traversable to array access
+ //
+
+ yield [
+ [
+ TypeFactory::array(),
+ ],
+ [
+ TypeFactory::array(),
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::array(),
+ ],
+ [
+ TypeFactory::objectOfClass(\ArrayAccess::class)
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(\ArrayObject::class),
+ ],
+ [
+ TypeFactory::objectOfClass(\ArrayAccess::class)
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(\ArrayObject::class),
+ ],
+ [
+ TypeFactory::objectOfClass(\ArrayObject::class)
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(\ArrayObject::class),
+ ],
+ [
+ TypeFactory::objectOfClass(ArrayCollection::class)
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(ArrayCollection::class),
+ ],
+ [
+ TypeFactory::objectOfClass(Collection::class)
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(Collection::class),
+ ],
+ [
+ TypeFactory::objectOfClass(\ArrayAccess::class)
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+
+ yield [
+ [
+ TypeFactory::objectOfClass(Collection::class),
+ ],
+ [
+ TypeFactory::objectOfClass(\ArrayObject::class)
+ ],
+ TraversableToArrayAccessTransformer::class,
+ ];
+ }
}
diff --git a/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php b/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php
index fcba2acb..6c1fcef9 100644
--- a/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php
+++ b/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php
@@ -13,6 +13,7 @@
namespace Rekalogika\Mapper\Tests\IntegrationTest;
+use ArrayObject;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest;
@@ -244,4 +245,27 @@ public function testSourceStringKeyToTargetIntKey(): void
$this->assertArrayHasKey(1, $result->property);
$this->assertArrayHasKey(2, $result->property);
}
+
+ //
+ // target existing object
+ //
+
+ public function testTraversableToExistingArrayAccessDto(): void
+ {
+ $source = new ObjectWithTraversableProperties();
+ $target = new ObjectWithArrayAccessPropertyDto();
+
+ /** @var \ArrayObject */
+ $arrayObject = new \ArrayObject();
+ $target->property = $arrayObject;
+
+ $result = $this->mapper->map($source, $target);
+
+ $this->assertInstanceOf(ObjectWithArrayAccessPropertyDto::class, $result);
+ $this->assertInstanceOf(\ArrayAccess::class, $result->property);
+ $this->assertEquals(1, $result->property[1]?->a);
+ $this->assertEquals("string", $result->property[1]?->b);
+ $this->assertEquals(true, $result->property[1]?->c);
+ $this->assertEquals(1.1, $result->property[1]?->d);
+ }
}