Valinor/tests/Integration/Mapping/Source/Modifier/CamelCaseKeysMappingTest.php
Nathan Boiron ad51039cc3
feat: introduce a source builder
The `Source` class is a new entry point for sources that are not plain 
array or iterable. It allows accessing other features like camel-case 
keys or custom paths mapping in a convenient way.

It should be used as follows:

```php
$source = \CuyZ\Valinor\Mapper\Source\Source::json($jsonString)
    ->camelCaseKeys()
    ->map([
        'towns' => 'cities',
        'towns.*.label' => 'name',
    ]);

$result = (new \CuyZ\Valinor\MapperBuilder())
    ->mapper()
    ->map(SomeClass::class, $source);
```
2022-03-24 14:23:03 +01:00

119 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Source\Modifier;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Source\Modifier\CamelCaseKeys;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
final class CamelCaseKeysMappingTest extends IntegrationTest
{
public function test_underscore_key_is_modified_to_camel_case(): void
{
try {
$object = $this->mapperBuilder->mapper()->map(
SomeClassWithCamelCaseProperty::class,
new CamelCaseKeys(['some_value' => 'foo'])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someValue);
}
public function test_dash_key_is_modified_to_camel_case(): void
{
try {
$object = $this->mapperBuilder->mapper()->map(
SomeClassWithCamelCaseProperty::class,
new CamelCaseKeys(['some-value' => 'foo'])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someValue);
}
public function test_spaced_key_is_modified_to_camel_case(): void
{
try {
$object = $this->mapperBuilder->mapper()->map(
SomeClassWithCamelCaseProperty::class,
new CamelCaseKeys(['some value' => 'foo'])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someValue);
}
public function test_nested_camel_case_keys_are_modified(): void
{
try {
$object = $this->mapperBuilder->mapper()->map(
SomeClassWithNestedProperty::class,
new CamelCaseKeys([
'some_nested_value' => ['some_value' => 'foo'],
])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someNestedValue->someValue);
}
public function test_existing_camel_case_key_is_not_overridden(): void
{
try {
$object = $this->mapperBuilder->mapper()->map(
SomeClassWithCamelCaseProperty::class,
new CamelCaseKeys([
'someValue' => 'bar',
'some_value' => 'foo',
])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('bar', $object->someValue);
}
public function test_multiple_camel_case_keys_are_modified(): void
{
try {
$object = $this->mapperBuilder->mapper()->map(
SomeClassWithCamelCaseProperty::class,
new CamelCaseKeys([
'some_value' => 'foo',
'someValue' => 'bar',
'some_other_value' => 'buz',
])
);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $object->someValue);
self::assertSame('buz', $object->someOtherValue);
}
}
final class SomeClassWithCamelCaseProperty
{
public string $someValue;
public string $someOtherValue = 'fiz';
}
final class SomeClassWithNestedProperty
{
public SomeClassWithCamelCaseProperty $someNestedValue;
}