|
| 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\Tests\Type\Stub\IntBackedEnumStub; |
| 10 | +use TypeLang\Mapper\Tests\Type\Stub\StringBackedEnumStub; |
| 11 | +use TypeLang\Mapper\Tests\Type\Stub\UnitEnumStub; |
| 12 | +use TypeLang\Mapper\Type\ArrayKeyType; |
| 13 | +use TypeLang\Mapper\Type\Coercer\ArrayKeyTypeCoercer; |
| 14 | +use TypeLang\Mapper\Type\StringType; |
| 15 | +use TypeLang\Mapper\Type\TypeInterface; |
| 16 | + |
| 17 | +#[Group('types')] |
| 18 | +#[CoversClass(ArrayKeyType::class)] |
| 19 | +#[CoversClass(ArrayKeyTypeCoercer::class)] |
| 20 | +final class StringTypeTest extends SymmetricTypeTestCase |
| 21 | +{ |
| 22 | + protected static function createType(): TypeInterface |
| 23 | + { |
| 24 | + return new StringType(); |
| 25 | + } |
| 26 | + |
| 27 | + protected static function matchValues(bool $strict): iterable |
| 28 | + { |
| 29 | + foreach (self::defaultMatchDataProviderSamples() as $value => $default) { |
| 30 | + yield $value => match (true) { |
| 31 | + $value === '9223372036854775808', |
| 32 | + $value === '9223372036854775807', |
| 33 | + $value === '42', |
| 34 | + $value === '1', |
| 35 | + $value === '0', |
| 36 | + $value === '-1', |
| 37 | + $value === '-42', |
| 38 | + $value === '-9223372036854775808', |
| 39 | + $value === '-9223372036854775809', |
| 40 | + $value === '9223372036854775808.0', |
| 41 | + $value === '9223372036854775807.0', |
| 42 | + $value === '42.5', |
| 43 | + $value === '42.0', |
| 44 | + $value === '1.0', |
| 45 | + $value === '0.0', |
| 46 | + $value === '-1.0', |
| 47 | + $value === '-42.0', |
| 48 | + $value === '-42.5', |
| 49 | + $value === '-9223372036854775808.0', |
| 50 | + $value === '-9223372036854775809.0', |
| 51 | + $value === 'true', |
| 52 | + $value === 'false', |
| 53 | + $value === 'non empty', |
| 54 | + $value === '' => true, |
| 55 | + default => $default, |
| 56 | + }; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + protected static function castValues(bool $strict): iterable |
| 61 | + { |
| 62 | + foreach (self::defaultCastDataProviderSamples() as $value => $default) { |
| 63 | + yield $value => match (true) { |
| 64 | + $value === '9223372036854775808' => '9223372036854775808', |
| 65 | + $value === '9223372036854775807' => '9223372036854775807', |
| 66 | + $value === '42' => '42', |
| 67 | + $value === '1' => '1', |
| 68 | + $value === '0' => '0', |
| 69 | + $value === '-1' => '-1', |
| 70 | + $value === '-42' => '-42', |
| 71 | + $value === '-9223372036854775808' => '-9223372036854775808', |
| 72 | + $value === '-9223372036854775809' => '-9223372036854775809', |
| 73 | + $value === '9223372036854775808.0' => '9223372036854775808.0', |
| 74 | + $value === '9223372036854775807.0' => '9223372036854775807.0', |
| 75 | + $value === '42.5' => '42.5', |
| 76 | + $value === '42.0' => '42.0', |
| 77 | + $value === '1.0' => '1.0', |
| 78 | + $value === '0.0' => '0.0', |
| 79 | + $value === '-1.0' => '-1.0', |
| 80 | + $value === '-42.0' => '-42.0', |
| 81 | + $value === '-42.5' => '-42.5', |
| 82 | + $value === '-9223372036854775808.0' => '-9223372036854775808.0', |
| 83 | + $value === '-9223372036854775809.0' => '-9223372036854775809.0', |
| 84 | + $value === 'true' => 'true', |
| 85 | + $value === 'false' => 'false', |
| 86 | + $value === 'non empty' => 'non empty', |
| 87 | + $value === '' => '', |
| 88 | + // Type casts |
| 89 | + $strict === false => match (true) { |
| 90 | + $value === \PHP_INT_MAX + 1 => '9223372036854775808.0', |
| 91 | + $value === \PHP_INT_MAX => '9223372036854775807', |
| 92 | + $value === 42 => '42', |
| 93 | + $value === 1 => '1', |
| 94 | + $value === 0 => '0', |
| 95 | + $value === -1 => '-1', |
| 96 | + $value === -42 => '-42', |
| 97 | + $value === \PHP_INT_MIN => '-9223372036854775808', |
| 98 | + $value === \PHP_INT_MIN - 1 => '-9223372036854775808.0', |
| 99 | + $value === 42.0 => '42.0', |
| 100 | + $value === 42.5 => '42.5', |
| 101 | + $value === 1.0 => '1.0', |
| 102 | + $value === 0.0 => '0.0', |
| 103 | + $value === -1.0 => '-1.0', |
| 104 | + $value === -42.0 => '-42.0', |
| 105 | + $value === -42.5 => '-42.5', |
| 106 | + $value === null => '', |
| 107 | + $value === true => 'true', |
| 108 | + $value === false => 'false', |
| 109 | + $value === \INF => 'inf', |
| 110 | + $value === -\INF => '-inf', |
| 111 | + \is_float($value) && \is_nan($value) => 'nan', |
| 112 | + \is_resource($value) => match (\get_resource_type($value)) { |
| 113 | + 'stream' => 'stream', |
| 114 | + default => $default, |
| 115 | + }, |
| 116 | + $value === IntBackedEnumStub::ExampleCase => (string) IntBackedEnumStub::ExampleCase->value, |
| 117 | + $value === StringBackedEnumStub::ExampleCase => StringBackedEnumStub::ExampleCase->value, |
| 118 | + $value === UnitEnumStub::ExampleCase => UnitEnumStub::ExampleCase->name, |
| 119 | + default => $default, |
| 120 | + }, |
| 121 | + default => $default, |
| 122 | + }; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments