2021-11-28 17:43:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping;
|
|
|
|
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
2022-05-22 20:43:01 +02:00
|
|
|
use CuyZ\Valinor\MapperBuilder;
|
2021-11-28 17:43:02 +01:00
|
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
|
|
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject;
|
|
|
|
|
|
|
|
use function strtolower;
|
|
|
|
use function strtoupper;
|
|
|
|
|
|
|
|
final class ValueAlteringMappingTest extends IntegrationTest
|
|
|
|
{
|
|
|
|
public function test_alter_string_alters_value(): void
|
|
|
|
{
|
|
|
|
try {
|
2022-05-22 20:43:01 +02:00
|
|
|
$result = (new MapperBuilder())
|
2022-02-15 21:45:38 +01:00
|
|
|
->alter(fn () => 'bar')
|
2021-11-28 17:43:02 +01:00
|
|
|
->alter(fn (string $value) => strtolower($value))
|
|
|
|
->alter(fn (string $value) => strtoupper($value))
|
|
|
|
->alter(/** @param string $value */ fn ($value) => $value . '!')
|
|
|
|
->alter(fn (int $value) => 42)
|
|
|
|
->mapper()
|
|
|
|
->map(SimpleObject::class, ['value' => 'foo']);
|
|
|
|
} catch (MappingError $error) {
|
|
|
|
$this->mappingFail($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertSame('FOO!', $result->value);
|
|
|
|
}
|
2022-04-01 15:06:06 +02:00
|
|
|
|
|
|
|
public function test_value_not_accepted_by_value_altering_callback_is_not_used(): void
|
|
|
|
{
|
|
|
|
try {
|
2022-05-22 20:43:01 +02:00
|
|
|
$result = (new MapperBuilder())
|
2022-04-01 15:06:06 +02:00
|
|
|
->alter(fn (string $value) => $value)
|
|
|
|
->mapper()
|
|
|
|
->map('string|null', null);
|
|
|
|
} catch (MappingError $error) {
|
|
|
|
$this->mappingFail($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
}
|
2022-04-05 19:36:48 +02:00
|
|
|
|
|
|
|
public function test_alter_function_is_called_when_not_the_first_nor_the_last_one(): void
|
|
|
|
{
|
|
|
|
try {
|
2022-05-22 20:43:01 +02:00
|
|
|
$result = (new MapperBuilder())
|
2022-04-05 19:36:48 +02:00
|
|
|
->alter(fn (int $value) => 404)
|
|
|
|
->alter(fn (string $value) => $value . '!')
|
|
|
|
->alter(fn (float $value) => 42.1337)
|
|
|
|
->mapper()
|
|
|
|
->map('string', 'some value');
|
|
|
|
} catch (MappingError $error) {
|
|
|
|
$this->mappingFail($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertSame('some value!', $result);
|
|
|
|
}
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|