|
5 | 5 | namespace TypeLang\Mapper\Tests; |
6 | 6 |
|
7 | 7 | use PHPUnit\Framework\TestCase as BaseTestCase; |
| 8 | +use TypeLang\Mapper\Context\Direction; |
| 9 | +use TypeLang\Mapper\Context\RootContext; |
| 10 | +use TypeLang\Mapper\Exception\Mapping\InvalidValueException; |
| 11 | +use TypeLang\Mapper\Platform\PlatformInterface; |
| 12 | +use TypeLang\Mapper\Platform\StandardPlatform; |
| 13 | +use TypeLang\Mapper\Runtime\Configuration; |
| 14 | +use TypeLang\Mapper\Runtime\ConfigurationInterface; |
| 15 | +use TypeLang\Mapper\Runtime\Extractor\NativeTypeExtractor; |
| 16 | +use TypeLang\Mapper\Runtime\Extractor\TypeExtractorInterface; |
| 17 | +use TypeLang\Mapper\Runtime\Parser\TypeLangParser; |
| 18 | +use TypeLang\Mapper\Runtime\Parser\TypeParserInterface; |
| 19 | +use TypeLang\Mapper\Runtime\Repository\TypeRepository; |
| 20 | +use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface; |
8 | 21 |
|
9 | | -abstract class TestCase extends BaseTestCase {} |
| 22 | +abstract class TestCase extends BaseTestCase |
| 23 | +{ |
| 24 | + private static int $dataProviderIndex = 0; |
| 25 | + |
| 26 | + protected static function dataProviderOf(iterable $data): iterable |
| 27 | + { |
| 28 | + foreach ($data as $value => $expected) { |
| 29 | + yield self::dataProviderKeyOf($value) => [$value, $expected]; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @return non-empty-string |
| 35 | + */ |
| 36 | + private static function dataProviderKeyOf(mixed $value): string |
| 37 | + { |
| 38 | + return \vsprintf('%s(%s)#%d', [ |
| 39 | + \get_debug_type($value), |
| 40 | + \is_array($value) || \is_object($value) ? \json_encode($value) : \var_export($value, true), |
| 41 | + ++self::$dataProviderIndex, |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + protected function createConfiguration(bool $strictTypes = true): ConfigurationInterface |
| 46 | + { |
| 47 | + return new Configuration( |
| 48 | + isStrictTypes: $strictTypes, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + protected function createTypeExtractor(): TypeExtractorInterface |
| 53 | + { |
| 54 | + return new NativeTypeExtractor(); |
| 55 | + } |
| 56 | + |
| 57 | + protected function createPlatform(): PlatformInterface |
| 58 | + { |
| 59 | + return new StandardPlatform(); |
| 60 | + } |
| 61 | + |
| 62 | + protected function createTypeParser(): TypeParserInterface |
| 63 | + { |
| 64 | + return TypeLangParser::createFromPlatform($this->createPlatform()); |
| 65 | + } |
| 66 | + |
| 67 | + protected function createTypeRepository(Direction $direction): TypeRepositoryInterface |
| 68 | + { |
| 69 | + $platform = $this->createPlatform(); |
| 70 | + |
| 71 | + return new TypeRepository( |
| 72 | + parser: $this->createTypeParser(), |
| 73 | + builders: $platform->getTypes($direction), |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + protected function createNormalizationContext(mixed $value, bool $strictTypes = true): RootContext |
| 78 | + { |
| 79 | + return RootContext::forNormalization( |
| 80 | + value: $value, |
| 81 | + config: $this->createConfiguration($strictTypes), |
| 82 | + extractor: $this->createTypeExtractor(), |
| 83 | + parser: $this->createTypeParser(), |
| 84 | + types: $this->createTypeRepository(Direction::Normalize), |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + protected function createDenormalizationContext(mixed $value, bool $strictTypes = true): RootContext |
| 89 | + { |
| 90 | + return RootContext::forDenormalization( |
| 91 | + value: $value, |
| 92 | + config: $this->createConfiguration($strictTypes), |
| 93 | + extractor: $this->createTypeExtractor(), |
| 94 | + parser: $this->createTypeParser(), |
| 95 | + types: $this->createTypeRepository(Direction::Denormalize), |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + protected function expectTypeErrorIfException(mixed $expected): void |
| 100 | + { |
| 101 | + if (!$expected instanceof \Throwable) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + $this->expectExceptionMessage($expected->getMessage()); |
| 106 | + $this->expectException(InvalidValueException::class); |
| 107 | + } |
| 108 | + |
| 109 | + protected static function assertIfNotException(mixed $expected, mixed $actual): void |
| 110 | + { |
| 111 | + switch (true) { |
| 112 | + case $expected instanceof \Throwable: |
| 113 | + break; |
| 114 | + case \is_array($expected): |
| 115 | + case \is_object($expected): |
| 116 | + self::assertEquals($expected, $actual); |
| 117 | + break; |
| 118 | + case \is_float($expected) && \is_nan($expected): |
| 119 | + self::assertNan($actual); |
| 120 | + break; |
| 121 | + default: |
| 122 | + self::assertSame($expected, $actual); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments