Valinor/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php

65 lines
1.7 KiB
PHP
Raw Normal View History

2021-11-28 17:43:02 +01:00
<?php
declare(strict_types=1);
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 = [
'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);
}
}
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);
}
}
}
2021-11-28 17:43:02 +01:00
}
class ObjectValues
{
public SimpleObject $object;
public string $string;
2021-11-28 17:43:02 +01:00
}
class ObjectValuesWithConstructor extends ObjectValues
{
public function __construct(SimpleObject $object, string $string)
2021-11-28 17:43:02 +01:00
{
$this->object = $object;
$this->string = $string;
2021-11-28 17:43:02 +01:00
}
}