Skip to content

Commit

Permalink
st: Assorted tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 15, 2024
1 parent 45f7431 commit f57a94e
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@
<file name="src/Transformer/TraversableToArrayAccessTransformer.php" />
</errorLevel>
</MixedAssignment>
<PossiblyInvalidArgument>
<errorLevel type="suppress">
<file name="tests/IntegrationTest/MappingTest.php" />
</errorLevel>
</PossiblyInvalidArgument>
</issueHandlers>
</psalm>
281 changes: 272 additions & 9 deletions tests/IntegrationTest/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,Type|MixedType> $sources
* @param array<int,Type|MixedType> $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<array-key,array<array-key,mixed>>
*/
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,
];
}
}
24 changes: 24 additions & 0 deletions tests/IntegrationTest/TraversableToArrayAccessMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int,ObjectWithScalarPropertiesDto> */
$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);
}
}

0 comments on commit f57a94e

Please sign in to comment.