mirror of
https://github.com/danog/Valinor.git
synced 2024-12-03 10:07:48 +01:00
69ad3f4777
The cache implementation that was previously injected in the mapper builder must now be manually injected. This gives better control on when the cache should be enabled, especially depending on which environment the application is running. The library provides a cache implementation out of the box, which saves cache entries into the file system. It is also possible to use any PSR-16 compliant implementation, as long as it is capable of caching the entries handled by the library. ```php $cache = new \CuyZ\Valinor\Cache\FileSystemCache('path/to/cache-dir'); (new \CuyZ\Valinor\MapperBuilder()) ->withCache($cache) ->mapper() ->map(SomeClass::class, [/* … */]); ```
65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping;
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
|
use CuyZ\Valinor\MapperBuilder;
|
|
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 {
|
|
$result = (new MapperBuilder())
|
|
->alter(fn () => 'bar')
|
|
->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);
|
|
}
|
|
|
|
public function test_value_not_accepted_by_value_altering_callback_is_not_used(): void
|
|
{
|
|
try {
|
|
$result = (new MapperBuilder())
|
|
->alter(fn (string $value) => $value)
|
|
->mapper()
|
|
->map('string|null', null);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertNull($result);
|
|
}
|
|
|
|
public function test_alter_function_is_called_when_not_the_first_nor_the_last_one(): void
|
|
{
|
|
try {
|
|
$result = (new MapperBuilder())
|
|
->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);
|
|
}
|
|
}
|