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

145 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject;
use stdClass;
final class ShapedArrayValuesMappingTest extends IntegrationTest
{
public function test_values_are_mapped_properly(): void
{
$source = [
'basicShapedArrayWithExcessiveKey' => [
'foo' => 'foo',
'bar' => 42,
],
'basicShapedArrayWithStringKeys' => [
'foo' => 'fiz',
'bar' => 42,
],
'basicShapedArrayWithIntegerKeys' => [
0 => 'fiz',
1 => 42.404,
],
'shapedArrayWithObject' => [
'foo' => ['value' => 'bar'],
],
'shapedArrayWithOptionalValue' => [
'optionalString' => 'some value',
],
'shapedArrayOnSeveralLines' => [
'foo' => 'fiz',
'bar' => 42,
],
'advancedShapedArray' => [
'mandatoryString' => 'bar',
1337,
'42.404',
],
];
foreach ([ShapedArrayValues::class, ShapedArrayValuesWithConstructor::class] as $class) {
try {
$result = (new MapperBuilder())->mapper()->map($class, $source);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame(['foo' => 'foo'], $result->basicShapedArrayWithExcessiveKey);
self::assertSame($source['basicShapedArrayWithStringKeys'], $result->basicShapedArrayWithStringKeys);
self::assertSame($source['basicShapedArrayWithIntegerKeys'], $result->basicShapedArrayWithIntegerKeys);
self::assertInstanceOf(SimpleObject::class, $result->shapedArrayWithObject['foo']); // @phpstan-ignore-line
self::assertSame($source['shapedArrayWithOptionalValue'], $result->shapedArrayWithOptionalValue);
self::assertSame($source['shapedArrayOnSeveralLines'], $result->shapedArrayOnSeveralLines);
self::assertSame('bar', $result->advancedShapedArray['mandatoryString']);
self::assertSame(1337, $result->advancedShapedArray[0]);
self::assertSame(42.404, $result->advancedShapedArray[1]);
}
}
public function test_value_that_cannot_be_casted_throws_exception(): void
{
try {
(new MapperBuilder())->mapper()->map(ShapedArrayValues::class, [
'basicShapedArrayWithStringKeys' => [
'foo' => new stdClass(),
'bar' => 42,
],
]);
} catch (MappingError $exception) {
$error = $exception->node()->children()['basicShapedArrayWithStringKeys']->children()['foo']->messages()[0];
self::assertSame('1618736242', $error->code());
self::assertSame('Cannot cast object(stdClass) to `string`.', (string)$error);
}
}
}
class ShapedArrayValues
{
/** @var array{foo: string} */
public array $basicShapedArrayWithExcessiveKey;
/** @var array{foo: string, bar: int} */
public array $basicShapedArrayWithStringKeys;
/** @var array{0: string, 1: float} */
public array $basicShapedArrayWithIntegerKeys;
/** @var array{foo: SimpleObject} */
public array $shapedArrayWithObject;
/** @var array{optionalString?: string} */
public array $shapedArrayWithOptionalValue;
/**
* @var array{
* foo: string,
* bar: int
* }
*/
public array $shapedArrayOnSeveralLines;
/** @var array{0: int, float, optionalString?: string, mandatoryString: string} */
public array $advancedShapedArray;
}
class ShapedArrayValuesWithConstructor extends ShapedArrayValues
{
/**
* @param array{foo: string} $basicShapedArrayWithExcessiveKey
* @param array{foo: string, bar: int} $basicShapedArrayWithStringKeys
* @param array{0: string, 1: float} $basicShapedArrayWithIntegerKeys
* @param array{foo: SimpleObject} $shapedArrayWithObject
* @param array{optionalString?: string} $shapedArrayWithOptionalValue
* @param array{
* foo: string,
* bar: int
* } $shapedArrayOnSeveralLines
* @param array{0: int, float, optionalString?: string, mandatoryString: string} $advancedShapedArray
*/
public function __construct(
array $basicShapedArrayWithExcessiveKey,
array $basicShapedArrayWithStringKeys,
array $basicShapedArrayWithIntegerKeys,
array $shapedArrayWithObject,
array $shapedArrayWithOptionalValue,
array $shapedArrayOnSeveralLines,
array $advancedShapedArray
) {
$this->basicShapedArrayWithExcessiveKey = $basicShapedArrayWithExcessiveKey;
$this->basicShapedArrayWithStringKeys = $basicShapedArrayWithStringKeys;
$this->basicShapedArrayWithIntegerKeys = $basicShapedArrayWithIntegerKeys;
$this->shapedArrayWithObject = $shapedArrayWithObject;
$this->shapedArrayWithOptionalValue = $shapedArrayWithOptionalValue;
$this->shapedArrayOnSeveralLines = $shapedArrayOnSeveralLines;
$this->advancedShapedArray = $advancedShapedArray;
}
}