Valinor/tests/Integration/Mapping/Source/SourceTest.php
Romain Canon 69ad3f4777 feat: allow injecting a cache implementation that is used by the mapper
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, [/* … */]);
```
2022-05-23 20:28:02 +02:00

167 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Source;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Source\Source;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
use IteratorAggregate;
use Traversable;
final class SourceTest extends IntegrationTest
{
public function test_iterable_source(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::iterable(new SomeIterableClass())
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
public function test_array_source(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::array(['someNestedValue' => ['someValue' => 'foo']])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
public function test_json_source(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::json('{"someNestedValue": {"someValue": "foo"}}')
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
/**
* @requires extension yaml
*/
public function test_yaml_source(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::yaml("someNestedValue:\n someValue: foo")
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
public function test_camel_case_keys(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::json('{"some nested value": {"some value": "foo"}}')
->camelCaseKeys()
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
public function test_path_mapping(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::json('{"A": {"B": "foo"}}')
->map([
'A' => 'someNestedValue',
'A.B' => 'someValue',
])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
public function test_camel_case_keys_then_path_mapping(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::json('{"level-one": {"level-two": "foo"}}')
->camelCaseKeys()
->map([
'levelOne' => 'someNestedValue',
'levelOne.levelTwo' => 'someValue',
])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
public function test_path_mapping_then_camel_case_keys(): void
{
try {
$object = (new MapperBuilder())->mapper()->map(
SomeClassWithSubProperty::class,
Source::json('{"level-one": {"level-two": "foo"}}')
->map([
'level-one' => 'some-nested-value',
'level-one.level-two' => 'some-value',
])
->camelCaseKeys()
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
}
final class SomeClassWithSingleProperty
{
public string $someValue;
}
final class SomeClassWithSubProperty
{
public SomeClassWithSingleProperty $someNestedValue;
}
/**
* @implements IteratorAggregate<mixed>
*/
final class SomeIterableClass implements IteratorAggregate
{
public function getIterator(): Traversable
{
yield from ['someNestedValue' => ['someValue' => 'foo']];
}
}