mirror of
https://github.com/danog/Valinor.git
synced 2024-12-13 01:27:39 +01:00
b2e810e3ce
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.
80 lines
3.0 KiB
PHP
80 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
|
use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum;
|
|
use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum;
|
|
use CuyZ\Valinor\Tests\Fixture\Enum\PureEnum;
|
|
use CuyZ\Valinor\Tests\Fixture\Object\StringableObject;
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
|
|
final class EnumValuesMappingTest extends IntegrationTest
|
|
{
|
|
/**
|
|
* @requires PHP >= 8.1
|
|
*/
|
|
public function test_values_are_mapped_properly(): void
|
|
{
|
|
$source = [
|
|
'pureEnumWithFirstValue' => 'FOO',
|
|
'pureEnumWithSecondValue' => 'BAR',
|
|
'backedStringEnumWithFirstValue' => 'foo',
|
|
'backedStringEnumWithSecondValue' => new StringableObject('bar'),
|
|
'backedIntegerEnumWithFirstValue' => '42',
|
|
'backedIntegerEnumWithSecondValue' => 1337,
|
|
];
|
|
|
|
foreach ([EnumValues::class, EnumValuesWithConstructor::class] as $class) {
|
|
try {
|
|
$result = $this->mapperBuilder->mapper()->map($class, $source);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertSame(PureEnum::FOO, $result->pureEnumWithFirstValue);
|
|
self::assertSame(PureEnum::BAR, $result->pureEnumWithSecondValue);
|
|
self::assertSame(BackedStringEnum::FOO, $result->backedStringEnumWithFirstValue);
|
|
self::assertSame(BackedStringEnum::BAR, $result->backedStringEnumWithSecondValue);
|
|
self::assertSame(BackedIntegerEnum::FOO, $result->backedIntegerEnumWithFirstValue);
|
|
self::assertSame(BackedIntegerEnum::BAR, $result->backedIntegerEnumWithSecondValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
class EnumValues
|
|
{
|
|
public PureEnum $pureEnumWithFirstValue;
|
|
|
|
public PureEnum $pureEnumWithSecondValue;
|
|
|
|
public BackedStringEnum $backedStringEnumWithFirstValue;
|
|
|
|
public BackedStringEnum $backedStringEnumWithSecondValue;
|
|
|
|
public BackedIntegerEnum $backedIntegerEnumWithFirstValue;
|
|
|
|
public BackedIntegerEnum $backedIntegerEnumWithSecondValue;
|
|
}
|
|
|
|
class EnumValuesWithConstructor extends EnumValues
|
|
{
|
|
public function __construct(
|
|
PureEnum $pureEnumWithFirstValue,
|
|
PureEnum $pureEnumWithSecondValue,
|
|
BackedStringEnum $backedStringEnumWithFirstValue,
|
|
BackedStringEnum $backedStringEnumWithSecondValue,
|
|
BackedIntegerEnum $backedIntegerEnumWithFirstValue,
|
|
BackedIntegerEnum $backedIntegerEnumWithSecondValue
|
|
) {
|
|
$this->pureEnumWithFirstValue = $pureEnumWithFirstValue;
|
|
$this->pureEnumWithSecondValue = $pureEnumWithSecondValue;
|
|
$this->backedStringEnumWithFirstValue = $backedStringEnumWithFirstValue;
|
|
$this->backedStringEnumWithSecondValue = $backedStringEnumWithSecondValue;
|
|
$this->backedIntegerEnumWithFirstValue = $backedIntegerEnumWithFirstValue;
|
|
$this->backedIntegerEnumWithSecondValue = $backedIntegerEnumWithSecondValue;
|
|
}
|
|
}
|