2021-12-29 00:09:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Other;
|
|
|
|
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
2022-05-22 20:43:01 +02:00
|
|
|
use CuyZ\Valinor\MapperBuilder;
|
2021-12-29 00:09:34 +01:00
|
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
|
|
|
|
|
|
final class ShapedArrayMappingTest extends IntegrationTest
|
|
|
|
{
|
|
|
|
public function test_values_are_mapped_properly(): void
|
|
|
|
{
|
|
|
|
$source = [
|
|
|
|
'foo' => 'foo',
|
|
|
|
'bar' => '42',
|
|
|
|
'fiz' => '1337.404',
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
2022-05-22 20:43:01 +02:00
|
|
|
$result = (new MapperBuilder())->mapper()->map('array{foo: string, bar: int, fiz: float}', $source);
|
2021-12-29 00:09:34 +01:00
|
|
|
} catch (MappingError $error) {
|
|
|
|
$this->mappingFail($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertSame('foo', $result['foo']);
|
|
|
|
self::assertSame(42, $result['bar']);
|
|
|
|
self::assertSame(1337.404, $result['fiz']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_shared_values_are_mapped_properly(): void
|
|
|
|
{
|
|
|
|
$source = [
|
|
|
|
'foo' => 'foo',
|
|
|
|
'bar' => '42',
|
|
|
|
'fiz' => '1337.404',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach (['array{foo: string, bar: int}', 'array{bar: int, fiz:float}'] as $signature) {
|
|
|
|
try {
|
2022-05-22 20:43:01 +02:00
|
|
|
$result = (new MapperBuilder())->mapper()->map($signature, $source);
|
2021-12-29 00:09:34 +01:00
|
|
|
} catch (MappingError $error) {
|
|
|
|
$this->mappingFail($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertSame(42, $result['bar']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|