|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Nuxtifyts\PhpDto\Tests\Unit\Support; |
| 4 | + |
| 5 | +use Nuxtifyts\PhpDto\Serializers\BackedEnumSerializer; |
| 6 | +use Nuxtifyts\PhpDto\Serializers\ScalarTypeSerializer; |
| 7 | +use Nuxtifyts\PhpDto\Serializers\Serializer; |
| 8 | +use Nuxtifyts\PhpDto\Support\Arr; |
| 9 | +use Nuxtifyts\PhpDto\Tests\Unit\UnitCase; |
| 10 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 11 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 12 | +use PHPUnit\Framework\Attributes\Test; |
| 13 | + |
| 14 | +#[CoversClass(Arr::class)] |
| 15 | +final class ArrTest extends UnitCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @param array<string, mixed> $parameters |
| 19 | + */ |
| 20 | + #[Test] |
| 21 | + #[DataProvider('get_arr_provider')] |
| 22 | + #[DataProvider('is_array_of_class_strings_provider')] |
| 23 | + public function test_arr_helper_functions( |
| 24 | + string $functionName, |
| 25 | + array $parameters, |
| 26 | + mixed $expected |
| 27 | + ): void { |
| 28 | + self::assertTrue(method_exists(Arr::class, $functionName)); |
| 29 | + self::assertEquals( |
| 30 | + $expected, |
| 31 | + Arr::{$functionName}(...$parameters) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return array<string, mixed> |
| 37 | + */ |
| 38 | + public static function get_arr_provider(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + 'get array existing key, invalid value' => [ |
| 42 | + 'getArray', |
| 43 | + [ |
| 44 | + 'array' => ['key' => 'value'], |
| 45 | + 'key' => 'key', |
| 46 | + ], |
| 47 | + [], |
| 48 | + ], |
| 49 | + 'get array existing key, valid value' => [ |
| 50 | + 'getArray', |
| 51 | + [ |
| 52 | + 'array' => ['key' => ['value']], |
| 53 | + 'key' => 'key', |
| 54 | + ], |
| 55 | + ['value'], |
| 56 | + ], |
| 57 | + 'get array non-existing key' => [ |
| 58 | + 'getArray', |
| 59 | + [ |
| 60 | + 'array' => ['key' => 'value'], |
| 61 | + 'key' => 'nonExistingKey', |
| 62 | + ], |
| 63 | + [], |
| 64 | + ], |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return array<string, mixed> |
| 70 | + */ |
| 71 | + public static function is_array_of_class_strings_provider(): array |
| 72 | + { |
| 73 | + return [ |
| 74 | + 'is array of class strings, valid' => [ |
| 75 | + 'isArrayOfClassStrings', |
| 76 | + [ |
| 77 | + 'array' => [ |
| 78 | + ScalarTypeSerializer::class, |
| 79 | + BackedEnumSerializer::class, |
| 80 | + ], |
| 81 | + 'classString' => Serializer::class, |
| 82 | + ], |
| 83 | + true, |
| 84 | + ], |
| 85 | + 'is array of class strings, invalid' => [ |
| 86 | + 'isArrayOfClassStrings', |
| 87 | + [ |
| 88 | + 'array' => [ |
| 89 | + ScalarTypeSerializer::class, |
| 90 | + BackedEnumSerializer::class, |
| 91 | + 'invalid', |
| 92 | + ], |
| 93 | + 'classString' => Serializer::class, |
| 94 | + ], |
| 95 | + false, |
| 96 | + ], |
| 97 | + ]; |
| 98 | + } |
| 99 | +} |
0 commit comments