|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Nuxtifyts\PhpDto\Tests\Unit\Attributes; |
| 4 | + |
| 5 | +use Nuxtifyts\PhpDto\Tests\Dummies\PropertyNameMapperData; |
| 6 | +use Nuxtifyts\PhpDto\Attributes\Class\MapName; |
| 7 | +use Nuxtifyts\PhpDto\Contexts\ClassContext; |
| 8 | +use Nuxtifyts\PhpDto\Contexts\ClassContext\NameMapperConfig; |
| 9 | +use Nuxtifyts\PhpDto\Data; |
| 10 | +use Nuxtifyts\PhpDto\Pipelines\DeserializePipeline\Pipes\MapNamesPipe; |
| 11 | +use Nuxtifyts\PhpDto\Tests\Unit\UnitCase; |
| 12 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | +use Throwable; |
| 17 | + |
| 18 | +#[CoversClass(MapName::class)] |
| 19 | +#[CoversClass(NameMapperConfig::class)] |
| 20 | +#[CoversClass(ClassContext::class)] |
| 21 | +#[CoversClass(MapNamesPipe::class)] |
| 22 | +#[UsesClass(PropertyNameMapperData::class)] |
| 23 | +final class MapNameTest extends UnitCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param class-string<Data> $dataClassString |
| 27 | + * @param array<string, mixed> $data |
| 28 | + * @param array<string, mixed> $expected |
| 29 | + * |
| 30 | + * @throws Throwable |
| 31 | + */ |
| 32 | + #[Test] |
| 33 | + #[DataProvider('property_name_mapper_data_provider')] |
| 34 | + public function will_be_able_to_map_properties( |
| 35 | + string $dataClassString, |
| 36 | + array $data, |
| 37 | + array $expected |
| 38 | + ): void { |
| 39 | + $data = $dataClassString::from($data); |
| 40 | + |
| 41 | + foreach ($expected as $key => $value) { |
| 42 | + self::assertObjectHasProperty($key, $data); |
| 43 | + self::assertEquals($value, $data->{$key}); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return array<string, mixed> |
| 49 | + */ |
| 50 | + public static function property_name_mapper_data_provider(): array |
| 51 | + { |
| 52 | + return [ |
| 53 | + 'snake_case' => [ |
| 54 | + 'dataClassString' => PropertyNameMapperData::class, |
| 55 | + 'data' => [ 'camel_case' => 'value' ], |
| 56 | + 'expected' => [ 'camelCase' => 'value' ] |
| 57 | + ], |
| 58 | + 'kebab_case' => [ |
| 59 | + 'dataClassString' => PropertyNameMapperData::class, |
| 60 | + 'data' => [ 'camel-case' => 'value' ], |
| 61 | + 'expected' => [ 'camelCase' => 'value' ] |
| 62 | + ], |
| 63 | + 'pascal_case' => [ |
| 64 | + 'dataClassString' => PropertyNameMapperData::class, |
| 65 | + 'data' => [ 'CamelCase' => 'value' ], |
| 66 | + 'expected' => [ 'camelCase' => 'value' ] |
| 67 | + ], |
| 68 | + 'no_change' => [ |
| 69 | + 'dataClassString' => PropertyNameMapperData::class, |
| 70 | + 'data' => [ 'camelCase' => 'value' ], |
| 71 | + 'expected' => [ 'camelCase' => 'value' ] |
| 72 | + ], |
| 73 | + 'multiple_letter_cases_can_be_transformed' => [ |
| 74 | + 'dataClassString' => PropertyNameMapperData::class, |
| 75 | + 'data' => [ 'CamelCase' => 'value', 'un_kNoWnCAsE' => 'anotherValue', 'another_snake_case' => 'value' ], |
| 76 | + 'expected' => [ 'camelCase' => 'value' ] |
| 77 | + ] |
| 78 | + ]; |
| 79 | + } |
| 80 | +} |
0 commit comments