Valinor/tests/Integration/Mapping/SingleNodeMappingTest.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

83 lines
2.6 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;
final class SingleNodeMappingTest extends IntegrationTest
{
public function test_single_property_and_constructor_parameter_are_mapped_properly(): void
{
$mapper = (new MapperBuilder())->mapper();
// Note that the key `value` is missing from the source
$scalarSource = 'foo';
$arraySource = ['foo', 42.404, 1337];
try {
$singleScalarProperty = $mapper->map(SingleScalarProperty::class, $scalarSource);
$singleConstructorScalarParameter = $mapper->map(SingleConstructorScalarParameter::class, $scalarSource);
$singleArrayProperty = $mapper->map(SingleArrayProperty::class, $arraySource);
$singleConstructorArrayParameter = $mapper->map(SingleConstructorArrayParameter::class, $arraySource);
$singleScalarPropertyWithDefaultValue = $mapper->map(SingleScalarPropertyWithDefaultValue::class, null);
$singleConstructorParameterWithDefaultValue = $mapper->map(SingleConstructorParameterWithDefaultValue::class, null);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $singleScalarProperty->value);
self::assertSame('foo', $singleConstructorScalarParameter->value);
self::assertSame(['foo', '42.404', '1337'], $singleArrayProperty->value);
self::assertSame(['foo', '42.404', '1337'], $singleConstructorArrayParameter->value);
self::assertSame('foo', $singleScalarPropertyWithDefaultValue->value);
self::assertSame('bar', $singleConstructorParameterWithDefaultValue->value);
}
}
class SingleScalarProperty
{
public string $value;
}
class SingleConstructorScalarParameter extends SingleScalarProperty
{
public function __construct(string $value)
{
$this->value = $value;
}
}
class SingleArrayProperty
{
/** @var array<string> */
public array $value = [];
}
class SingleConstructorArrayParameter extends SingleArrayProperty
{
/**
* @param array<string> $value
*/
public function __construct(array $value)
{
$this->value = $value;
}
}
class SingleScalarPropertyWithDefaultValue
{
public string $value = 'foo';
}
class SingleConstructorParameterWithDefaultValue extends SingleScalarPropertyWithDefaultValue
{
public function __construct(string $value = 'bar')
{
$this->value = $value;
}
}