From 9e043614787baf3b7b597eb7a0087e1f8538913b Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Fri, 27 Dec 2024 19:19:30 +0100 Subject: [PATCH] test: add mapping errors check method for consistency between tests --- tests/Integration/IntegrationTestCase.php | 33 ++++++ .../Mapping/Closure/ArgumentsMappingTest.php | 10 +- .../ConstructorRegistrationMappingTest.php | 14 +-- ...EnumConstructorRegistrationMappingTest.php | 14 +-- .../Mapping/EnumValueOfMappingTest.php | 5 +- .../Mapping/ExceptionFilteringTest.php | 7 +- .../Mapping/InterfaceInferringMappingTest.php | 20 ++-- .../Mapping/Object/ArrayValuesMappingTest.php | 21 ++-- .../Object/ConstantValuesMappingTest.php | 14 +-- .../Mapping/Object/DateTimeMappingTest.php | 27 ++--- .../Object/DateTimeZoneMappingTest.php | 6 +- .../Mapping/Object/EnumValuesMappingTest.php | 36 +++--- .../Mapping/Object/ListValuesMappingTest.php | 28 ++--- .../Mapping/Object/NullableMappingTest.php | 7 +- .../Object/ObjectValuesMappingTest.php | 24 ++-- .../Object/ScalarValuesMappingTest.php | 12 +- .../Object/ShapedArrayValuesMappingTest.php | 30 +++-- .../Mapping/Object/UnionValuesMappingTest.php | 7 +- .../Mapping/Other/ArrayMappingTest.php | 48 ++++---- .../Mapping/Other/ShapedArrayMappingTest.php | 17 ++- .../Mapping/Other/StrictMappingTest.php | 110 ++++++++---------- .../Mapping/SingleNodeMappingTest.php | 12 +- .../Integration/Mapping/UnionMappingTest.php | 28 ++--- .../Mapping/VariadicParameterMappingTest.php | 6 +- 24 files changed, 264 insertions(+), 272 deletions(-) diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index 41d58ca1..0931db32 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -6,12 +6,14 @@ use CuyZ\Valinor\Cache\FileSystemCache; use CuyZ\Valinor\Mapper\MappingError; +use CuyZ\Valinor\Mapper\Tree\Message\Messages; use CuyZ\Valinor\Mapper\Tree\Node; use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\Mapping\Namespace\NamespacedInterfaceInferringTest; use PHPUnit\Framework\TestCase; use Psr\SimpleCache\CacheInterface; +use function array_keys; use function bin2hex; use function implode; use function iterator_to_array; @@ -65,6 +67,37 @@ protected function mapperBuilder(): MapperBuilder return $builder; } + /** + * @param non-empty-array $expected + */ + protected function assertMappingErrors(MappingError $error, array $expected): void + { + $errors = []; + + foreach (Messages::flattenFromNode($error->node()) as $message) { + $errors[$message->node()->path()] = $message; + } + + $remainingErrors = $errors; + + foreach ($expected as $path => $message) { + self::assertArrayHasKey($path, $remainingErrors, "Error path `$path` not found in error messages, the following path(s) were found: `" . implode('`, `', array_keys($errors)) . '`.'); + + if (! preg_match('/^\[([^]]+)] (.*)/', $message, $matches)) { + self::fail('Incorrect error message format. Expected format: `[code] message`.'); + } + + self::assertSame($matches[1], $remainingErrors[$path]->code()); + self::assertSame($matches[2], $remainingErrors[$path]->toString()); + + unset($remainingErrors[$path]); + } + + if ($remainingErrors !== []) { + self::fail('Untested error messages at path(s): `' . implode('`, `', array_keys($remainingErrors)) . '`'); + } + } + protected function mappingFail(MappingError $error): never { $errorFinder = static function (Node $node, callable $errorFinder) { diff --git a/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php b/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php index 575388c2..4303ef16 100644 --- a/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php +++ b/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php @@ -80,8 +80,10 @@ public function test_invalid_source_with_one_error_throws_mapping_error(): void } catch (MappingError $exception) { self::assertMatchesRegularExpression('/Could not map arguments of `[^`]+` with value array{foo: false, bar: false}. A total of 2 errors were encountered./', $exception->getMessage()); - self::assertSame('Value false is not a valid string.', (string)$exception->node()->children()['foo']->messages()[0]); - self::assertSame('Value false is not a valid integer.', (string)$exception->node()->children()['bar']->messages()[0]); + self::assertMappingErrors($exception, [ + 'foo' => '[unknown] Value false is not a valid string.', + 'bar' => '[unknown] Value false is not a valid integer.', + ]); } } @@ -97,7 +99,9 @@ public function test_invalid_source_with_two_errors_throws_mapping_error(): void } catch (MappingError $exception) { self::assertMatchesRegularExpression('/Could not map arguments of `[^`]+`. An error occurred at path foo: Value false is not a valid string./', $exception->getMessage()); - self::assertSame('Value false is not a valid string.', (string)$exception->node()->children()['foo']->messages()[0]); + self::assertMappingErrors($exception, [ + 'foo' => '[unknown] Value false is not a valid string.', + ]); } } } diff --git a/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php b/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php index 568ffc4e..94b8f24c 100644 --- a/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php +++ b/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php @@ -673,10 +673,9 @@ public function test_source_not_matching_registered_constructors_throws_exceptio ->mapper() ->map(stdClass::class, []); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1642183169', $error->code()); - self::assertSame('Value array (empty) does not match any of `string`, `array{bar: int, baz?: float}`.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1642183169] Value array (empty) does not match any of `string`, `array{bar: int, baz?: float}`.", + ]); } } @@ -813,10 +812,9 @@ public function test_registered_constructor_throwing_exception_fails_mapping_wit self::fail('No mapping error when one was expected'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1656076090', $error->code()); - self::assertSame('some error message', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1656076090] some error message", + ]); } } diff --git a/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php b/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php index c4ab4d73..05ac0ed2 100644 --- a/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php +++ b/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php @@ -122,10 +122,9 @@ public function test_register_internal_from_constructor_is_overridden_by_library ->mapper() ->map(BackedStringEnum::class, 'fiz'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame("Value 'fiz' does not match any of 'foo', 'bar', 'baz'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value 'fiz' does not match any of 'foo', 'bar', 'baz'.", + ]); } } @@ -137,10 +136,9 @@ public function test_register_internal_try_from_constructor_is_overridden_by_lib ->mapper() ->map(BackedStringEnum::class, 'fiz'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame("Value 'fiz' does not match any of 'foo', 'bar', 'baz'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value 'fiz' does not match any of 'foo', 'bar', 'baz'.", + ]); } } } diff --git a/tests/Integration/Mapping/EnumValueOfMappingTest.php b/tests/Integration/Mapping/EnumValueOfMappingTest.php index fd3875ec..2949223e 100644 --- a/tests/Integration/Mapping/EnumValueOfMappingTest.php +++ b/tests/Integration/Mapping/EnumValueOfMappingTest.php @@ -68,8 +68,9 @@ public function test_array_keys_using_value_of_error(): void ->mapper() ->map('array, string>', ['oof' => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()['oof']->messages()[0]; - self::assertSame("Key 'oof' does not match type `'FOO'|'FOZ'|'BAZ'`.", (string)$error); + self::assertMappingErrors($exception, [ + 'oof' => "[1630946163] Key 'oof' does not match type `'FOO'|'FOZ'|'BAZ'`.", + ]); } } } diff --git a/tests/Integration/Mapping/ExceptionFilteringTest.php b/tests/Integration/Mapping/ExceptionFilteringTest.php index 3dc169f5..38ece205 100644 --- a/tests/Integration/Mapping/ExceptionFilteringTest.php +++ b/tests/Integration/Mapping/ExceptionFilteringTest.php @@ -36,10 +36,9 @@ public function test_userland_exception_filtered_is_caught_and_added_to_mapping_ ->mapper() ->map(ClassThatThrowsExceptionIfInvalidValue::class, 'bar'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1657197780', $error->code()); - self::assertSame('some error message', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1657197780] some error message", + ]); } } } diff --git a/tests/Integration/Mapping/InterfaceInferringMappingTest.php b/tests/Integration/Mapping/InterfaceInferringMappingTest.php index 159ee3ab..7c9f2f7d 100644 --- a/tests/Integration/Mapping/InterfaceInferringMappingTest.php +++ b/tests/Integration/Mapping/InterfaceInferringMappingTest.php @@ -351,10 +351,9 @@ public function test_invalid_source_throws_exception(): void ->mapper() ->map(SomeInterface::class, 42); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1632903281', $error->code()); - self::assertSame('Value 42 does not match type `array{type: string, key: int}`.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1632903281] Value 42 does not match type `array{type: string, key: int}`.", + ]); } } @@ -370,9 +369,9 @@ public function test_invalid_source_value_throws_exception(): void ->mapper() ->map(SomeInterface::class, 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->children()['key']->messages()[0]; - - self::assertSame("Value 'foo' is not a valid integer.", (string)$error); + self::assertMappingErrors($exception, [ + 'key' => "[unknown] Value 'foo' is not a valid integer.", + ]); } } @@ -391,10 +390,9 @@ public function test_superfluous_values_throws_exception(): void 'superfluousValue' => 'bar', ]); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1655117782', $error->code()); - self::assertSame('Unexpected key(s) `superfluousValue`, expected `valueA`.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1655117782] Unexpected key(s) `superfluousValue`, expected `valueA`.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php b/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php index c8fca1db..982e7259 100644 --- a/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php @@ -69,27 +69,22 @@ public function test_values_are_mapped_properly(): void public function test_empty_array_in_non_empty_array_throws_exception(): void { try { - $this->mapperBuilder()->mapper()->map(ArrayValues::class, [ - 'nonEmptyArraysOfStrings' => [], - ]); + $this->mapperBuilder()->mapper()->map('non-empty-array', []); } catch (MappingError $exception) { - $error = $exception->node()->children()['nonEmptyArraysOfStrings']->messages()[0]; - - self::assertSame('1630678334', $error->code()); - self::assertSame('Value array (empty) does not match type `non-empty-array`.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[1630678334] Value array (empty) does not match type `non-empty-array`.', + ]); } } public function test_value_with_invalid_type_throws_exception(): void { try { - $this->mapperBuilder()->mapper()->map(ArrayValues::class, [ - 'integers' => ['foo'], - ]); + $this->mapperBuilder()->mapper()->map('array', ['foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()['integers']->children()[0]->messages()[0]; - - self::assertSame("Value 'foo' is not a valid integer.", (string)$error); + self::assertMappingErrors($exception, [ + '0' => "[unknown] Value 'foo' is not a valid integer.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php b/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php index 67c32a2e..4218cf43 100644 --- a/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php @@ -70,10 +70,9 @@ public function test_private_constant_cannot_be_mapped(): void ->mapper() ->map(ObjectWithConstants::class . '::CONST_WITH_STRING_*', 'some private string value'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame("Value 'some private string value' does not match any of 'some string value', 'another string value'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value 'some private string value' does not match any of 'some string value', 'another string value'.", + ]); } } @@ -84,10 +83,9 @@ public function test_constant_not_matching_pattern_cannot_be_mapped(): void ->mapper() ->map(ObjectWithConstants::class . '::CONST_WITH_STRING_*', 'some prefixed string value'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame("Value 'some prefixed string value' does not match any of 'some string value', 'another string value'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value 'some prefixed string value' does not match any of 'some string value', 'another string value'.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/DateTimeMappingTest.php b/tests/Integration/Mapping/Object/DateTimeMappingTest.php index 4c454864..7c7fbd0e 100644 --- a/tests/Integration/Mapping/Object/DateTimeMappingTest.php +++ b/tests/Integration/Mapping/Object/DateTimeMappingTest.php @@ -17,9 +17,9 @@ public function test_default_datetime_constructor_cannot_be_used(): void ->mapper() ->map(DateTimeInterface::class, ['datetime' => '2022/08/05', 'timezone' => 'Europe/Paris']); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value array{datetime: '2022/08/05', timezone: 'Europe/Paris'} does not match any of `non-empty-string`, `int`, `float`.", + ]); } } @@ -139,10 +139,9 @@ public function test_default_date_constructor_with_invalid_source_throws_excepti ->mapper() ->map(DateTimeInterface::class, 'invalid datetime'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1630686564', $error->code()); - self::assertSame("Value 'invalid datetime' does not match any of the following formats: `Y-m-d\TH:i:sP`, `Y-m-d\TH:i:s.uP`, `U`, `U.u`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1630686564] Value 'invalid datetime' does not match any of the following formats: `Y-m-d\TH:i:sP`, `Y-m-d\TH:i:s.uP`, `U`, `U.u`.", + ]); } } @@ -154,10 +153,9 @@ public function test_registered_date_constructor_with_invalid_source_throws_exce ->mapper() ->map(DateTimeInterface::class, 'invalid datetime'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1630686564', $error->code()); - self::assertSame("Value 'invalid datetime' does not match any of the following formats: `Y/m/d`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1630686564] Value 'invalid datetime' does not match any of the following formats: `Y/m/d`.", + ]); } } @@ -170,10 +168,9 @@ public function test_date_constructor_with_overridden_format_source_throws_excep ->mapper() ->map(DateTimeInterface::class, '1971-11-08'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1630686564', $error->code()); - self::assertSame("Value '1971-11-08' does not match any of the following formats: `d/m/Y`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1630686564] Value '1971-11-08' does not match any of the following formats: `d/m/Y`.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php b/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php index ba86d164..3c4dbc4f 100644 --- a/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php +++ b/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php @@ -68,9 +68,9 @@ public function test_invalid_timezone_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(DateTimeZone::class, 'Jupiter/Europa'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value 'Jupiter/Europa' is not a valid timezone.", $error->toString()); + self::assertMappingErrors($exception, [ + '*root*' => "[unknown] Value 'Jupiter/Europa' is not a valid timezone.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/EnumValuesMappingTest.php b/tests/Integration/Mapping/Object/EnumValuesMappingTest.php index cd0e667f..8a61cd98 100644 --- a/tests/Integration/Mapping/Object/EnumValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/EnumValuesMappingTest.php @@ -47,9 +47,9 @@ public function test_invalid_string_enum_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(BackedStringEnum::class, new StringableObject('fiz')); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('Value object(' . StringableObject::class . ") does not match any of 'foo', 'bar', 'baz'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[1607027306] Value object(' . StringableObject::class . ") does not match any of 'foo', 'bar', 'baz'.", + ]); } } @@ -58,9 +58,9 @@ public function test_invalid_integer_enum_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(BackedIntegerEnum::class, '512'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value '512' does not match any of 42, 404, 1337.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value '512' does not match any of 42, 404, 1337.", + ]); } } @@ -69,9 +69,9 @@ public function test_value_not_matching_pure_enum_case_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(PureEnum::class . '::FOO', 'fiz'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value 'fiz' does not match string value 'FOO'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[unknown] Value 'fiz' does not match string value 'FOO'.", + ]); } } @@ -80,9 +80,9 @@ public function test_value_not_matching_backed_integer_enum_case_throws_exceptio try { $this->mapperBuilder()->mapper()->map(BackedIntegerEnum::class . '::FOO', '512'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value '512' does not match integer value 42.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[unknown] Value '512' does not match integer value 42.", + ]); } } @@ -91,9 +91,9 @@ public function test_value_not_filled_for_pure_enum_case_throws_exception(): voi try { $this->mapperBuilder()->mapper()->map('array{foo: ' . PureEnum::class . '::FOO}', []); } catch (MappingError $exception) { - $error = $exception->node()->children()['foo']->messages()[0]; - - self::assertSame('Cannot be empty and must be filled with a value matching type `FOO`.', (string)$error); + self::assertMappingErrors($exception, [ + 'foo' => '[1655449641] Cannot be empty and must be filled with a value matching type `FOO`.', + ]); } } @@ -102,9 +102,9 @@ public function test_value_not_filled_for_backed_integer_enum_case_throws_except try { $this->mapperBuilder()->mapper()->map('array{foo: ' . BackedIntegerEnum::class . '::FOO}', []); } catch (MappingError $exception) { - $error = $exception->node()->children()['foo']->messages()[0]; - - self::assertSame('Cannot be empty and must be filled with a value matching type `42`.', (string)$error); + self::assertMappingErrors($exception, [ + 'foo' => '[1655449641] Cannot be empty and must be filled with a value matching type `42`.', + ]); } } } diff --git a/tests/Integration/Mapping/Object/ListValuesMappingTest.php b/tests/Integration/Mapping/Object/ListValuesMappingTest.php index 6f1d3ef6..b6fa88ed 100644 --- a/tests/Integration/Mapping/Object/ListValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ListValuesMappingTest.php @@ -49,14 +49,11 @@ public function test_values_are_mapped_properly(): void public function test_empty_list_in_non_empty_list_throws_exception(): void { try { - $this->mapperBuilder()->mapper()->map(ListValues::class, [ - 'nonEmptyListOfStrings' => [], - ]); + $this->mapperBuilder()->mapper()->map('non-empty-list', []); } catch (MappingError $exception) { - $error = $exception->node()->children()['nonEmptyListOfStrings']->messages()[0]; - - self::assertSame('1630678334', $error->code()); - self::assertSame('Value array (empty) does not match type `non-empty-list`.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1630678334] Value array (empty) does not match type `non-empty-list`.", + ]); } } @@ -68,23 +65,20 @@ public function test_map_array_with_non_sequential_keys_to_list_throws_exception 2 => 'bar', ]); } catch (MappingError $exception) { - $error = $exception->node()->children()[2]->messages()[0]; - - self::assertSame('1654273010', $error->code()); - self::assertSame('Invalid sequential key 2, expected 1.', (string)$error); + self::assertMappingErrors($exception, [ + '2' => '[1654273010] Invalid sequential key 2, expected 1.', + ]); } } public function test_value_with_invalid_type_throws_exception(): void { try { - $this->mapperBuilder()->mapper()->map(ListValues::class, [ - 'integers' => ['foo'], - ]); + $this->mapperBuilder()->mapper()->map('list', ['foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()['integers']->children()['0']->messages()[0]; - - self::assertSame("Value 'foo' is not a valid integer.", (string)$error); + self::assertMappingErrors($exception, [ + '0' => "[unknown] Value 'foo' is not a valid integer.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/NullableMappingTest.php b/tests/Integration/Mapping/Object/NullableMappingTest.php index 4a244206..081b2018 100644 --- a/tests/Integration/Mapping/Object/NullableMappingTest.php +++ b/tests/Integration/Mapping/Object/NullableMappingTest.php @@ -39,10 +39,9 @@ public function test_string_value_mapped_in_null_throws_exception(): void self::fail('No mapping error when one was expected'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1710263908', $error->code()); - self::assertSame("Value 'foo' is not null.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1710263908] Value 'foo' is not null.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php b/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php index 0fc8dcd2..badfeca1 100644 --- a/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php @@ -37,10 +37,9 @@ public function test_invalid_iterable_source_throws_exception(): void try { $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1632903281', $error->code()); - self::assertSame("Value 'foo' does not match type `array{object: ?, string: string}`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1632903281] Value 'foo' does not match type `array{object: ?, string: string}`.", + ]); } } } @@ -56,12 +55,10 @@ public function test_superfluous_values_throws_exception_and_keeps_nested_errors 42 => 'baz', ]); } catch (MappingError $exception) { - $rootError = $exception->node()->messages()[0]; - $nestedError = $exception->node()->children()['stringA']->messages()[0]; - - self::assertSame('1655117782', $rootError->code()); - self::assertSame('Unexpected key(s) `unexpectedValueA`, `unexpectedValueB`, `42`, expected `stringA`, `stringB`.', (string)$rootError); - self::assertSame('Value 42 is not a valid string.', (string)$nestedError); + self::assertMappingErrors($exception, [ + '*root*' => "[1655117782] Unexpected key(s) `unexpectedValueA`, `unexpectedValueB`, `42`, expected `stringA`, `stringB`.", + 'stringA' => '[unknown] Value 42 is not a valid string.' + ]); } } @@ -70,10 +67,9 @@ public function test_object_with_no_argument_build_with_non_array_source_throws_ try { $this->mapperBuilder()->mapper()->map(stdClass::class, 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1632903281', $error->code()); - self::assertSame("Value 'foo' does not match type array.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1632903281] Value 'foo' does not match type array.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php b/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php index 47555218..df61dd9d 100644 --- a/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php @@ -93,9 +93,9 @@ public function test_value_with_invalid_type_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(SimpleObject::class, new stdClass()); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('Value object(stdClass) is not a valid string.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[unknown] Value object(stdClass) is not a valid string.', + ]); } } @@ -104,9 +104,9 @@ public function test_invalid_array_key_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('array-key', new stdClass()); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('Value object(stdClass) is not a valid array key.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[unknown] Value object(stdClass) is not a valid array key.', + ]); } } } diff --git a/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php b/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php index e1e23ade..7ea7bb59 100644 --- a/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php @@ -107,16 +107,14 @@ public function test_values_are_mapped_properly(): void public function test_value_with_invalid_type_throws_exception(): void { try { - $this->mapperBuilder()->mapper()->map(ShapedArrayValues::class, [ - 'basicShapedArrayWithStringKeys' => [ - 'foo' => new stdClass(), - 'bar' => 42, - ], + $this->mapperBuilder()->mapper()->map('array{foo: string, bar: int}', [ + 'foo' => new stdClass(), + 'bar' => 42, ]); } catch (MappingError $exception) { - $error = $exception->node()->children()['basicShapedArrayWithStringKeys']->children()['foo']->messages()[0]; - - self::assertSame('Value object(stdClass) is not a valid string.', (string)$error); + self::assertMappingErrors($exception, [ + 'foo' => '[unknown] Value object(stdClass) is not a valid string.', + ]); } } @@ -126,14 +124,14 @@ public function test_unsealed_shaped_array_invalid_key_throws_exception(): void $this->mapperBuilder()->mapper()->map( 'array{foo: string, ...array}', [ - 'foo' => new stdClass(), + 'foo' => 'foo', 'bar' => 'bar', ], ); } catch (MappingError $exception) { - $error = $exception->node()->children()['bar']->messages()[0]; - - self::assertSame("Key 'bar' does not match type `int`.", (string)$error); + self::assertMappingErrors($exception, [ + 'bar' => "[1630946163] Key 'bar' does not match type `int`.", + ]); } } @@ -143,14 +141,14 @@ public function test_unsealed_shaped_array_invalid_value_throws_exception(): voi $this->mapperBuilder()->mapper()->map( 'array{foo: string, ...array}', [ - 'foo' => new stdClass(), + 'foo' => 'foo', 'bar' => 'bar', ], ); } catch (MappingError $exception) { - $error = $exception->node()->children()['bar']->messages()[0]; - - self::assertSame("Value 'bar' is not a valid integer.", (string)$error); + self::assertMappingErrors($exception, [ + 'bar' => "[unknown] Value 'bar' is not a valid integer.", + ]); } } } diff --git a/tests/Integration/Mapping/Object/UnionValuesMappingTest.php b/tests/Integration/Mapping/Object/UnionValuesMappingTest.php index 1cdbe5ba..a7596bd0 100644 --- a/tests/Integration/Mapping/Object/UnionValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/UnionValuesMappingTest.php @@ -89,10 +89,9 @@ public function test_invalid_value_is_not_casted_when_casting_mode_is_disabled() try { $this->mapperBuilder()->mapper()->map('string|int', 42.404); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame('Value 42.404 does not match any of `string`, `int`.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[1607027306] Value 42.404 does not match any of `string`, `int`.', + ]); } } } diff --git a/tests/Integration/Mapping/Other/ArrayMappingTest.php b/tests/Integration/Mapping/Other/ArrayMappingTest.php index 0ac41371..c72625f0 100644 --- a/tests/Integration/Mapping/Other/ArrayMappingTest.php +++ b/tests/Integration/Mapping/Other/ArrayMappingTest.php @@ -119,9 +119,9 @@ public function test_value_with_invalid_integer_key_type_throws_exception(): voi try { $this->mapperBuilder()->mapper()->map('array', ['foo' => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()['foo']->messages()[0]; - - self::assertSame("Key 'foo' does not match type `int`.", (string)$error); + self::assertMappingErrors($exception, [ + 'foo' => "[1630946163] Key 'foo' does not match type `int`.", + ]); } } @@ -130,9 +130,9 @@ public function test_value_with_invalid_positive_integer_key_type_throws_excepti try { $this->mapperBuilder()->mapper()->map('array', [-42 => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()[-42]->messages()[0]; - - self::assertSame("Key -42 does not match type `positive-int`.", (string)$error); + self::assertMappingErrors($exception, [ + '-42' => "[1630946163] Key -42 does not match type `positive-int`.", + ]); } } @@ -141,9 +141,9 @@ public function test_value_with_invalid_negative_integer_key_type_throws_excepti try { $this->mapperBuilder()->mapper()->map('array', [42 => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()[42]->messages()[0]; - - self::assertSame("Key 42 does not match type `negative-int`.", (string)$error); + self::assertMappingErrors($exception, [ + '42' => '[1630946163] Key 42 does not match type `negative-int`.', + ]); } } @@ -152,9 +152,9 @@ public function test_value_with_invalid_integer_range_key_type_throws_exception( try { $this->mapperBuilder()->mapper()->map('array, string>', [-404 => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()[-404]->messages()[0]; - - self::assertSame("Key -404 does not match type `int<-42, 1337>`.", (string)$error); + self::assertMappingErrors($exception, [ + '-404' => '[1630946163] Key -404 does not match type `int<-42, 1337>`.', + ]); } } @@ -163,9 +163,9 @@ public function test_value_with_invalid_union_string_key_type_throws_exception() try { $this->mapperBuilder()->mapper()->map("array<'foo'|'bar', string>", ['baz' => 'baz']); } catch (MappingError $exception) { - $error = $exception->node()->children()['baz']->messages()[0]; - - self::assertSame("Key 'baz' does not match type `'foo'|'bar'`.", (string)$error); + self::assertMappingErrors($exception, [ + 'baz' => "[1630946163] Key 'baz' does not match type `'foo'|'bar'`.", + ]); } } @@ -174,9 +174,9 @@ public function test_value_with_invalid_union_integer_key_type_throws_exception( try { $this->mapperBuilder()->mapper()->map('array<42|1337, string>', [404 => 'baz']); } catch (MappingError $exception) { - $error = $exception->node()->children()[404]->messages()[0]; - - self::assertSame("Key 404 does not match type `42|1337`.", (string)$error); + self::assertMappingErrors($exception, [ + '404' => '[1630946163] Key 404 does not match type `42|1337`.', + ]); } } @@ -185,9 +185,9 @@ public function test_value_with_invalid_non_empty_string_key_type_throws_excepti try { $this->mapperBuilder()->mapper()->map('array', ['' => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()['']->messages()[0]; - - self::assertSame("Key '' does not match type `non-empty-string`.", (string)$error); + self::assertMappingErrors($exception, [ + '' => "[1630946163] Key '' does not match type `non-empty-string`.", + ]); } } @@ -196,9 +196,9 @@ public function test_value_with_invalid_class_string_key_type_throws_exception() try { $this->mapperBuilder()->mapper()->map('array', ['foo bar' => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()['foo bar']->messages()[0]; - - self::assertSame("Key 'foo bar' does not match type `class-string`.", (string)$error); + self::assertMappingErrors($exception, [ + 'foo bar' => "[1630946163] Key 'foo bar' does not match type `class-string`.", + ]); } } } diff --git a/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php b/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php index d82f7bfa..5fbb4fb6 100644 --- a/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php +++ b/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php @@ -52,10 +52,9 @@ public function test_missing_element_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('array{foo: string, bar: int}', ['foo' => 'foo']); } catch (MappingError $exception) { - $error = $exception->node()->children()['bar']->messages()[0]; - - self::assertSame('1655449641', $error->code()); - self::assertSame('Cannot be empty and must be filled with a value matching type `int`.', (string)$error); + self::assertMappingErrors($exception, [ + 'bar' => "[1655449641] Cannot be empty and must be filled with a value matching type `int`.", + ]); } } @@ -70,12 +69,10 @@ public function test_superfluous_values_throws_exception_and_keeps_nested_errors try { $this->mapperBuilder()->mapper()->map('array{foo: string, bar: int}', $source); } catch (MappingError $exception) { - $rootError = $exception->node()->messages()[0]; - $nestedError = $exception->node()->children()['foo']->messages()[0]; - - self::assertSame('1655117782', $rootError->code()); - self::assertSame('Unexpected key(s) `fiz`, expected `foo`, `bar`.', (string)$rootError); - self::assertSame('Value 404 is not a valid string.', (string)$nestedError); + self::assertMappingErrors($exception, [ + '*root*' => '[1655117782] Unexpected key(s) `fiz`, expected `foo`, `bar`.', + 'foo' => '[unknown] Value 404 is not a valid string.', + ]); } } } diff --git a/tests/Integration/Mapping/Other/StrictMappingTest.php b/tests/Integration/Mapping/Other/StrictMappingTest.php index 69332449..6875805c 100644 --- a/tests/Integration/Mapping/Other/StrictMappingTest.php +++ b/tests/Integration/Mapping/Other/StrictMappingTest.php @@ -28,10 +28,9 @@ public function test_missing_value_throws_exception(): void 'foo' => 'foo', ]); } catch (MappingError $exception) { - $error = $exception->node()->children()['bar']->messages()[0]; - - self::assertSame('1655449641', $error->code()); - self::assertSame('Cannot be empty and must be filled with a value matching type `string`.', (string)$error); + self::assertMappingErrors($exception, [ + 'bar' => '[1655449641] Cannot be empty and must be filled with a value matching type `string`.', + ]); } } @@ -85,9 +84,9 @@ public function test_null_that_cannot_be_cast_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('int', null); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('Value null is not a valid integer.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[unknown] Value null is not a valid integer.', + ]); } } @@ -96,9 +95,9 @@ public function test_invalid_float_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('float', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value 'foo' is not a valid float.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[unknown] Value 'foo' is not a valid float.", + ]); } } @@ -107,9 +106,9 @@ public function test_invalid_float_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('42.404', 1337); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('Value 1337 does not match float value 42.404.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[unknown] Value 1337 does not match float value 42.404.", + ]); } } @@ -118,9 +117,9 @@ public function test_invalid_integer_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('int', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value 'foo' is not a valid integer.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[unknown] Value 'foo' is not a valid integer.", + ]); } } @@ -129,9 +128,9 @@ public function test_invalid_integer_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('42', 1337); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('Value 1337 does not match integer value 42.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[unknown] Value 1337 does not match integer value 42.', + ]); } } @@ -140,9 +139,9 @@ public function test_invalid_integer_range_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('int<42, 1337>', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value 'foo' is not a valid integer between 42 and 1337.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[unknown] Value 'foo' is not a valid integer between 42 and 1337.", + ]); } } @@ -151,9 +150,9 @@ public function test_invalid_enum_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(PureEnum::class, 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value 'foo' does not match any of 'FOO', 'BAR', 'BAZ'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value 'foo' does not match any of 'FOO', 'BAR', 'BAZ'.", + ]); } } @@ -162,9 +161,9 @@ public function test_invalid_string_enum_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(BackedStringEnum::class, new stdClass()); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value object(stdClass) does not match any of 'foo', 'bar', 'baz'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value object(stdClass) does not match any of 'foo', 'bar', 'baz'.", + ]); } } @@ -173,9 +172,9 @@ public function test_invalid_integer_enum_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map(BackedIntegerEnum::class, false); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame("Value false does not match any of 42, 404, 1337.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value false does not match any of 42, 404, 1337.", + ]); } } @@ -184,10 +183,9 @@ public function test_invalid_union_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('bool|int|float', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame("Value 'foo' does not match any of `bool`, `int`, `float`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value 'foo' does not match any of `bool`, `int`, `float`.", + ]); } } @@ -196,10 +194,9 @@ public function test_invalid_utf8_union_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('bool|int|float', '🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame("Value '🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄…' does not match any of `bool`, `int`, `float`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Value '🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄…' does not match any of `bool`, `int`, `float`.", + ]); } } @@ -208,10 +205,9 @@ public function test_null_in_union_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('bool|int|float', null); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1607027306', $error->code()); - self::assertSame("Cannot be empty and must be filled with a value matching any of `bool`, `int`, `float`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1607027306] Cannot be empty and must be filled with a value matching any of `bool`, `int`, `float`.", + ]); } } @@ -220,10 +216,9 @@ public function test_invalid_array_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('array', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1618739163', $error->code()); - self::assertSame("Value 'foo' does not match type `array`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1618739163] Value 'foo' does not match type `array`.", + ]); } } @@ -232,10 +227,9 @@ public function test_invalid_list_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('list', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1618739163', $error->code()); - self::assertSame("Value 'foo' does not match type `list`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1618739163] Value 'foo' does not match type `list`.", + ]); } } @@ -244,10 +238,9 @@ public function test_invalid_shaped_array_value_throws_exception(): void try { $this->mapperBuilder()->mapper()->map('array{foo: string}', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1618739163', $error->code()); - self::assertSame("Value 'foo' does not match type `array{foo: string}`.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1618739163] Value 'foo' does not match type `array{foo: string}`.", + ]); } } @@ -256,10 +249,9 @@ public function test_invalid_shaped_array_with_object_value_throws_exception(): try { $this->mapperBuilder()->mapper()->map('array{foo: stdClass}', 'foo'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1618739163', $error->code()); - self::assertSame("Invalid value 'foo'.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1618739163] Invalid value 'foo'.", + ]); } } } diff --git a/tests/Integration/Mapping/SingleNodeMappingTest.php b/tests/Integration/Mapping/SingleNodeMappingTest.php index e93a55f9..4e86fa06 100644 --- a/tests/Integration/Mapping/SingleNodeMappingTest.php +++ b/tests/Integration/Mapping/SingleNodeMappingTest.php @@ -61,9 +61,9 @@ public function test_single_argument_invalid_value_with_key_present_in_source_ke try { $this->mapperBuilder()->mapper()->map(SimpleObject::class, ['value' => 42]); } catch (MappingError $exception) { - $error = $exception->node()->children()['value']->messages()[0]; - - self::assertSame('Value 42 is not a valid string.', (string)$error); + self::assertMappingErrors($exception, [ + 'value' => '[unknown] Value 42 is not a valid string.', + ]); } } @@ -72,9 +72,9 @@ public function test_single_argument_invalid_value_with_key_not_present_in_sourc try { $this->mapperBuilder()->mapper()->map(SimpleObject::class, 42); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('Value 42 is not a valid string.', (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => '[unknown] Value 42 is not a valid string.', + ]); } } diff --git a/tests/Integration/Mapping/UnionMappingTest.php b/tests/Integration/Mapping/UnionMappingTest.php index e840a98c..ceeac6e5 100644 --- a/tests/Integration/Mapping/UnionMappingTest.php +++ b/tests/Integration/Mapping/UnionMappingTest.php @@ -306,10 +306,9 @@ public function test_scalar_value_matching_two_objects_in_union_throws_exception self::fail('No mapping error when one was expected'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1710262975', $error->code()); - self::assertSame("Invalid value 'foo', it matches two or more types from union: cannot take a decision.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1710262975] Invalid value 'foo', it matches two or more types from union: cannot take a decision.", + ]); } } @@ -326,10 +325,9 @@ public function test_array_value_matching_two_objects_in_union_throws_exception( self::fail('No mapping error when one was expected'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1710262975', $error->code()); - self::assertSame("Invalid value array{string: 'foo'}, it matches two or more types from union: cannot take a decision.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1710262975] Invalid value array{string: 'foo'}, it matches two or more types from union: cannot take a decision.", + ]); } } @@ -345,10 +343,9 @@ public function test_array_value_matching_two_arrays_in_union_throws_exception() self::fail('No mapping error when one was expected'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1710262975', $error->code()); - self::assertSame("Invalid value array{0: 'foo', 1: 'bar'}, it matches two or more types from `array`, `array<'foo'|'bar'>`: cannot take a decision.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1710262975] Invalid value array{0: 'foo', 1: 'bar'}, it matches two or more types from `array`, `array<'foo'|'bar'>`: cannot take a decision.", + ]); } } @@ -364,10 +361,9 @@ public function test_array_value_matching_array_shape_and_object_in_union_throws self::fail('No mapping error when one was expected'); } catch (MappingError $exception) { - $error = $exception->node()->messages()[0]; - - self::assertSame('1710262975', $error->code()); - self::assertSame("Invalid value array{string: 'foo'}, it matches two or more types from union: cannot take a decision.", (string)$error); + self::assertMappingErrors($exception, [ + '*root*' => "[1710262975] Invalid value array{string: 'foo'}, it matches two or more types from union: cannot take a decision.", + ]); } } } diff --git a/tests/Integration/Mapping/VariadicParameterMappingTest.php b/tests/Integration/Mapping/VariadicParameterMappingTest.php index dc6cc1b9..759ab126 100644 --- a/tests/Integration/Mapping/VariadicParameterMappingTest.php +++ b/tests/Integration/Mapping/VariadicParameterMappingTest.php @@ -42,9 +42,9 @@ public function test_constructor_with_variadic_parameters_with_dots_in_dot_block ->mapper() ->map(SomeClassWithVariadicParametersInDocBlock::class, ['']); } catch (MappingError $exception) { - $error = $exception->node()->children()[0]->messages()[0]; - - self::assertSame("Value '' is not a valid non-empty string.", (string)$error); + self::assertMappingErrors($exception, [ + 'values.0' => "[unknown] Value '' is not a valid non-empty string.", + ]); } }