Valinor/tests/Integration/Mapping/Object/IterableValuesMappingTest.php
Romain Canon b2e810e3ce feat!: allow mapping to any type
Previously, the method `TreeMapper::map` would allow mapping only to an
object. It is now possible to map to any type handled by the library.

It is for instance possible to map to an array of objects:

```php
$objects = (new \CuyZ\Valinor\MapperBuilder())->mapper()->map(
    'array<' . SomeClass::class . '>',
    [/* … */]
);
```

For simple use-cases, an array shape can be used:

```php
$array = (new \CuyZ\Valinor\MapperBuilder())->mapper()->map(
    'array{foo: string, bar: int}',
    [/* … */]
);

echo strtolower($array['foo']);
echo $array['bar'] * 2;
```

This new feature changes the possible behaviour of the mapper, meaning
static analysis tools need help to understand the types correctly. An
extension for PHPStan and a plugin for Psalm are now provided and can be
included in a project to automatically increase the type coverage.
2022-01-02 00:48:01 +01:00

131 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject;
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject as SimpleObjectAlias;
final class IterableValuesMappingTest extends IntegrationTest
{
public function test_values_are_mapped_properly(): void
{
$source = [
'booleans' => [true, false, true],
'floats' => [42.404, 404.42],
'integers' => [42, 404, 1337],
'strings' => ['foo', 'bar', 'baz'],
'iterableWithDefaultKeyType' => [42 => 'foo', 'some-key' => 'bar'],
'iterableWithIntegerKeyType' => [1337 => 'foo', 42.0 => 'bar', '404' => 'baz'],
'iterableWithStringKeyType' => [1337 => 'foo', 42.0 => 'bar', 'some-key' => 'baz'],
'objects' => [
'foo' => ['value' => 'foo'],
'bar' => ['value' => 'bar'],
'baz' => ['value' => 'baz'],
],
'objectsWithAlias' => [
'foo' => ['value' => 'foo'],
'bar' => ['value' => 'bar'],
'baz' => ['value' => 'baz'],
],
];
foreach ([IterableValues::class, IterableValuesWithConstructor::class] as $class) {
try {
$result = $this->mapperBuilder->mapper()->map($class, $source);
} catch (MappingError $error) {
$this->mappingFail($error);
}
/** @var SimpleObject[] $objects */
$objects = $result->objects;
/** @var SimpleObjectAlias[] $objectsWithAlias */
$objectsWithAlias = $result->objectsWithAlias;
self::assertSame($source['booleans'], $result->booleans);
self::assertSame($source['floats'], $result->floats);
self::assertSame($source['integers'], $result->integers);
self::assertSame($source['strings'], $result->strings);
self::assertSame($source['iterableWithDefaultKeyType'], $result->iterableWithDefaultKeyType);
self::assertSame($source['iterableWithIntegerKeyType'], $result->iterableWithIntegerKeyType);
self::assertSame($source['iterableWithStringKeyType'], $result->iterableWithStringKeyType);
self::assertSame('foo', $objects['foo']->value);
self::assertSame('bar', $objects['bar']->value);
self::assertSame('baz', $objects['baz']->value);
self::assertSame('foo', $objectsWithAlias['foo']->value);
self::assertSame('bar', $objectsWithAlias['bar']->value);
self::assertSame('baz', $objectsWithAlias['baz']->value);
}
}
}
class IterableValues
{
/** @var iterable<bool> */
public iterable $booleans;
/** @var iterable<float> */
public iterable $floats;
/** @var iterable<int> */
public iterable $integers;
/** @var iterable<string> */
public iterable $strings;
/** @var iterable<array-key, string> */
public iterable $iterableWithDefaultKeyType;
/** @var iterable<int, string> */
public iterable $iterableWithIntegerKeyType;
/** @var iterable<string, string> */
public iterable $iterableWithStringKeyType;
/** @var iterable<SimpleObject> */
public iterable $objects;
/** @var iterable<SimpleObjectAlias> */
public iterable $objectsWithAlias;
}
class IterableValuesWithConstructor extends IterableValues
{
/**
* @param iterable<bool> $booleans
* @param iterable<float> $floats
* @param iterable<int> $integers
* @param iterable<string> $strings
* @param iterable<array-key, string> $iterableWithDefaultKeyType
* @param iterable<int, string> $iterableWithIntegerKeyType
* @param iterable<string, string> $iterableWithStringKeyType
* @param iterable<SimpleObject> $objects
* @param iterable<SimpleObjectAlias> $objectsWithAlias
*/
public function __construct(
iterable $booleans,
iterable $floats,
iterable $integers,
iterable $strings,
iterable $iterableWithDefaultKeyType,
iterable $iterableWithIntegerKeyType,
iterable $iterableWithStringKeyType,
iterable $objects,
iterable $objectsWithAlias
) {
$this->booleans = $booleans;
$this->floats = $floats;
$this->integers = $integers;
$this->strings = $strings;
$this->iterableWithDefaultKeyType = $iterableWithDefaultKeyType;
$this->iterableWithIntegerKeyType = $iterableWithIntegerKeyType;
$this->iterableWithStringKeyType = $iterableWithStringKeyType;
$this->objects = $objects;
$this->objectsWithAlias = $objectsWithAlias;
}
}