2021-11-28 17:43:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-12-29 00:09:34 +01:00
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
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 = [
|
2022-01-07 13:21:43 +01:00
|
|
|
'string' => 'foo',
|
2021-11-28 17:43:02 +01:00
|
|
|
'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);
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 13:21:43 +01:00
|
|
|
|
|
|
|
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());
|
2022-04-04 23:36:28 +02:00
|
|
|
self::assertSame("Value 'foo' does not match `array{object: ?, string: string}`.", (string)$error);
|
2022-01-07 13:21:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ObjectValues
|
|
|
|
{
|
|
|
|
public SimpleObject $object;
|
2022-01-07 13:21:43 +01:00
|
|
|
|
|
|
|
public string $string;
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ObjectValuesWithConstructor extends ObjectValues
|
|
|
|
{
|
2022-01-07 13:21:43 +01:00
|
|
|
public function __construct(SimpleObject $object, string $string)
|
2021-11-28 17:43:02 +01:00
|
|
|
{
|
|
|
|
$this->object = $object;
|
2022-01-07 13:21:43 +01:00
|
|
|
$this->string = $string;
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
}
|