mirror of
https://github.com/danog/Valinor.git
synced 2024-12-12 09:09:38 +01:00
90dc586018
The mapper is now more type-sensitive and will fail in the following situations: - When a value does not match exactly the awaited scalar type, for instance a string `"42"` given to a node that awaits an integer. - When unnecessary array keys are present, for instance mapping an array `['foo' => …, 'bar' => …, 'baz' => …]` to an object that needs only `foo` and `bar`. - When permissive types like `mixed` or `object` are encountered. These limitations can be bypassed by enabling the flexible mode: ```php (new \CuyZ\Valinor\MapperBuilder()) ->flexible() ->mapper(); ->map('array{foo: int, bar: bool}', [ 'foo' => '42', // Will be cast from `string` to `int` 'bar' => 'true', // Will be cast from `string` to `bool` 'baz' => '…', // Will be ignored ]); ``` When using this library for a provider application — for instance an API endpoint that can be called with a JSON payload — it is recommended to use the strict mode. This ensures that the consumers of the API provide the exact awaited data structure, and prevents unknown values to be passed. When using this library as a consumer of an external source, it can make sense to enable the flexible mode. This allows for instance to convert string numeric values to integers or to ignore data that is present in the source but not needed in the application. --- All these changes led to a new check that runs on all registered object constructors. If a collision is found between several constructors that have the same signature (the same parameter names), an exception will be thrown. ```php final class SomeClass { public static function constructorA(string $foo, string $bar): self { // … } public static function constructorB(string $foo, string $bar): self { // … } } (new \CuyZ\Valinor\MapperBuilder()) ->registerConstructor( SomeClass::constructorA(...), SomeClass::constructorB(...), ) ->mapper(); ->map(SomeClass::class, [ 'foo' => 'foo', 'bar' => 'bar', ]); // Exception: A collision was detected […] ```
84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree\Builder;
|
|
|
|
use AssertionError;
|
|
use CuyZ\Valinor\Mapper\Tree\Builder\EnumNodeBuilder;
|
|
use CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder;
|
|
use CuyZ\Valinor\Mapper\Tree\Exception\InvalidEnumValue;
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\FakeShell;
|
|
use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum;
|
|
use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum;
|
|
use CuyZ\Valinor\Tests\Fixture\Enum\PureEnum;
|
|
use CuyZ\Valinor\Type\Types\EnumType;
|
|
use PHPUnit\Framework\TestCase;
|
|
use stdClass;
|
|
|
|
/**
|
|
* @requires PHP >= 8.1
|
|
*/
|
|
final class EnumNodeBuilderTest extends TestCase
|
|
{
|
|
private RootNodeBuilder $builder;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->builder = new RootNodeBuilder(new EnumNodeBuilder(true));
|
|
}
|
|
|
|
public function test_invalid_type_fails_assertion(): void
|
|
{
|
|
$this->expectException(AssertionError::class);
|
|
|
|
$this->builder->build(FakeShell::any());
|
|
}
|
|
|
|
public function test_invalid_value_throws_exception(): void
|
|
{
|
|
$type = new EnumType(PureEnum::class);
|
|
|
|
$this->expectException(InvalidEnumValue::class);
|
|
$this->expectExceptionCode(1633093113);
|
|
$this->expectExceptionMessage("Value 'foo' does not match any of 'FOO', 'BAR'.");
|
|
|
|
$this->builder->build(FakeShell::new($type, 'foo'));
|
|
}
|
|
|
|
public function test_invalid_string_value_throws_exception(): void
|
|
{
|
|
$type = new EnumType(BackedStringEnum::class);
|
|
|
|
$this->expectException(InvalidEnumValue::class);
|
|
$this->expectExceptionCode(1633093113);
|
|
$this->expectExceptionMessage("Value object(stdClass) does not match any of 'foo', 'bar'.");
|
|
|
|
$this->builder->build(FakeShell::new($type, new stdClass()));
|
|
}
|
|
|
|
public function test_boolean_instead_of_integer_value_throws_exception(): void
|
|
{
|
|
$type = new EnumType(BackedIntegerEnum::class);
|
|
|
|
$this->expectException(InvalidEnumValue::class);
|
|
$this->expectExceptionCode(1633093113);
|
|
$this->expectExceptionMessage('Value false does not match any of 42, 1337.');
|
|
|
|
$this->builder->build(FakeShell::new($type, false));
|
|
}
|
|
|
|
public function test_invalid_integer_value_throws_exception(): void
|
|
{
|
|
$type = new EnumType(BackedIntegerEnum::class);
|
|
|
|
$this->expectException(InvalidEnumValue::class);
|
|
$this->expectExceptionCode(1633093113);
|
|
$this->expectExceptionMessage('Value object(stdClass) does not match any of 42, 1337.');
|
|
|
|
$this->builder->build(FakeShell::new($type, new stdClass()));
|
|
}
|
|
}
|