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\NativeUnionValues;
|
|
|
|
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\NativeUnionValuesWithConstructor;
|
|
|
|
|
|
|
|
final class UnionValuesMappingTest extends IntegrationTest
|
|
|
|
{
|
|
|
|
public function test_values_are_mapped_properly(): void
|
|
|
|
{
|
|
|
|
$source = [
|
|
|
|
'scalarWithBoolean' => true,
|
|
|
|
'scalarWithFloat' => 42.404,
|
|
|
|
'scalarWithInteger' => 1337,
|
|
|
|
'scalarWithString' => 'foo',
|
|
|
|
'nullableWithString' => 'bar',
|
|
|
|
'nullableWithNull' => null,
|
2021-11-29 11:33:23 +01:00
|
|
|
'integerValue' => 1337,
|
|
|
|
'stringValueWithSingleQuote' => 'bar',
|
|
|
|
'stringValueWithDoubleQuote' => 'fiz',
|
2021-11-28 17:43:02 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$classes = [UnionValues::class, UnionValuesWithConstructor::class];
|
|
|
|
|
|
|
|
if (PHP_VERSION_ID >= 8_00_00) {
|
|
|
|
$classes[] = NativeUnionValues::class;
|
|
|
|
$classes[] = NativeUnionValuesWithConstructor::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
try {
|
|
|
|
$result = $this->mapperBuilder->mapper()->map($class, $source);
|
|
|
|
} catch (MappingError $error) {
|
|
|
|
$this->mappingFail($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertSame(true, $result->scalarWithBoolean);
|
|
|
|
self::assertSame(42.404, $result->scalarWithFloat);
|
|
|
|
self::assertSame(1337, $result->scalarWithInteger);
|
|
|
|
self::assertSame('foo', $result->scalarWithString);
|
|
|
|
self::assertSame('bar', $result->nullableWithString);
|
|
|
|
self::assertSame(null, $result->nullableWithNull);
|
|
|
|
|
2021-11-29 11:33:23 +01:00
|
|
|
if ($result instanceof UnionValues) {
|
|
|
|
self::assertSame(1337, $result->integerValue);
|
|
|
|
self::assertSame('bar', $result->stringValueWithSingleQuote);
|
|
|
|
self::assertSame('fiz', $result->stringValueWithDoubleQuote);
|
|
|
|
}
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnionValues
|
|
|
|
{
|
|
|
|
/** @var bool|float|int|string */
|
|
|
|
public $scalarWithBoolean = 'Schwifty!';
|
|
|
|
|
|
|
|
/** @var bool|float|int|string */
|
|
|
|
public $scalarWithFloat = 'Schwifty!';
|
|
|
|
|
|
|
|
/** @var bool|float|int|string */
|
|
|
|
public $scalarWithInteger = 'Schwifty!';
|
|
|
|
|
|
|
|
/** @var bool|float|int|string */
|
|
|
|
public $scalarWithString = 'Schwifty!';
|
|
|
|
|
|
|
|
/** @var string|null|float */
|
|
|
|
public $nullableWithString = 'Schwifty!';
|
|
|
|
|
|
|
|
/** @var string|null|float */
|
|
|
|
public $nullableWithNull = 'Schwifty!';
|
2021-11-29 11:33:23 +01:00
|
|
|
|
|
|
|
/** @var 42|1337 */
|
|
|
|
public int $integerValue = 42;
|
|
|
|
|
|
|
|
/** @var 'foo'|'bar' */
|
|
|
|
public string $stringValueWithSingleQuote;
|
|
|
|
|
|
|
|
/** @var "baz"|"fiz" */
|
|
|
|
public string $stringValueWithDoubleQuote;
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class UnionValuesWithConstructor extends UnionValues
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param bool|float|int|string $scalarWithBoolean
|
|
|
|
* @param bool|float|int|string $scalarWithFloat
|
|
|
|
* @param bool|float|int|string $scalarWithInteger
|
|
|
|
* @param bool|float|int|string $scalarWithString
|
|
|
|
* @param string|null|float $nullableWithString
|
|
|
|
* @param string|null|float $nullableWithNull
|
2021-11-29 11:33:23 +01:00
|
|
|
* @param 42|1337 $integerValue
|
|
|
|
* @param 'foo'|'bar' $stringValueWithSingleQuote
|
|
|
|
* @param "baz"|"fiz" $stringValueWithDoubleQuote
|
2021-11-28 17:43:02 +01:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$scalarWithBoolean = 'Schwifty!',
|
|
|
|
$scalarWithFloat = 'Schwifty!',
|
|
|
|
$scalarWithInteger = 'Schwifty!',
|
|
|
|
$scalarWithString = 'Schwifty!',
|
|
|
|
$nullableWithString = 'Schwifty!',
|
2021-11-29 11:33:23 +01:00
|
|
|
$nullableWithNull = 'Schwifty!',
|
|
|
|
int $integerValue = 42,
|
|
|
|
string $stringValueWithSingleQuote = 'foo',
|
|
|
|
string $stringValueWithDoubleQuote = 'baz'
|
2021-11-28 17:43:02 +01:00
|
|
|
) {
|
|
|
|
$this->scalarWithBoolean = $scalarWithBoolean;
|
|
|
|
$this->scalarWithFloat = $scalarWithFloat;
|
|
|
|
$this->scalarWithInteger = $scalarWithInteger;
|
|
|
|
$this->scalarWithString = $scalarWithString;
|
|
|
|
$this->nullableWithString = $nullableWithString;
|
|
|
|
$this->nullableWithNull = $nullableWithNull;
|
2021-11-29 11:33:23 +01:00
|
|
|
$this->integerValue = $integerValue;
|
|
|
|
$this->stringValueWithSingleQuote = $stringValueWithSingleQuote;
|
|
|
|
$this->stringValueWithDoubleQuote = $stringValueWithDoubleQuote;
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
}
|