|
| 1 | +<?php |
| 2 | + |
| 3 | +use Spatie\LaravelData\Attributes\DataCollectionOf; |
| 4 | +use Spatie\LaravelData\Attributes\MapOutputName; |
| 5 | +use Spatie\LaravelData\Data; |
| 6 | +use Spatie\LaravelData\Tests\Fakes\SimpleDataWithMappedDottedProperty; |
| 7 | + |
| 8 | +it('can map dotted property names when transforming without undot', function () { |
| 9 | + config()->set('data.features.expand_dot_notation', false); |
| 10 | + $data = new SimpleDataWithMappedDottedProperty('hello'); |
| 11 | + $dataCollection = SimpleDataWithMappedDottedProperty::collect([ |
| 12 | + ['dotted.description' => 'never'], |
| 13 | + ['dotted.description' => 'gonna'], |
| 14 | + ['dotted.description' => 'give'], |
| 15 | + ['dotted.description' => 'you'], |
| 16 | + ['dotted' => ['description' => 'up']], |
| 17 | + ]); |
| 18 | + |
| 19 | + $dataClass = new class ('hello', $data, $data, $dataCollection, $dataCollection) extends Data { |
| 20 | + public function __construct( |
| 21 | + #[MapOutputName('dotted.property')] |
| 22 | + public string $string, |
| 23 | + public SimpleDataWithMappedDottedProperty $nested, |
| 24 | + #[MapOutputName('dotted.nested_other')] |
| 25 | + public SimpleDataWithMappedDottedProperty $nested_renamed, |
| 26 | + #[DataCollectionOf(SimpleDataWithMappedDottedProperty::class)] |
| 27 | + public array $nested_collection, |
| 28 | + #[ |
| 29 | + MapOutputName('dotted.nested_other_collection'), |
| 30 | + DataCollectionOf(SimpleDataWithMappedDottedProperty::class) |
| 31 | + ] |
| 32 | + public array $nested_renamed_collection, |
| 33 | + ) { |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + expect($dataClass->toArray())->toMatchArray([ |
| 38 | + 'dotted.property' => 'hello', |
| 39 | + 'nested' => [ |
| 40 | + 'dotted.description' => 'hello', |
| 41 | + ], |
| 42 | + 'dotted.nested_other' => [ |
| 43 | + 'dotted.description' => 'hello', |
| 44 | + ], |
| 45 | + 'nested_collection' => [ |
| 46 | + ['dotted.description' => 'never'], |
| 47 | + ['dotted.description' => 'gonna'], |
| 48 | + ['dotted.description' => 'give'], |
| 49 | + ['dotted.description' => 'you'], |
| 50 | + ['dotted.description' => 'up'], |
| 51 | + ], |
| 52 | + 'dotted.nested_other_collection' => [ |
| 53 | + ['dotted.description' => 'never'], |
| 54 | + ['dotted.description' => 'gonna'], |
| 55 | + ['dotted.description' => 'give'], |
| 56 | + ['dotted.description' => 'you'], |
| 57 | + ['dotted.description' => 'up'], |
| 58 | + ], |
| 59 | + ]); |
| 60 | +}); |
| 61 | + |
| 62 | +it('can map dotted property names when transforming with undot', function () { |
| 63 | + config()->set('data.features.expand_dot_notation', true); |
| 64 | + $data = new SimpleDataWithMappedDottedProperty('hello'); |
| 65 | + $dataCollection = SimpleDataWithMappedDottedProperty::collect([ |
| 66 | + ['dotted.description' => 'never'], |
| 67 | + ['dotted.description' => 'gonna'], |
| 68 | + ['dotted.description' => 'give'], |
| 69 | + ['dotted.description' => 'you'], |
| 70 | + ['dotted' => ['description' => 'up']], |
| 71 | + ]); |
| 72 | + |
| 73 | + $dataClass = new class ('hello', $data, $data, $dataCollection, $dataCollection) extends Data { |
| 74 | + public function __construct( |
| 75 | + #[MapOutputName('dotted.property')] |
| 76 | + public string $string, |
| 77 | + public SimpleDataWithMappedDottedProperty $nested, |
| 78 | + #[MapOutputName('dotted.nested_other')] |
| 79 | + public SimpleDataWithMappedDottedProperty $nested_renamed, |
| 80 | + #[DataCollectionOf(SimpleDataWithMappedDottedProperty::class)] |
| 81 | + public array $nested_collection, |
| 82 | + #[ |
| 83 | + MapOutputName('dotted.nested_other_collection'), |
| 84 | + DataCollectionOf(SimpleDataWithMappedDottedProperty::class) |
| 85 | + ] |
| 86 | + public array $nested_renamed_collection, |
| 87 | + ) { |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + expect($dataClass->toArray())->toMatchArray([ |
| 92 | + 'dotted' => [ |
| 93 | + 'property' => 'hello', |
| 94 | + 'nested_other' => ['dotted' => ['description' => 'hello']], |
| 95 | + 'nested_other_collection' => [ |
| 96 | + ['dotted' => ['description' => 'never']], |
| 97 | + ['dotted' => ['description' => 'gonna']], |
| 98 | + ['dotted' => ['description' => 'give']], |
| 99 | + ['dotted' => ['description' => 'you']], |
| 100 | + ['dotted' => ['description' => 'up']], |
| 101 | + ], |
| 102 | + ], |
| 103 | + 'nested' => [ |
| 104 | + 'dotted' => ['description' => 'hello'], |
| 105 | + ], |
| 106 | + 'nested_collection' => [ |
| 107 | + ['dotted' => ['description' => 'never']], |
| 108 | + ['dotted' => ['description' => 'gonna']], |
| 109 | + ['dotted' => ['description' => 'give']], |
| 110 | + ['dotted' => ['description' => 'you']], |
| 111 | + ['dotted' => ['description' => 'up']], |
| 112 | + ], |
| 113 | + ]); |
| 114 | +}); |
0 commit comments