|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\Mapper\Tests\Type; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\Group; |
| 9 | +use TypeLang\Mapper\Context\Direction; |
| 10 | +use TypeLang\Mapper\Platform\PlatformInterface; |
| 11 | +use TypeLang\Mapper\Platform\StandardPlatform; |
| 12 | +use TypeLang\Mapper\Tests\Type\Stub\AnyTypeStub; |
| 13 | +use TypeLang\Mapper\Tests\Type\Stub\IntBackedEnumStub; |
| 14 | +use TypeLang\Mapper\Tests\Type\Stub\StringBackedEnumStub; |
| 15 | +use TypeLang\Mapper\Tests\Type\Stub\UnitEnumStub; |
| 16 | +use TypeLang\Mapper\Type\Builder\SimpleTypeBuilder; |
| 17 | +use TypeLang\Mapper\Type\MixedType; |
| 18 | +use TypeLang\Mapper\Type\TypeInterface; |
| 19 | + |
| 20 | +#[Group('type')] |
| 21 | +#[CoversClass(MixedType::class)] |
| 22 | +final class MixedTypeTest extends TypeTestCase |
| 23 | +{ |
| 24 | + protected function createPlatform(): PlatformInterface |
| 25 | + { |
| 26 | + return new class extends StandardPlatform { |
| 27 | + public function getTypes(Direction $direction): iterable |
| 28 | + { |
| 29 | + yield new SimpleTypeBuilder('resource', AnyTypeStub::class); |
| 30 | + |
| 31 | + yield from parent::getTypes($direction); |
| 32 | + } |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + protected static function createType(): TypeInterface |
| 37 | + { |
| 38 | + return new MixedType(); |
| 39 | + } |
| 40 | + |
| 41 | + protected static function matchValues(bool $normalize): iterable |
| 42 | + { |
| 43 | + foreach (self::defaultMatchDataProviderSamples() as $value => $_) { |
| 44 | + yield $value => true; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + protected static function castValues(bool $normalize): iterable |
| 49 | + { |
| 50 | + foreach (self::defaultCastDataProviderSamples() as $value => $_) { |
| 51 | + yield $value => $normalize === true |
| 52 | + ? match (true) { |
| 53 | + \is_object($value) => match (true) { |
| 54 | + // Unit enum coverts to enum's name |
| 55 | + $value === UnitEnumStub::ExampleCase => $value->name, |
| 56 | + // Int backed enum coverts to enum's int value |
| 57 | + $value === IntBackedEnumStub::ExampleCase => $value->value, |
| 58 | + // String backed enum coverts to enum's string value |
| 59 | + $value === StringBackedEnumStub::ExampleCase => $value->value, |
| 60 | + // Empty object converts to empty array |
| 61 | + $value == (object)[] => [], |
| 62 | + // Object with 1 property converts to hash-map |
| 63 | + $value == (object)['key' => 'val'] => ['key' => 'val'], |
| 64 | + // Object without named properties converts to list<string> |
| 65 | + $value == (object)['val'] => ['val'], |
| 66 | + default => $value, |
| 67 | + }, |
| 68 | + default => $value, |
| 69 | + } |
| 70 | + : match (true) { |
| 71 | + // Denormalization does not supports enums |
| 72 | + $value === UnitEnumStub::ExampleCase => new \ValueError('Passed value "ExampleCase" is invalid'), |
| 73 | + $value === IntBackedEnumStub::ExampleCase => new \ValueError('Passed value 3735928559 is invalid'), |
| 74 | + $value === StringBackedEnumStub::ExampleCase => new \ValueError('Passed value "case" is invalid'), |
| 75 | + default => $value, |
| 76 | + }; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments