Valinor/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php
Romain Canon 05cf4a4a4d feat: improve mapping error messages
Enhances most of the messages for the end users.

Two major changes can be noticed:

1. In most cases no class name will be written in the message; it
   prevents users that potentially have no access to the codebase to
   get a useless/unclear information.

2. The input values are now properly formatted; for instance a string
   value will now be written directly instead of the type `string`;
   arrays are also handled with the array shape format, for instance:
   `array{foo: 'some string'}`.
2022-05-21 16:30:24 +02:00

65 lines
1.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;
final class ObjectValuesMappingTest extends IntegrationTest
{
public function test_values_are_mapped_properly(): void
{
$source = [
'string' => 'foo',
'object' => [
'value' => 'foo',
],
];
foreach ([ObjectValues::class, ObjectValuesWithConstructor::class] as $class) {
try {
$result = $this->mapperBuilder->mapper()->map($class, $source);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $result->object->value);
}
}
public function test_invalid_iterable_source_throws_exception(): void
{
$source = 'foo';
foreach ([ObjectValues::class, ObjectValuesWithConstructor::class] as $class) {
try {
$this->mapperBuilder->mapper()->map($class, $source);
} catch (MappingError $exception) {
$error = $exception->node()->messages()[0];
self::assertSame('1632903281', $error->code());
self::assertSame("Value 'foo' does not match `array{object: ?, string: string}`.", (string)$error);
}
}
}
}
class ObjectValues
{
public SimpleObject $object;
public string $string;
}
class ObjectValuesWithConstructor extends ObjectValues
{
public function __construct(SimpleObject $object, string $string)
{
$this->object = $object;
$this->string = $string;
}
}