From 1688bc6b9a76e011cd5f0d3e2b984eb45b5a0712 Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Fri, 16 Feb 2024 00:07:24 +0100 Subject: [PATCH] test: run the whole integration test suite with & without cache injected This allows us to test the exact same test cases and assertions, but with a completely different code path (as the file cache is used only in one scenario). --- src/Cache/FileSystemCache.php | 13 ++++ .../Integration/Cache/CacheInjectionTest.php | 3 +- tests/Integration/Cache/CacheWarmupTest.php | 6 +- tests/Integration/IntegrationTestCase.php | 52 ++++++++++++++ .../Mapping/AnonymousClassMappingTest.php | 3 +- .../ClassInheritanceInferringMappingTest.php | 11 ++- .../ClassNameCollisionTestCollision.php | 3 +- .../Mapping/Closure/ArgumentsMappingTest.php | 13 ++-- .../ConstructorRegistrationMappingTest.php | 67 +++++++++---------- ...EnumConstructorRegistrationMappingTest.php | 17 +++-- .../Mapping/ExceptionFilteringTest.php | 5 +- .../Mapping/InterfaceInferringMappingTest.php | 47 +++++++------ .../Integration/Mapping/MappingErrorTest.php | 7 +- .../Mapping/Message/MessageFormatterTest.php | 3 +- ...onstructorWithReturnTypeInDocBlockTest.php | 3 +- .../Mapping/Object/ArrayValuesMappingTest.php | 7 +- .../Object/ConstantValuesMappingTest.php | 7 +- .../Mapping/Object/DateTimeMappingTest.php | 23 +++---- .../Object/DateTimeZoneMappingTest.php | 9 ++- .../Mapping/Object/EnumValuesMappingTest.php | 15 ++--- .../ExtendedDateIntervalMappingTest.php | 3 +- .../Mapping/Object/GenericInheritanceTest.php | 3 +- .../Object/GenericValuesMappingTest.php | 3 +- .../Object/IterableValuesMappingTest.php | 3 +- .../Mapping/Object/ListValuesMappingTest.php | 9 ++- .../Object/LocalTypeAliasMappingTest.php | 5 +- .../Mapping/Object/NullableMappingTest.php | 3 +- .../Object/ObjectValuesMappingTest.php | 9 ++- .../Object/ScalarValuesMappingTest.php | 5 +- .../Object/ShapedArrayValuesMappingTest.php | 5 +- .../SimpleNamespacedObjectMappingTest.php | 3 +- .../Object/UnionOfObjectsMappingTest.php | 7 +- .../Mapping/Object/UnionValuesMappingTest.php | 7 +- .../Mapping/Other/ArrayMappingTest.php | 33 +++++---- .../Other/FlexibleCastingMappingTest.php | 7 +- .../Other/PermissiveTypesMappingTest.php | 3 +- .../Mapping/Other/ShapedArrayMappingTest.php | 9 ++- .../Mapping/Other/StrictMappingTest.php | 43 ++++++------ .../Other/SuperfluousKeysMappingTest.php | 3 +- .../Mapping/ReadonlyMappingTest.php | 3 +- .../Mapping/SingleNodeMappingTest.php | 11 ++- .../Mapping/Source/JsonSourceMappingTest.php | 3 +- .../Modifier/CamelCaseKeysMappingTest.php | 3 +- .../Source/Modifier/PathMappingTest.php | 3 +- .../Integration/Mapping/Source/SourceTest.php | 3 +- .../Mapping/Source/YamlSourceMappingTest.php | 3 +- .../Integration/Mapping/UnionMappingTest.php | 9 ++- .../Mapping/ValueAlteringMappingTest.php | 7 +- .../Mapping/VariadicParameterMappingTest.php | 11 ++- .../CustomObjectNormalizationTest.php | 7 +- .../DateFormatFromAttributeTest.php | 7 +- .../DateFormatFromGlobalTransformerTest.php | 7 +- .../CommonExamples/IgnoreAttributeTest.php | 7 +- ...ObjectKeysToSnakeCaseFromAttributeTest.php | 7 +- .../ObjectKeysToSnakeCaseTest.php | 7 +- .../RenamePropertyFromAttributeTest.php | 7 +- .../UppercaseFromAttributeTest.php | 7 +- .../CommonExamples/VersionTransformerTest.php | 7 +- .../Integration/Normalizer/NormalizerTest.php | 51 +++++++------- .../Normalizer/StreamNormalizerTest.php | 3 +- tests/Unit/Cache/FileSystemCacheTest.php | 45 +++++++++---- 61 files changed, 362 insertions(+), 333 deletions(-) diff --git a/src/Cache/FileSystemCache.php b/src/Cache/FileSystemCache.php index 782908c1..b8b40f54 100644 --- a/src/Cache/FileSystemCache.php +++ b/src/Cache/FileSystemCache.php @@ -22,6 +22,7 @@ use function mkdir; use function random_bytes; use function rename; +use function rmdir; use function str_contains; use function unlink; use function var_export; @@ -124,22 +125,34 @@ public function clear(): bool } $success = true; + $shouldDeleteRootDir = true; /** @var FilesystemIterator $file */ foreach (new FilesystemIterator($this->cacheDir) as $file) { + if ($file->getFilename() === '.valinor.tmp') { + $success = @rmdir($this->cacheDir . DIRECTORY_SEPARATOR . $file->getFilename()) && $success; + continue; + } + if (! $file->isFile()) { + $shouldDeleteRootDir = false; continue; } $line = $file->openFile()->getCurrentLine(); if (! $line || ! str_contains($line, self::GENERATED_MESSAGE)) { + $shouldDeleteRootDir = false; continue; } $success = @unlink($this->cacheDir . DIRECTORY_SEPARATOR . $file->getFilename()) && $success; } + if ($shouldDeleteRootDir) { + $success = @rmdir($this->cacheDir) && $success; + } + return $success; } diff --git a/tests/Integration/Cache/CacheInjectionTest.php b/tests/Integration/Cache/CacheInjectionTest.php index 14f30bd3..d55ed777 100644 --- a/tests/Integration/Cache/CacheInjectionTest.php +++ b/tests/Integration/Cache/CacheInjectionTest.php @@ -7,7 +7,6 @@ use CuyZ\Valinor\Cache\FileSystemCache; use CuyZ\Valinor\Cache\FileWatchingCache; use CuyZ\Valinor\Mapper\TreeMapper; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use org\bovigo\vfs\vfsStream; @@ -53,7 +52,7 @@ public function test_cache_entries_are_written_once_during_mapping(): void */ private function createMapper(CacheInterface $cache): TreeMapper { - return (new MapperBuilder()) + return $this->mapperBuilder() ->withCache($cache) // The cache should be able to cache function definitions… ->registerConstructor(fn (): stdClass => new stdClass()) diff --git a/tests/Integration/Cache/CacheWarmupTest.php b/tests/Integration/Cache/CacheWarmupTest.php index 1d0377b6..17dc5a5c 100644 --- a/tests/Integration/Cache/CacheWarmupTest.php +++ b/tests/Integration/Cache/CacheWarmupTest.php @@ -22,13 +22,13 @@ protected function setUp(): void parent::setUp(); $this->cache = new FakeCache(); - $this->mapper = (new MapperBuilder())->withCache($this->cache); + $this->mapper = $this->mapperBuilder()->withCache($this->cache); } public function test_cache_warmup_is_called_only_once(): void { $cache = new FakeCacheWithWarmup(); - $mapper = (new MapperBuilder())->withCache($cache); + $mapper = $this->mapperBuilder()->withCache($cache); $mapper->warmup(); $mapper->warmup(); @@ -41,7 +41,7 @@ public function test_cache_warmup_is_called_only_once(): void */ public function test_cache_warmup_does_not_call_delegate_warmup_if_not_handled(): void { - $mapper = new MapperBuilder(); // no cache registered + $mapper = $this->mapperBuilder(); // no cache registered $mapper->warmup(); } diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index ce8ec0bc..41d58ca1 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -4,15 +4,67 @@ namespace CuyZ\Valinor\Tests\Integration; +use CuyZ\Valinor\Cache\FileSystemCache; use CuyZ\Valinor\Mapper\MappingError; 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 bin2hex; use function implode; use function iterator_to_array; +use function random_bytes; +use function sys_get_temp_dir; abstract class IntegrationTestCase extends TestCase { + /** @var CacheInterface */ + private CacheInterface $cacheToInject; + + /** + * After the test has run, it is run again with the cache injected. This + * allows us to test the exact same case and assertions, but with a + * completely different code path (as the file cache is used only in one + * scenario). + */ + protected function tearDown(): void + { + // Excluding this test because it cannot run twice in the same process. + if (static::class === NamespacedInterfaceInferringTest::class) { + return; + } + + $cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(16)); + $this->cacheToInject = new FileSystemCache($cacheDir); + + // First rerun of the test: the cache entries will be injected. + parent::runTest(); + + // Second rerun of the test: the cache entries will be used and tested. + parent::runTest(); + + $this->cacheToInject->clear(); + } + + /** + * This method *must* be used by every integration test in replacement of a + * direct creation of a MapperBuilder instance. The goal is to ensure that + * a test is run twice: once without a cache and once with the internal + * filesystem cache injected. + */ + protected function mapperBuilder(): MapperBuilder + { + $builder = new MapperBuilder(); + + if (isset($this->cacheToInject)) { + $builder = $builder->withCache($this->cacheToInject); + } + + return $builder; + } + protected function mappingFail(MappingError $error): never { $errorFinder = static function (Node $node, callable $errorFinder) { diff --git a/tests/Integration/Mapping/AnonymousClassMappingTest.php b/tests/Integration/Mapping/AnonymousClassMappingTest.php index b4d8cafe..6193ce01 100644 --- a/tests/Integration/Mapping/AnonymousClassMappingTest.php +++ b/tests/Integration/Mapping/AnonymousClassMappingTest.php @@ -2,7 +2,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class AnonymousClassMappingTest extends IntegrationTestCase @@ -14,7 +13,7 @@ public function test_map_to_string_or_anonymous_class_with_string_works_correctl public string $bar; }; - $res = (new MapperBuilder()) + $res = $this->mapperBuilder() ->mapper() ->map('string|' . $class::class, 'foo'); diff --git a/tests/Integration/Mapping/ClassInheritanceInferringMappingTest.php b/tests/Integration/Mapping/ClassInheritanceInferringMappingTest.php index f113b59c..209d1848 100644 --- a/tests/Integration/Mapping/ClassInheritanceInferringMappingTest.php +++ b/tests/Integration/Mapping/ClassInheritanceInferringMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\Tree\Exception\CannotInferFinalClass; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use LogicException; @@ -13,7 +12,7 @@ final class ClassInheritanceInferringMappingTest extends IntegrationTestCase { public function test_infer_abstract_class_works_as_expected(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer( SomeAbstractClass::class, fn () => SomeAbstractChildClass::class @@ -33,7 +32,7 @@ public function test_infer_abstract_class_works_as_expected(): void public function test_infer_abstract_class_with_argument_in_callback_works_as_expected(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer( SomeAbstractClass::class, /** @return class-string */ @@ -58,7 +57,7 @@ public function test_infer_abstract_class_with_argument_in_callback_works_as_exp public function test_infer_class_works_as_expected(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer( SomeParentClass::class, fn () => SomeChildClass::class @@ -78,7 +77,7 @@ public function test_infer_class_works_as_expected(): void public function test_infer_class_with_argument_in_callback_works_as_expected(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer( SomeParentClass::class, /** @return class-string */ @@ -107,7 +106,7 @@ public function test_infer_final_class_throws_exception(): void $this->expectExceptionCode(1671468163); $this->expectExceptionMessage('Cannot infer final class `' . SomeAbstractChildClass::class . '` with function'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer(SomeAbstractChildClass::class, fn () => SomeAbstractChildClass::class) ->mapper() ->map(SomeAbstractChildClass::class, []); diff --git a/tests/Integration/Mapping/ClassNameCollisionTestCollision.php b/tests/Integration/Mapping/ClassNameCollisionTestCollision.php index 66f734ec..49be7c20 100644 --- a/tests/Integration/Mapping/ClassNameCollisionTestCollision.php +++ b/tests/Integration/Mapping/ClassNameCollisionTestCollision.php @@ -3,7 +3,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\Error; @@ -12,7 +11,7 @@ final class ClassNameCollisionTestCollision extends IntegrationTestCase public function test_mapping_to_class_with_same_class_name_as_native_class_works_properly(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(ObjectWithErrorsClassNameCollision::class, ['foo', 'bar']); } catch (MappingError $error) { diff --git a/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php b/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php index d7b73e12..575388c2 100644 --- a/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php +++ b/tests/Integration/Mapping/Closure/ArgumentsMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Closure; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class ArgumentsMappingTest extends IntegrationTestCase @@ -15,7 +14,7 @@ public function test_can_map_to_anonymous_function(): void $function = fn (string $foo, int $bar): string => "$foo / $bar"; try { - $arguments = (new MapperBuilder())->argumentsMapper()->mapArguments($function, [ + $arguments = $this->mapperBuilder()->argumentsMapper()->mapArguments($function, [ 'foo' => 'foo', 'bar' => 42, ]); @@ -31,7 +30,7 @@ public function test_can_map_to_class_method(): void $object = new SomeClassWithMethods(); try { - $arguments = (new MapperBuilder())->argumentsMapper()->mapArguments($object->somePublicMethod(...), [ + $arguments = $this->mapperBuilder()->argumentsMapper()->mapArguments($object->somePublicMethod(...), [ 'foo' => 'foo', 'bar' => 42, ]); @@ -45,7 +44,7 @@ public function test_can_map_to_class_method(): void public function test_can_map_to_class_static_method(): void { try { - $arguments = (new MapperBuilder())->argumentsMapper()->mapArguments(SomeClassWithMethods::somePublicStaticMethod(...), [ + $arguments = $this->mapperBuilder()->argumentsMapper()->mapArguments(SomeClassWithMethods::somePublicStaticMethod(...), [ 'foo' => 'foo', 'bar' => 42, ]); @@ -61,7 +60,7 @@ public function test_can_map_to_function_with_single_argument(): void $function = fn (string $foo): string => $foo; try { - $arguments = (new MapperBuilder())->argumentsMapper()->mapArguments($function, ['foo' => 'foo']); + $arguments = $this->mapperBuilder()->argumentsMapper()->mapArguments($function, ['foo' => 'foo']); } catch (MappingError $error) { $this->mappingFail($error); } @@ -74,7 +73,7 @@ public function test_invalid_source_with_one_error_throws_mapping_error(): void $function = fn (string $foo, int $bar): string => "$foo / $bar"; try { - (new MapperBuilder())->argumentsMapper()->mapArguments($function, [ + $this->mapperBuilder()->argumentsMapper()->mapArguments($function, [ 'foo' => false, 'bar' => false, ]); @@ -91,7 +90,7 @@ public function test_invalid_source_with_two_errors_throws_mapping_error(): void $function = fn (string $foo, int $bar): string => "$foo / $bar"; try { - (new MapperBuilder())->argumentsMapper()->mapArguments($function, [ + $this->mapperBuilder()->argumentsMapper()->mapArguments($function, [ 'foo' => false, 'bar' => 42, ]); diff --git a/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php b/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php index 80c47ddc..893c502f 100644 --- a/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php +++ b/tests/Integration/Mapping/ConstructorRegistrationMappingTest.php @@ -12,7 +12,6 @@ use CuyZ\Valinor\Mapper\Object\Exception\InvalidConstructorReturnType; use CuyZ\Valinor\Mapper\Object\Exception\MissingConstructorClassTypeParameter; use CuyZ\Valinor\Mapper\Object\Exception\ObjectBuildersCollision; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; @@ -29,7 +28,7 @@ public function test_registered_anonymous_function_constructor_is_used(): void $object = new stdClass(); try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(fn (): stdClass => $object) ->mapper() ->map(stdClass::class, []); @@ -45,7 +44,7 @@ public function test_registered_static_anonymous_function_constructor_is_used(): $object = new stdClass(); try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(static fn (): stdClass => $object) ->mapper() ->map(stdClass::class, []); @@ -61,7 +60,7 @@ public function test_registered_anonymous_function_from_static_scope_constructor $object = new stdClass(); try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(SomeClassProvidingStaticClosure::getConstructor($object)) ->mapper() ->map(stdClass::class, []); @@ -77,7 +76,7 @@ public function test_registered_anonymous_function_constructor_with_docblock_is_ $object = new stdClass(); try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(/** @return stdClass */ fn () => $object) ->mapper() ->map(stdClass::class, []); @@ -91,7 +90,7 @@ public function test_registered_anonymous_function_constructor_with_docblock_is_ public function test_registered_named_constructor_is_used(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(SomeClassWithNamedConstructors::namedConstructor(...)) ->mapper() ->map(SomeClassWithNamedConstructors::class, 'foo'); @@ -115,7 +114,7 @@ public function __invoke(): stdClass }; try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor($constructor) ->mapper() ->map(stdClass::class, []); @@ -139,7 +138,7 @@ public function build(): stdClass }; try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor($constructor->build(...)) ->mapper() ->map(stdClass::class, []); @@ -153,7 +152,7 @@ public function build(): stdClass public function test_class_static_constructor_for_other_class_is_used(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(SomeClassWithStaticConstructorForOtherClass::from(...)) ->mapper() ->map(SimpleObject::class, 'foo'); @@ -167,7 +166,7 @@ public function test_class_static_constructor_for_other_class_is_used(): void public function test_registered_constructor_with_injected_class_name_is_used_for_abstract_class(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( /** * @param class-string $className @@ -193,7 +192,7 @@ public function test_registered_constructor_with_injected_class_name_is_used_for public function test_registered_constructor_with_injected_class_name_is_used_for_interface(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( /** * @param class-string $className @@ -221,7 +220,7 @@ public function test_registered_constructor_with_injected_class_name_without_cla try { $object = new stdClass(); - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( #[DynamicConstructor] fn (string $className, string $foo): stdClass => $object @@ -240,7 +239,7 @@ public function test_registered_constructor_with_injected_class_name_with_previo try { $object = new stdClass(); - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(fn (): DateTimeInterface => new DateTime()) ->registerConstructor( #[DynamicConstructor] @@ -258,7 +257,7 @@ public function test_registered_constructor_with_injected_class_name_with_previo public function test_native_constructor_is_not_called_if_not_registered_but_other_constructors_are_registered(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(SomeClassWithSimilarNativeConstructorAndNamedConstructor::namedConstructor(...)) ->mapper() ->map(SomeClassWithSimilarNativeConstructorAndNamedConstructor::class, 'foo'); @@ -272,7 +271,7 @@ public function test_native_constructor_is_not_called_if_not_registered_but_othe public function test_registered_native_constructor_is_called_if_registered_and_other_constructors_are_registered(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(SomeClassWithDifferentNativeConstructorAndNamedConstructor::class) ->registerConstructor(SomeClassWithDifferentNativeConstructorAndNamedConstructor::namedConstructor(...)) ->mapper() @@ -293,7 +292,7 @@ public function test_registered_constructor_is_used_when_not_the_first_nor_last_ $object = new stdClass(); try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(fn (): DateTime => new DateTime()) // This constructor is surrounded by other ones to ensure it is // still used correctly. @@ -311,7 +310,7 @@ public function test_registered_constructor_is_used_when_not_the_first_nor_last_ public function test_registered_constructor_with_one_argument_is_used(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(function (int $int): stdClass { $class = new stdClass(); $class->int = $int; @@ -330,7 +329,7 @@ public function test_registered_constructor_with_one_argument_is_used(): void public function test_registered_constructor_with_several_arguments_is_used(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(function (string $string, int $int, float $float = 1337.404): stdClass { $class = new stdClass(); $class->string = $string; @@ -355,7 +354,7 @@ public function test_registered_constructor_with_several_arguments_is_used(): vo public function test_registered_constructors_for_same_class_are_filtered_correctly(): void { - $mapper = (new MapperBuilder()) + $mapper = $this->mapperBuilder() // Basic constructor ->registerConstructor(function (string $foo): stdClass { $class = new stdClass(); @@ -413,7 +412,7 @@ public function test_registered_constructors_for_same_class_are_filtered_correct public function test_several_constructors_with_same_arguments_number_are_filtered_correctly(): void { - $mapper = (new MapperBuilder()) + $mapper = $this->mapperBuilder() ->registerConstructor(function (string $foo, string $bar): stdClass { $class = new stdClass(); $class->foo = $foo; @@ -458,7 +457,7 @@ public function test_inherited_static_constructor_is_used_to_map_child_class(): })::class; try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(SomeAbstractClassWithStaticConstructor::from(...)) ->mapper() ->map($class, [ @@ -481,7 +480,7 @@ public function test_identical_registered_constructors_with_no_argument_throws_e $this->expectExceptionCode(1654955787); $this->expectExceptionMessageMatches('/A collision was detected between the following constructors of the class `stdClass`: `Closure .*`, `Closure .*`\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor( fn (string $foo): stdClass => new stdClass(), fn (): stdClass => new stdClass(), @@ -497,7 +496,7 @@ public function test_identical_registered_constructors_with_one_argument_throws_ $this->expectExceptionCode(1654955787); $this->expectExceptionMessageMatches('/A collision was detected between the following constructors of the class `stdClass`: `Closure .*`, `Closure .*`\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor( fn (int $int): stdClass => new stdClass(), fn (float $float): stdClass => new stdClass(), @@ -512,7 +511,7 @@ public function test_identical_registered_constructors_with_several_argument_thr $this->expectExceptionCode(1654955787); $this->expectExceptionMessage('A collision was detected between the following constructors of the class `stdClass`: `CuyZ\Valinor\Tests\Integration\Mapping\constructorA()`, `CuyZ\Valinor\Tests\Integration\Mapping\constructorB()`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor( constructorA(...), fn (int $other, float $arguments): stdClass => new stdClass(), @@ -525,7 +524,7 @@ public function test_identical_registered_constructors_with_several_argument_thr public function test_source_not_matching_registered_constructors_throws_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor( fn (int $bar, float $baz = 1337.404): stdClass => new stdClass(), fn (string $foo): stdClass => new stdClass(), @@ -546,7 +545,7 @@ public function test_non_registered_named_constructors_are_ignored(): void $this->expectExceptionCode(1646916477); $this->expectExceptionMessage('No available constructor found for class `' . SomeClassWithPrivateNativeConstructor::class . '`'); - (new MapperBuilder()) + $this->mapperBuilder() ->mapper() ->map(SomeClassWithPrivateNativeConstructor::class, []); } @@ -557,7 +556,7 @@ public function test_invalid_constructor_return_type_throws_exception(): void $this->expectExceptionCode(1659446121); $this->expectExceptionMessageMatches('/Invalid return type `string` for constructor `.*`\, it must be a valid class name\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor(fn (): string => 'foo') ->mapper() ->map(stdClass::class, []); @@ -569,7 +568,7 @@ public function test_invalid_constructor_return_type_missing_generic_throws_exce $this->expectExceptionCode(1659446121); $this->expectExceptionMessageMatches('/The type `.*` for return type of method `.*` could not be resolved: No generic was assigned to the template\(s\) `T` for the class .*/'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor( fn (): SimpleObjectWithGeneric => new SimpleObjectWithGeneric() ) @@ -583,7 +582,7 @@ public function test_missing_constructor_class_type_parameter_throws_exception() $this->expectExceptionCode(1661516853); $this->expectExceptionMessageMatches('/Missing first parameter of type `class-string` for the constructor `.*`\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor( #[DynamicConstructor] fn (): stdClass => new stdClass() @@ -598,7 +597,7 @@ public function test_invalid_constructor_class_type_parameter_throws_exception() $this->expectExceptionCode(1661517000); $this->expectExceptionMessageMatches('/Invalid type `int` for the first parameter of the constructor `.*`, it should be of type `class-string`\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor( #[DynamicConstructor] fn (int $invalidParameterType): stdClass => new stdClass() @@ -612,7 +611,7 @@ public function test_registered_datetime_constructor_is_used(): void $default = new DateTime('@1356097062'); $defaultImmutable = new DateTimeImmutable('@1356097062'); - $mapper = (new MapperBuilder()) + $mapper = $this->mapperBuilder() ->registerConstructor(fn (int $timestamp): DateTime => $default) ->registerConstructor(fn (int $timestamp): DateTimeImmutable => $defaultImmutable) ->mapper(); @@ -633,7 +632,7 @@ public function test_constructor_with_same_number_of_arguments_as_native_datetim $default = new DateTime('@1659691266'); $defaultImmutable = new DateTimeImmutable('@1659691266'); - $mapper = (new MapperBuilder()) + $mapper = $this->mapperBuilder() ->registerConstructor(fn (int $timestamp, string $timezone): DateTime => $default) ->registerConstructor(fn (int $timestamp, string $timezone): DateTimeImmutable => $defaultImmutable) ->mapper(); @@ -652,7 +651,7 @@ public function test_constructor_with_same_number_of_arguments_as_native_datetim public function test_registered_datetime_constructor_not_matching_source_uses_default_constructor(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(fn (string $foo, int $bar): DateTimeImmutable => new DateTimeImmutable()) ->mapper() ->map(DateTimeInterface::class, 1647781015); @@ -666,7 +665,7 @@ public function test_registered_datetime_constructor_not_matching_source_uses_de public function test_registered_constructor_throwing_exception_fails_mapping_with_message(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor(fn (): stdClass => throw new FakeErrorMessage('some error message', 1656076090)) ->mapper() ->map(stdClass::class, []); diff --git a/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php b/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php index 59f79d59..c4ab4d73 100644 --- a/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php +++ b/tests/Integration/Mapping/EnumConstructorRegistrationMappingTest.php @@ -3,7 +3,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; @@ -12,7 +11,7 @@ class EnumConstructorRegistrationMappingTest extends IntegrationTestCase public function test_constructor_with_no_argument_is_called_when_no_value_is_given(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( BackedStringEnum::class, fn (): BackedStringEnum => BackedStringEnum::FOO @@ -29,7 +28,7 @@ public function test_constructor_with_no_argument_is_called_when_no_value_is_giv public function test_constructor_with_no_argument_is_not_called_when_value_is_given(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( BackedStringEnum::class, fn (): BackedStringEnum => BackedStringEnum::FOO @@ -46,7 +45,7 @@ public function test_constructor_with_no_argument_is_not_called_when_value_is_gi public function test_constructor_with_one_argument_is_called_when_one_value_is_given(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(fn (string $value): BackedStringEnum => BackedStringEnum::from($value)) ->mapper() ->map(BackedStringEnum::class, 'foo'); @@ -60,7 +59,7 @@ public function test_constructor_with_one_argument_is_called_when_one_value_is_g public function test_constructor_with_several_arguments_is_called_when_values_are_given(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(fn (string $foo, int $bar): BackedStringEnum => BackedStringEnum::FOO) ->mapper() ->map(BackedStringEnum::class, [ @@ -77,7 +76,7 @@ public function test_constructor_with_several_arguments_is_called_when_values_ar public function test_registered_native_constructor_is_called_when_one_argument_is_given(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( BackedStringEnum::class, fn (string $foo, int $bar): BackedStringEnum => BackedStringEnum::BAR @@ -94,7 +93,7 @@ public function test_registered_native_constructor_is_called_when_one_argument_i public function test_map_to_enum_pattern_fetches_correct_constructor(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( /** * @return BackedStringEnum::BA* @@ -118,7 +117,7 @@ public function test_map_to_enum_pattern_fetches_correct_constructor(): void public function test_register_internal_from_constructor_is_overridden_by_library_constructor(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor(BackedStringEnum::from(...)) ->mapper() ->map(BackedStringEnum::class, 'fiz'); @@ -133,7 +132,7 @@ public function test_register_internal_from_constructor_is_overridden_by_library public function test_register_internal_try_from_constructor_is_overridden_by_library_constructor(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->registerConstructor(BackedStringEnum::tryFrom(...)) ->mapper() ->map(BackedStringEnum::class, 'fiz'); diff --git a/tests/Integration/Mapping/ExceptionFilteringTest.php b/tests/Integration/Mapping/ExceptionFilteringTest.php index 06185c4c..3dc169f5 100644 --- a/tests/Integration/Mapping/ExceptionFilteringTest.php +++ b/tests/Integration/Mapping/ExceptionFilteringTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DomainException; @@ -20,13 +19,13 @@ public function test_userland_exception_not_filtered_is_not_caught(): void $this->expectExceptionCode(1657042062); $this->expectExceptionMessage('some error message'); - (new MapperBuilder())->mapper()->map(ClassThatThrowsExceptionIfInvalidValue::class, 'bar'); + $this->mapperBuilder()->mapper()->map(ClassThatThrowsExceptionIfInvalidValue::class, 'bar'); } public function test_userland_exception_filtered_is_caught_and_added_to_mapping_errors(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->filterExceptions(function (Throwable $exception): ErrorMessage { if ($exception instanceof DomainException) { return new FakeErrorMessage('some error message', 1657197780); diff --git a/tests/Integration/Mapping/InterfaceInferringMappingTest.php b/tests/Integration/Mapping/InterfaceInferringMappingTest.php index 9c06453b..41358cfc 100644 --- a/tests/Integration/Mapping/InterfaceInferringMappingTest.php +++ b/tests/Integration/Mapping/InterfaceInferringMappingTest.php @@ -12,7 +12,6 @@ use CuyZ\Valinor\Mapper\Tree\Exception\ObjectImplementationCallbackError; use CuyZ\Valinor\Mapper\Tree\Exception\ObjectImplementationNotRegistered; use CuyZ\Valinor\Mapper\Tree\Exception\ResolvedImplementationIsNotAccepted; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage; use CuyZ\Valinor\Tests\Fixture\Object\InterfaceWithDifferentNamespaces\A\ClassThatInheritsInterfaceA; use CuyZ\Valinor\Tests\Fixture\Object\InterfaceWithDifferentNamespaces\B\ClassThatInheritsInterfaceB; @@ -32,7 +31,7 @@ final class InterfaceInferringMappingTest extends IntegrationTestCase public function test_override_date_time_interface_inferring_overrides_correctly(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer(DateTimeInterface::class, fn () => DateTime::class) ->mapper() ->map(DateTimeInterface::class, 1645279176); @@ -47,7 +46,7 @@ public function test_override_date_time_interface_inferring_overrides_correctly( public function test_single_argument_for_both_infer_and_object_constructor_can_be_used(): void { try { - $mapper = (new MapperBuilder()) + $mapper = $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string|class-string */ @@ -72,7 +71,7 @@ public function test_single_argument_for_both_infer_and_object_constructor_can_b public function test_infer_interface_with_union_of_class_string_works_properly(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string|class-string */ @@ -90,7 +89,7 @@ public function test_infer_interface_with_union_of_class_string_works_properly() public function test_infer_interface_with_class_string_with_union_of_class_names_works_properly(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ @@ -108,7 +107,7 @@ public function test_infer_interface_with_class_string_with_union_of_class_names public function test_infer_interface_with_single_argument_works_properly(): void { try { - [$resultA, $resultB] = (new MapperBuilder()) + [$resultA, $resultB] = $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ @@ -136,7 +135,7 @@ public function test_infer_interface_with_single_argument_works_properly(): void public function test_infer_interface_with_several_arguments_works_properly(): void { try { - [$resultA, $resultB] = (new MapperBuilder()) + [$resultA, $resultB] = $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ @@ -176,7 +175,7 @@ function (string $type, int $key): string { public function test_infer_with_two_functions_with_same_name_works_properly(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer( InterfaceA::class, InterfaceAInferer::infer(...) @@ -210,7 +209,7 @@ public function test_unresolvable_implementation_throws_exception(): void $this->expectExceptionCode(1618049116); $this->expectExceptionMessage('Impossible to resolve an implementation for `' . SomeInterface::class . '`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->mapper() ->map(SomeInterface::class, []); } @@ -221,7 +220,7 @@ public function test_invalid_resolved_implementation_value_throws_exception(): v $this->expectExceptionCode(1630091260); $this->expectExceptionMessage('Invalid value 42, expected a subtype of `DateTimeInterface`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer(DateTimeInterface::class, fn () => 42) ->mapper() ->map(DateTimeInterface::class, []); @@ -233,7 +232,7 @@ public function test_invalid_resolved_implementation_type_throws_exception(): vo $this->expectExceptionCode(1618049487); $this->expectExceptionMessage('Invalid implementation type `int`, expected a subtype of `DateTimeInterface`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer(DateTimeInterface::class, fn () => 'int') ->mapper() ->map(DateTimeInterface::class, []); @@ -245,7 +244,7 @@ public function test_invalid_resolved_implementation_throws_exception(): void $this->expectExceptionCode(1618049487); $this->expectExceptionMessage('Invalid implementation type `stdClass`, expected a subtype of `DateTimeInterface`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer(DateTimeInterface::class, fn () => stdClass::class) ->mapper() ->map(DateTimeInterface::class, []); @@ -257,7 +256,7 @@ public function test_invalid_object_type_resolved_implementation_throws_exceptio $this->expectExceptionCode(1618049487); $this->expectExceptionMessage('Invalid implementation type `DateTimeInterface`, expected a subtype of `DateTimeInterface`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer(DateTimeInterface::class, fn () => DateTimeInterface::class) ->mapper() ->map(DateTimeInterface::class, []); @@ -271,7 +270,7 @@ public function test_object_implementation_callback_error_throws_exception(): vo $this->expectExceptionCode(1653983061); $this->expectExceptionMessage('Error thrown when trying to get implementation of `DateTimeInterface`: some error message'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer( DateTimeInterface::class, fn () => throw $exception @@ -286,7 +285,7 @@ public function test_invalid_abstract_object_name_throws_exception(): void $this->expectExceptionCode(1653990369); $this->expectExceptionMessage('Invalid interface or class name `invalid type`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer('invalid type', fn () => stdClass::class) // @phpstan-ignore-line ->mapper() ->map(stdClass::class, []); @@ -298,7 +297,7 @@ public function test_invalid_abstract_object_type_throws_exception(): void $this->expectExceptionCode(1653990369); $this->expectExceptionMessage('Invalid interface or class name `string`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer('string', fn () => stdClass::class) // @phpstan-ignore-line ->mapper() ->map(stdClass::class, []); @@ -310,7 +309,7 @@ public function test_missing_object_implementation_registration_throws_exception $this->expectExceptionCode(1653990549); $this->expectExceptionMessage('No implementation of `' . SomeInterface::class . '` found with return type `mixed` of'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer( SomeInterface::class, fn (string $type) => SomeClassThatInheritsInterfaceA::class @@ -325,7 +324,7 @@ public function test_invalid_union_object_implementation_registration_throws_exc $this->expectExceptionCode(1653990549); $this->expectExceptionMessage('No implementation of `' . SomeInterface::class . '` found with return type `string|int` of'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer( SomeInterface::class, fn (string $value): string|int => $value === 'foo' ? 'foo' : 42 @@ -340,7 +339,7 @@ public function test_invalid_class_string_object_implementation_registration_thr $this->expectExceptionCode(1653990549); $this->expectExceptionMessage('No implementation of `' . SomeInterface::class . '` found with return type `class-string` of'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ @@ -356,7 +355,7 @@ public function test_object_implementation_not_registered_throws_exception(): vo $this->expectExceptionCode(1653990989); $this->expectExceptionMessage('Invalid implementation `' . SomeClassThatInheritsInterfaceC::class . '` for `' . SomeInterface::class . '`, it should be one of `' . SomeClassThatInheritsInterfaceA::class . '`, `' . SomeClassThatInheritsInterfaceB::class . '`.'); - (new MapperBuilder()) + $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ @@ -369,7 +368,7 @@ public function test_object_implementation_not_registered_throws_exception(): vo public function test_invalid_source_throws_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ @@ -388,7 +387,7 @@ public function test_invalid_source_throws_exception(): void public function test_invalid_source_value_throws_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ @@ -406,7 +405,7 @@ public function test_invalid_source_value_throws_exception(): void public function test_exception_thrown_is_caught_and_throws_message_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->infer( DateTimeInterface::class, /** @return class-string */ @@ -425,7 +424,7 @@ public function test_exception_thrown_is_caught_and_throws_message_exception(): public function test_superfluous_values_throws_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->infer( SomeInterface::class, /** @return class-string */ diff --git a/tests/Integration/Mapping/MappingErrorTest.php b/tests/Integration/Mapping/MappingErrorTest.php index 623897be..d9d85738 100644 --- a/tests/Integration/Mapping/MappingErrorTest.php +++ b/tests/Integration/Mapping/MappingErrorTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class MappingErrorTest extends IntegrationTestCase @@ -16,7 +15,7 @@ public function test_single_tree_mapper_error_details_are_reported_in_exception_ $this->expectExceptionCode(1617193185); $this->expectExceptionMessage("Could not map type `string`. An error occurred at path *root*: Value array{0: 'foo'} is not a valid string."); - (new MapperBuilder())->mapper()->map('string', ['foo']); + $this->mapperBuilder()->mapper()->map('string', ['foo']); } public function test_several_tree_mapper_errors_count_are_reported_in_exception_message(): void @@ -25,7 +24,7 @@ public function test_several_tree_mapper_errors_count_are_reported_in_exception_ $this->expectExceptionCode(1617193185); $this->expectExceptionMessage("Could not map type `array{foo: string, bar: int}` with value array{foo: 42, bar: 'some string'}. A total of 2 errors were encountered."); - (new MapperBuilder())->mapper()->map( + $this->mapperBuilder()->mapper()->map( 'array{foo: string, bar: int}', ['foo' => 42, 'bar' => 'some string'] ); @@ -36,6 +35,6 @@ public function test_single_argument_mapper_error_details_are_reported_in_except $this->expectException(MappingError::class); $this->expectExceptionCode(1671115362); - (new MapperBuilder())->argumentsMapper()->mapArguments(fn (string $foo) => $foo, 42); + $this->mapperBuilder()->argumentsMapper()->mapArguments(fn (string $foo) => $foo, 42); } } diff --git a/tests/Integration/Mapping/Message/MessageFormatterTest.php b/tests/Integration/Mapping/Message/MessageFormatterTest.php index f9c9341a..ac7f2987 100644 --- a/tests/Integration/Mapping/Message/MessageFormatterTest.php +++ b/tests/Integration/Mapping/Message/MessageFormatterTest.php @@ -9,7 +9,6 @@ use CuyZ\Valinor\Mapper\Tree\Message\Formatter\LocaleMessageFormatter; use CuyZ\Valinor\Mapper\Tree\Message\Formatter\MessageMapFormatter; use CuyZ\Valinor\Mapper\Tree\Message\Formatter\TranslationMessageFormatter; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class MessageFormatterTest extends IntegrationTestCase @@ -17,7 +16,7 @@ final class MessageFormatterTest extends IntegrationTestCase public function test_message_is_formatted_correctly(): void { try { - (new MapperBuilder())->mapper()->map('int', 'foo'); + $this->mapperBuilder()->mapper()->map('int', 'foo'); } catch (MappingError $error) { $formatter = new AggregateMessageFormatter( new LocaleMessageFormatter('fr'), diff --git a/tests/Integration/Mapping/Namespace/RegisteredStaticConstructorWithReturnTypeInDocBlockTest.php b/tests/Integration/Mapping/Namespace/RegisteredStaticConstructorWithReturnTypeInDocBlockTest.php index ee221dfd..61bbc63f 100644 --- a/tests/Integration/Mapping/Namespace/RegisteredStaticConstructorWithReturnTypeInDocBlockTest.php +++ b/tests/Integration/Mapping/Namespace/RegisteredStaticConstructorWithReturnTypeInDocBlockTest.php @@ -4,7 +4,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Namespace; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class RegisteredStaticConstructorWithReturnTypeInDocBlockTest extends IntegrationTestCase @@ -12,7 +11,7 @@ final class RegisteredStaticConstructorWithReturnTypeInDocBlockTest extends Inte // @see https://github.com/CuyZ/Valinor/issues/461 public function test_registered_static_constructor_with_return_type_in_doc_block_works_properly(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( SomeClassWithStaticConstructor::staticConstructorWithReturnTypeInDocBlock(...), ) diff --git a/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php b/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php index 6220030d..c8fca1db 100644 --- a/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ArrayValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject as SimpleObjectAlias; @@ -41,7 +40,7 @@ public function test_values_are_mapped_properly(): void foreach ([ArrayValues::class, ArrayValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -70,7 +69,7 @@ public function test_values_are_mapped_properly(): void public function test_empty_array_in_non_empty_array_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(ArrayValues::class, [ + $this->mapperBuilder()->mapper()->map(ArrayValues::class, [ 'nonEmptyArraysOfStrings' => [], ]); } catch (MappingError $exception) { @@ -84,7 +83,7 @@ public function test_empty_array_in_non_empty_array_throws_exception(): void public function test_value_with_invalid_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(ArrayValues::class, [ + $this->mapperBuilder()->mapper()->map(ArrayValues::class, [ 'integers' => ['foo'], ]); } catch (MappingError $exception) { diff --git a/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php b/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php index 50db27b1..37900e06 100644 --- a/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ConstantValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum; use CuyZ\Valinor\Tests\Fixture\Object\ObjectWithConstants; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; @@ -35,7 +34,7 @@ public function test_values_are_mapped_properly(): void foreach ([ClassWithConstantValues::class, ClassWithConstantValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -63,7 +62,7 @@ public function test_values_are_mapped_properly(): void public function test_private_constant_cannot_be_mapped(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->mapper() ->map(ObjectWithConstants::class . '::CONST_WITH_STRING_*', 'some private string value'); } catch (MappingError $exception) { @@ -77,7 +76,7 @@ public function test_private_constant_cannot_be_mapped(): void public function test_constant_not_matching_pattern_cannot_be_mapped(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->mapper() ->map(ObjectWithConstants::class . '::CONST_WITH_STRING_*', 'some prefixed string value'); } catch (MappingError $exception) { diff --git a/tests/Integration/Mapping/Object/DateTimeMappingTest.php b/tests/Integration/Mapping/Object/DateTimeMappingTest.php index 609d7682..99d7113f 100644 --- a/tests/Integration/Mapping/Object/DateTimeMappingTest.php +++ b/tests/Integration/Mapping/Object/DateTimeMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DateTimeInterface; @@ -14,7 +13,7 @@ final class DateTimeMappingTest extends IntegrationTestCase public function test_default_datetime_constructor_cannot_be_used(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, ['datetime' => '2022/08/05', 'timezone' => 'Europe/Paris']); } catch (MappingError $exception) { @@ -27,7 +26,7 @@ public function test_default_datetime_constructor_cannot_be_used(): void public function test_default_date_constructor_with_valid_rfc_3339_format_source_returns_datetime(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, '2022-08-05T08:32:06+00:00'); } catch (MappingError $error) { @@ -40,7 +39,7 @@ public function test_default_date_constructor_with_valid_rfc_3339_format_source_ public function test_default_date_constructor_with_valid_rfc_3339_and_milliseconds_format_source_returns_datetime(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, '2022-08-05T08:32:06.123Z'); } catch (MappingError $error) { @@ -55,7 +54,7 @@ public function test_default_date_constructor_with_valid_rfc_3339_and_millisecon public function test_default_date_constructor_with_valid_rfc_3339_and_microseconds_format_source_returns_datetime(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, '2022-08-05T08:32:06.123456Z'); } catch (MappingError $error) { @@ -70,7 +69,7 @@ public function test_default_date_constructor_with_valid_rfc_3339_and_microsecon public function test_default_date_constructor_with_valid_timestamp_format_source_returns_datetime(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, 1659688380); } catch (MappingError $error) { @@ -83,7 +82,7 @@ public function test_default_date_constructor_with_valid_timestamp_format_source public function test_default_date_constructor_with_timestamp_at_0_source_returns_datetime(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, 0); } catch (MappingError $error) { @@ -96,7 +95,7 @@ public function test_default_date_constructor_with_timestamp_at_0_source_returns public function test_default_date_constructor_with_a_negative_timestamp_source_returns_datetime(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, -1); } catch (MappingError $error) { @@ -109,7 +108,7 @@ public function test_default_date_constructor_with_a_negative_timestamp_source_r public function test_registered_date_constructor_with_valid_source_returns_datetime(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->supportDateFormats('d/m/Y', 'Y/m/d') ->mapper() ->map(DateTimeInterface::class, '2022/08/05'); @@ -123,7 +122,7 @@ public function test_registered_date_constructor_with_valid_source_returns_datet public function test_default_date_constructor_with_invalid_source_throws_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->mapper() ->map(DateTimeInterface::class, 'invalid datetime'); } catch (MappingError $exception) { @@ -137,7 +136,7 @@ public function test_default_date_constructor_with_invalid_source_throws_excepti public function test_registered_date_constructor_with_invalid_source_throws_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->supportDateFormats('Y/m/d') ->mapper() ->map(DateTimeInterface::class, 'invalid datetime'); @@ -152,7 +151,7 @@ public function test_registered_date_constructor_with_invalid_source_throws_exce public function test_date_constructor_with_overridden_format_source_throws_exception(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->supportDateFormats('Y/m/d') ->supportDateFormats('d/m/Y') ->mapper() diff --git a/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php b/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php index 8ab2a455..ba86d164 100644 --- a/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php +++ b/tests/Integration/Mapping/Object/DateTimeZoneMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DateTimeZone; @@ -14,7 +13,7 @@ final class DateTimeZoneMappingTest extends IntegrationTestCase public function test_can_map_to_timezone_with_default_constructor(): void { try { - $result = (new MapperBuilder())->mapper()->map(DateTimeZone::class, 'Europe/Paris'); + $result = $this->mapperBuilder()->mapper()->map(DateTimeZone::class, 'Europe/Paris'); } catch (MappingError $error) { $this->mappingFail($error); } @@ -25,7 +24,7 @@ public function test_can_map_to_timezone_with_default_constructor(): void public function test_constructor_with_one_argument_replaces_default_constructor(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor( fn (string $europeanCity): DateTimeZone => new DateTimeZone("Europe/$europeanCity") ) @@ -40,7 +39,7 @@ public function test_constructor_with_one_argument_replaces_default_constructor( public function test_constructor_with_two_arguments_does_not_replaces_default_constructor(): void { - $mapper = (new MapperBuilder())->registerConstructor( + $mapper = $this->mapperBuilder()->registerConstructor( fn (string $continent, string $city): DateTimeZone => new DateTimeZone("$continent/$city") )->mapper(); @@ -67,7 +66,7 @@ public function test_constructor_with_two_arguments_does_not_replaces_default_co public function test_invalid_timezone_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(DateTimeZone::class, 'Jupiter/Europa'); + $this->mapperBuilder()->mapper()->map(DateTimeZone::class, 'Jupiter/Europa'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; diff --git a/tests/Integration/Mapping/Object/EnumValuesMappingTest.php b/tests/Integration/Mapping/Object/EnumValuesMappingTest.php index b16d52e1..cd0e667f 100644 --- a/tests/Integration/Mapping/Object/EnumValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/EnumValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum; use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum; use CuyZ\Valinor\Tests\Fixture\Enum\PureEnum; @@ -28,7 +27,7 @@ public function test_values_are_mapped_properly(): void foreach ([EnumValues::class, EnumValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -46,7 +45,7 @@ public function test_values_are_mapped_properly(): void public function test_invalid_string_enum_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(BackedStringEnum::class, new StringableObject('fiz')); + $this->mapperBuilder()->mapper()->map(BackedStringEnum::class, new StringableObject('fiz')); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -57,7 +56,7 @@ public function test_invalid_string_enum_value_throws_exception(): void public function test_invalid_integer_enum_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(BackedIntegerEnum::class, '512'); + $this->mapperBuilder()->mapper()->map(BackedIntegerEnum::class, '512'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -68,7 +67,7 @@ public function test_invalid_integer_enum_value_throws_exception(): void public function test_value_not_matching_pure_enum_case_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(PureEnum::class . '::FOO', 'fiz'); + $this->mapperBuilder()->mapper()->map(PureEnum::class . '::FOO', 'fiz'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -79,7 +78,7 @@ public function test_value_not_matching_pure_enum_case_throws_exception(): void public function test_value_not_matching_backed_integer_enum_case_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(BackedIntegerEnum::class . '::FOO', '512'); + $this->mapperBuilder()->mapper()->map(BackedIntegerEnum::class . '::FOO', '512'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -90,7 +89,7 @@ public function test_value_not_matching_backed_integer_enum_case_throws_exceptio public function test_value_not_filled_for_pure_enum_case_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array{foo: ' . PureEnum::class . '::FOO}', []); + $this->mapperBuilder()->mapper()->map('array{foo: ' . PureEnum::class . '::FOO}', []); } catch (MappingError $exception) { $error = $exception->node()->children()['foo']->messages()[0]; @@ -101,7 +100,7 @@ public function test_value_not_filled_for_pure_enum_case_throws_exception(): voi public function test_value_not_filled_for_backed_integer_enum_case_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array{foo: ' . BackedIntegerEnum::class . '::FOO}', []); + $this->mapperBuilder()->mapper()->map('array{foo: ' . BackedIntegerEnum::class . '::FOO}', []); } catch (MappingError $exception) { $error = $exception->node()->children()['foo']->messages()[0]; diff --git a/tests/Integration/Mapping/Object/ExtendedDateIntervalMappingTest.php b/tests/Integration/Mapping/Object/ExtendedDateIntervalMappingTest.php index 2d2b4044..93e3e7a8 100644 --- a/tests/Integration/Mapping/Object/ExtendedDateIntervalMappingTest.php +++ b/tests/Integration/Mapping/Object/ExtendedDateIntervalMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\DateInterval; @@ -14,7 +13,7 @@ final class ExtendedDateIntervalMappingTest extends IntegrationTestCase public function test_extended_date_interval_is_mapped_properly(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map(DateInterval::class, 'P1Y2M3DT4H5M6S'); } catch (MappingError $error) { diff --git a/tests/Integration/Mapping/Object/GenericInheritanceTest.php b/tests/Integration/Mapping/Object/GenericInheritanceTest.php index 10a92ca1..d27e7394 100644 --- a/tests/Integration/Mapping/Object/GenericInheritanceTest.php +++ b/tests/Integration/Mapping/Object/GenericInheritanceTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class GenericInheritanceTest extends IntegrationTestCase @@ -13,7 +12,7 @@ final class GenericInheritanceTest extends IntegrationTestCase public function test_generic_types_are_inherited_properly(): void { try { - $object = (new MapperBuilder()) + $object = $this->mapperBuilder() ->mapper() ->map(ChildClassWithInheritedGenericType::class, [ 'valueA' => 'foo', diff --git a/tests/Integration/Mapping/Object/GenericValuesMappingTest.php b/tests/Integration/Mapping/Object/GenericValuesMappingTest.php index 0e7110b0..18e253ea 100644 --- a/tests/Integration/Mapping/Object/GenericValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/GenericValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject as SimpleObjectAlias; @@ -43,7 +42,7 @@ public function test_values_are_mapped_properly(): void foreach ([GenericValues::class, GenericValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/Object/IterableValuesMappingTest.php b/tests/Integration/Mapping/Object/IterableValuesMappingTest.php index 82467f6a..b1cc25ae 100644 --- a/tests/Integration/Mapping/Object/IterableValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/IterableValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject as SimpleObjectAlias; @@ -36,7 +35,7 @@ public function test_values_are_mapped_properly(): void foreach ([IterableValues::class, IterableValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/Object/ListValuesMappingTest.php b/tests/Integration/Mapping/Object/ListValuesMappingTest.php index 5b6d2322..6f1d3ef6 100644 --- a/tests/Integration/Mapping/Object/ListValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ListValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject as SimpleObjectAlias; @@ -27,7 +26,7 @@ public function test_values_are_mapped_properly(): void foreach ([ListValues::class, ListValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -50,7 +49,7 @@ public function test_values_are_mapped_properly(): void public function test_empty_list_in_non_empty_list_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(ListValues::class, [ + $this->mapperBuilder()->mapper()->map(ListValues::class, [ 'nonEmptyListOfStrings' => [], ]); } catch (MappingError $exception) { @@ -64,7 +63,7 @@ public function test_empty_list_in_non_empty_list_throws_exception(): void public function test_map_array_with_non_sequential_keys_to_list_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('list', [ + $this->mapperBuilder()->mapper()->map('list', [ 0 => 'foo', 2 => 'bar', ]); @@ -79,7 +78,7 @@ public function test_map_array_with_non_sequential_keys_to_list_throws_exception public function test_value_with_invalid_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(ListValues::class, [ + $this->mapperBuilder()->mapper()->map(ListValues::class, [ 'integers' => ['foo'], ]); } catch (MappingError $exception) { diff --git a/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php b/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php index 128e8a8e..b661e40f 100644 --- a/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php +++ b/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class LocalTypeAliasMappingTest extends IntegrationTestCase @@ -28,7 +27,7 @@ public function test_values_are_mapped_properly(): void foreach ([PhpStanLocalAliases::class, PsalmLocalAliases::class] as $class) { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map($class, $source); @@ -47,7 +46,7 @@ public function test_type_aliases_are_imported_correctly(): void { foreach ([PhpStanAliasImport::class, PsalmAliasImport::class] as $class) { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map($class, [ 'firstImportedType' => 42, diff --git a/tests/Integration/Mapping/Object/NullableMappingTest.php b/tests/Integration/Mapping/Object/NullableMappingTest.php index fbc2f94f..5bfd203a 100644 --- a/tests/Integration/Mapping/Object/NullableMappingTest.php +++ b/tests/Integration/Mapping/Object/NullableMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class NullableMappingTest extends IntegrationTestCase @@ -13,7 +12,7 @@ final class NullableMappingTest extends IntegrationTestCase public function test_nullable_properties_default_value_are_handled_properly(): void { try { - $result = (new MapperBuilder())->mapper()->map(NullablePropertyWithNullDefaultValue::class, []); + $result = $this->mapperBuilder()->mapper()->map(NullablePropertyWithNullDefaultValue::class, []); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php b/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php index 4ae056ea..04681cb8 100644 --- a/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ObjectValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use stdClass; @@ -21,7 +20,7 @@ public function test_values_are_mapped_properly(): void foreach ([ObjectValues::class, ObjectValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -36,7 +35,7 @@ public function test_invalid_iterable_source_throws_exception(): void foreach ([ObjectValues::class, ObjectValuesWithConstructor::class] as $class) { try { - (new MapperBuilder())->mapper()->map($class, $source); + $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -49,7 +48,7 @@ public function test_invalid_iterable_source_throws_exception(): void public function test_superfluous_values_throws_exception_and_keeps_nested_errors(): void { try { - (new MapperBuilder())->mapper()->map(ObjectWithTwoProperties::class, [ + $this->mapperBuilder()->mapper()->map(ObjectWithTwoProperties::class, [ 'stringA' => 42, 'stringB' => 'fooB', 'unexpectedValueA' => 'foo', @@ -69,7 +68,7 @@ public function test_superfluous_values_throws_exception_and_keeps_nested_errors public function test_object_with_no_argument_build_with_non_array_source_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(stdClass::class, 'foo'); + $this->mapperBuilder()->mapper()->map(stdClass::class, 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; diff --git a/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php b/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php index aa793a5d..e9ba4bf4 100644 --- a/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ScalarValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use DateTime; @@ -50,7 +49,7 @@ public function test_values_are_mapped_properly(): void foreach ([ScalarValues::class, ScalarValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -88,7 +87,7 @@ public function test_values_are_mapped_properly(): void public function test_value_with_invalid_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(SimpleObject::class, new stdClass()); + $this->mapperBuilder()->mapper()->map(SimpleObject::class, new stdClass()); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; diff --git a/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php b/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php index 8a0b369a..acf2634a 100644 --- a/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use stdClass; @@ -66,7 +65,7 @@ public function test_values_are_mapped_properly(): void foreach ([ShapedArrayValues::class, ShapedArrayValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -92,7 +91,7 @@ public function test_values_are_mapped_properly(): void public function test_value_with_invalid_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(ShapedArrayValues::class, [ + $this->mapperBuilder()->mapper()->map(ShapedArrayValues::class, [ 'basicShapedArrayWithStringKeys' => [ 'foo' => new stdClass(), 'bar' => 42, diff --git a/tests/Integration/Mapping/Object/SimpleNamespacedObjectMappingTest.php b/tests/Integration/Mapping/Object/SimpleNamespacedObjectMappingTest.php index b515b21e..fcb7f1e2 100644 --- a/tests/Integration/Mapping/Object/SimpleNamespacedObjectMappingTest.php +++ b/tests/Integration/Mapping/Object/SimpleNamespacedObjectMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use SimpleNamespace\SimpleNamespacedObject; @@ -16,7 +15,7 @@ public function test_simple_namespaced_object_can_be_mapped(): void require_once(__DIR__ . '/../Fixture/SimpleNamespacedObject.php'); try { - $object = (new MapperBuilder())->mapper()->map(SimpleNamespacedObject::class, ['foo']); + $object = $this->mapperBuilder()->mapper()->map(SimpleNamespacedObject::class, ['foo']); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/Object/UnionOfObjectsMappingTest.php b/tests/Integration/Mapping/Object/UnionOfObjectsMappingTest.php index 1fdfafd6..22fa8613 100644 --- a/tests/Integration/Mapping/Object/UnionOfObjectsMappingTest.php +++ b/tests/Integration/Mapping/Object/UnionOfObjectsMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use PHPUnit\Framework\Attributes\DataProvider; @@ -14,7 +13,7 @@ final class UnionOfObjectsMappingTest extends IntegrationTestCase public function test_objects_sharing_one_property_are_resolved_correctly(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerConstructor(SomeFooAndBarObject::constructorA(...)) ->registerConstructor(SomeFooAndBarObject::constructorB(...)) ->mapper() @@ -33,7 +32,7 @@ public function test_objects_sharing_one_property_are_resolved_correctly(): void public function test_mapping_to_union_of_null_and_objects_can_infer_object(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->mapper() ->map( 'null|' . SomeObjectWithFooAndBar::class . '|' . SomeObjectWithBazAndFiz::class, @@ -60,7 +59,7 @@ public function test_mapping_to_union_of_null_and_objects_can_infer_object(): vo public function test_mapping_error_when_cannot_resolve_union(string $className, array $source): void { try { - (new MapperBuilder())->mapper()->map($className, $source); + $this->mapperBuilder()->mapper()->map($className, $source); self::fail('No mapping error when one was expected'); } catch (MappingError $exception) { diff --git a/tests/Integration/Mapping/Object/UnionValuesMappingTest.php b/tests/Integration/Mapping/Object/UnionValuesMappingTest.php index c0d87922..1cdbe5ba 100644 --- a/tests/Integration/Mapping/Object/UnionValuesMappingTest.php +++ b/tests/Integration/Mapping/Object/UnionValuesMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fixture\Object\ObjectWithConstants; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DateTimeImmutable; @@ -39,7 +38,7 @@ public function test_values_are_mapped_properly(): void foreach ($classes as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -71,7 +70,7 @@ public function test_values_are_mapped_properly(): void foreach ([UnionOfFixedValues::class, UnionOfFixedValuesWithConstructor::class] as $class) { try { - $result = (new MapperBuilder())->mapper()->map($class, $source); + $result = $this->mapperBuilder()->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -88,7 +87,7 @@ public function test_values_are_mapped_properly(): void public function test_invalid_value_is_not_casted_when_casting_mode_is_disabled(): void { try { - (new MapperBuilder())->mapper()->map('string|int', 42.404); + $this->mapperBuilder()->mapper()->map('string|int', 42.404); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; diff --git a/tests/Integration/Mapping/Other/ArrayMappingTest.php b/tests/Integration/Mapping/Other/ArrayMappingTest.php index e6d68910..0ac41371 100644 --- a/tests/Integration/Mapping/Other/ArrayMappingTest.php +++ b/tests/Integration/Mapping/Other/ArrayMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Other; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use stdClass; @@ -16,7 +15,7 @@ public function test_map_to_array_of_scalars_works_properly(): void $source = ['foo', 'bar', 'baz']; try { - $result = (new MapperBuilder())->mapper()->map('string[]', $source); + $result = $this->mapperBuilder()->mapper()->map('string[]', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -29,7 +28,7 @@ public function test_map_to_array_with_union_string_key_works_properly(): void $source = ['foo' => 'foo', 'bar' => 'bar']; try { - $result = (new MapperBuilder())->mapper()->map("array<'foo'|'bar', string>", $source); + $result = $this->mapperBuilder()->mapper()->map("array<'foo'|'bar', string>", $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -42,7 +41,7 @@ public function test_map_to_array_with_union_integer_key_works_properly(): void $source = [42 => 'foo', 1337 => 'bar']; try { - $result = (new MapperBuilder())->mapper()->map('array<42|1337, string>', $source); + $result = $this->mapperBuilder()->mapper()->map('array<42|1337, string>', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -55,7 +54,7 @@ public function test_map_to_array_with_positive_integer_key_works_properly(): vo $source = [42 => 'foo', 1337 => 'bar']; try { - $result = (new MapperBuilder())->mapper()->map('array', $source); + $result = $this->mapperBuilder()->mapper()->map('array', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -68,7 +67,7 @@ public function test_map_to_array_with_negative_integer_key_works_properly(): vo $source = [-42 => 'foo', -1337 => 'bar']; try { - $result = (new MapperBuilder())->mapper()->map('array', $source); + $result = $this->mapperBuilder()->mapper()->map('array', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -81,7 +80,7 @@ public function test_map_to_array_with_integer_range_key_works_properly(): void $source = [-42 => 'foo', 42 => 'foo', 1337 => 'bar']; try { - $result = (new MapperBuilder())->mapper()->map('array, string>', $source); + $result = $this->mapperBuilder()->mapper()->map('array, string>', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -94,7 +93,7 @@ public function test_map_to_array_with_non_empty_string_key_works_properly(): vo $source = ['foo' => 'foo', 'bar' => 'bar']; try { - $result = (new MapperBuilder())->mapper()->map('array', $source); + $result = $this->mapperBuilder()->mapper()->map('array', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -107,7 +106,7 @@ public function test_map_to_array_with_class_string_key_works_properly(): void $source = [stdClass::class => 'foo']; try { - $result = (new MapperBuilder())->mapper()->map('array', $source); + $result = $this->mapperBuilder()->mapper()->map('array', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -118,7 +117,7 @@ public function test_map_to_array_with_class_string_key_works_properly(): void public function test_value_with_invalid_integer_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array', ['foo' => 'foo']); + $this->mapperBuilder()->mapper()->map('array', ['foo' => 'foo']); } catch (MappingError $exception) { $error = $exception->node()->children()['foo']->messages()[0]; @@ -129,7 +128,7 @@ public function test_value_with_invalid_integer_key_type_throws_exception(): voi public function test_value_with_invalid_positive_integer_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array', [-42 => 'foo']); + $this->mapperBuilder()->mapper()->map('array', [-42 => 'foo']); } catch (MappingError $exception) { $error = $exception->node()->children()[-42]->messages()[0]; @@ -140,7 +139,7 @@ public function test_value_with_invalid_positive_integer_key_type_throws_excepti public function test_value_with_invalid_negative_integer_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array', [42 => 'foo']); + $this->mapperBuilder()->mapper()->map('array', [42 => 'foo']); } catch (MappingError $exception) { $error = $exception->node()->children()[42]->messages()[0]; @@ -151,7 +150,7 @@ public function test_value_with_invalid_negative_integer_key_type_throws_excepti public function test_value_with_invalid_integer_range_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array, string>', [-404 => 'foo']); + $this->mapperBuilder()->mapper()->map('array, string>', [-404 => 'foo']); } catch (MappingError $exception) { $error = $exception->node()->children()[-404]->messages()[0]; @@ -162,7 +161,7 @@ public function test_value_with_invalid_integer_range_key_type_throws_exception( public function test_value_with_invalid_union_string_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map("array<'foo'|'bar', string>", ['baz' => 'baz']); + $this->mapperBuilder()->mapper()->map("array<'foo'|'bar', string>", ['baz' => 'baz']); } catch (MappingError $exception) { $error = $exception->node()->children()['baz']->messages()[0]; @@ -173,7 +172,7 @@ public function test_value_with_invalid_union_string_key_type_throws_exception() public function test_value_with_invalid_union_integer_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array<42|1337, string>', [404 => 'baz']); + $this->mapperBuilder()->mapper()->map('array<42|1337, string>', [404 => 'baz']); } catch (MappingError $exception) { $error = $exception->node()->children()[404]->messages()[0]; @@ -184,7 +183,7 @@ public function test_value_with_invalid_union_integer_key_type_throws_exception( public function test_value_with_invalid_non_empty_string_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array', ['' => 'foo']); + $this->mapperBuilder()->mapper()->map('array', ['' => 'foo']); } catch (MappingError $exception) { $error = $exception->node()->children()['']->messages()[0]; @@ -195,7 +194,7 @@ public function test_value_with_invalid_non_empty_string_key_type_throws_excepti public function test_value_with_invalid_class_string_key_type_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array', ['foo bar' => 'foo']); + $this->mapperBuilder()->mapper()->map('array', ['foo bar' => 'foo']); } catch (MappingError $exception) { $error = $exception->node()->children()['foo bar']->messages()[0]; diff --git a/tests/Integration/Mapping/Other/FlexibleCastingMappingTest.php b/tests/Integration/Mapping/Other/FlexibleCastingMappingTest.php index 154bec99..021e2be3 100644 --- a/tests/Integration/Mapping/Other/FlexibleCastingMappingTest.php +++ b/tests/Integration/Mapping/Other/FlexibleCastingMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\TreeMapper; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum; use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum; use CuyZ\Valinor\Tests\Fixture\Object\StringableObject; @@ -21,7 +20,7 @@ protected function setUp(): void { parent::setUp(); - $this->mapper = (new MapperBuilder())->enableFlexibleCasting()->mapper(); + $this->mapper = $this->mapperBuilder()->enableFlexibleCasting()->mapper(); } public function test_leading_zero_in_numeric_is_mapped_properly(): void @@ -172,7 +171,7 @@ public function test_null_value_for_class_fills_it_with_empty_array(): void public function test_null_value_for_interface_with_no_properties_needed_fills_it_with_empty_array(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer(SomeInterfaceForClassWithNoProperties::class, fn () => SomeClassWithNoProperties::class) ->enableFlexibleCasting() ->mapper() @@ -187,7 +186,7 @@ public function test_null_value_for_interface_with_no_properties_needed_fills_it public function test_interface_is_inferred_and_mapped_properly_in_flexible_casting_mode(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->infer(SomeInterfaceForClassWithProperties::class, fn () => SomeClassWithProperties::class) ->enableFlexibleCasting() ->mapper() diff --git a/tests/Integration/Mapping/Other/PermissiveTypesMappingTest.php b/tests/Integration/Mapping/Other/PermissiveTypesMappingTest.php index 40c893f0..89d50f0a 100644 --- a/tests/Integration/Mapping/Other/PermissiveTypesMappingTest.php +++ b/tests/Integration/Mapping/Other/PermissiveTypesMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\TreeMapper; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DateTime; use stdClass; @@ -19,7 +18,7 @@ protected function setUp(): void { parent::setUp(); - $this->mapper = (new MapperBuilder())->allowPermissiveTypes()->mapper(); + $this->mapper = $this->mapperBuilder()->allowPermissiveTypes()->mapper(); } public function test_can_map_to_mixed_type(): void diff --git a/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php b/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php index 9ca10d8c..d82f7bfa 100644 --- a/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php +++ b/tests/Integration/Mapping/Other/ShapedArrayMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping\Other; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class ShapedArrayMappingTest extends IntegrationTestCase @@ -19,7 +18,7 @@ public function test_values_are_mapped_properly(): void ]; try { - $result = (new MapperBuilder())->mapper()->map('array{foo: string, bar: int, fiz: float}', $source); + $result = $this->mapperBuilder()->mapper()->map('array{foo: string, bar: int, fiz: float}', $source); } catch (MappingError $error) { $this->mappingFail($error); } @@ -38,7 +37,7 @@ public function test_mapping_from_iterable_to_shaped_array_works_properly(): voi })(); try { - $result = (new MapperBuilder())->mapper()->map('array{foo: string, bar: int, fiz: float}', $iterator); + $result = $this->mapperBuilder()->mapper()->map('array{foo: string, bar: int, fiz: float}', $iterator); } catch (MappingError $error) { $this->mappingFail($error); } @@ -51,7 +50,7 @@ public function test_mapping_from_iterable_to_shaped_array_works_properly(): voi public function test_missing_element_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array{foo: string, bar: int}', ['foo' => 'foo']); + $this->mapperBuilder()->mapper()->map('array{foo: string, bar: int}', ['foo' => 'foo']); } catch (MappingError $exception) { $error = $exception->node()->children()['bar']->messages()[0]; @@ -69,7 +68,7 @@ public function test_superfluous_values_throws_exception_and_keeps_nested_errors ]; try { - (new MapperBuilder())->mapper()->map('array{foo: string, bar: int}', $source); + $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]; diff --git a/tests/Integration/Mapping/Other/StrictMappingTest.php b/tests/Integration/Mapping/Other/StrictMappingTest.php index 3da8d3d8..fcf33c87 100644 --- a/tests/Integration/Mapping/Other/StrictMappingTest.php +++ b/tests/Integration/Mapping/Other/StrictMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\Object\Exception\PermissiveTypeNotAllowed; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum; use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum; use CuyZ\Valinor\Tests\Fixture\Enum\PureEnum; @@ -25,7 +24,7 @@ public function test_missing_value_throws_exception(): void }; try { - (new MapperBuilder())->mapper()->map($class::class, [ + $this->mapperBuilder()->mapper()->map($class::class, [ 'foo' => 'foo', ]); } catch (MappingError $exception) { @@ -42,7 +41,7 @@ public function test_map_to_undefined_object_type_throws_exception(): void $this->expectExceptionCode(1655231817); $this->expectExceptionMessage('Type `object` is too permissive.'); - (new MapperBuilder())->mapper()->map('object', new stdClass()); + $this->mapperBuilder()->mapper()->map('object', new stdClass()); } public function test_map_to_object_containing_undefined_object_type_throws_exception(): void @@ -51,7 +50,7 @@ public function test_map_to_object_containing_undefined_object_type_throws_excep $this->expectExceptionCode(1655389255); $this->expectExceptionMessage('Error for `value` in `' . ObjectContainingUndefinedObjectType::class . ' (properties)`: Type `object` is too permissive.'); - (new MapperBuilder())->mapper()->map(ObjectContainingUndefinedObjectType::class, ['value' => new stdClass()]); + $this->mapperBuilder()->mapper()->map(ObjectContainingUndefinedObjectType::class, ['value' => new stdClass()]); } public function test_map_to_type_containing_mixed_type_throws_exception(): void @@ -60,7 +59,7 @@ public function test_map_to_type_containing_mixed_type_throws_exception(): void $this->expectExceptionCode(1655231817); $this->expectExceptionMessage('Type `mixed` in `array{foo: string, bar: mixed}` is too permissive.'); - (new MapperBuilder())->mapper()->map('array{foo: string, bar: mixed}', ['foo' => 'foo', 'bar' => 42]); + $this->mapperBuilder()->mapper()->map('array{foo: string, bar: mixed}', ['foo' => 'foo', 'bar' => 42]); } public function test_map_to_object_containing_mixed_type_throws_exception(): void @@ -69,13 +68,13 @@ public function test_map_to_object_containing_mixed_type_throws_exception(): voi $this->expectExceptionCode(1655389255); $this->expectExceptionMessage('Error for `value` in `' . ObjectContainingMixedType::class . ' (properties)`: Type `mixed` in `array{foo: string, bar: mixed}` is too permissive.'); - (new MapperBuilder())->mapper()->map(ObjectContainingMixedType::class, ['value' => 'foo']); + $this->mapperBuilder()->mapper()->map(ObjectContainingMixedType::class, ['value' => 'foo']); } public function test_null_that_cannot_be_cast_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('int', null); + $this->mapperBuilder()->mapper()->map('int', null); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -86,7 +85,7 @@ public function test_null_that_cannot_be_cast_throws_exception(): void public function test_invalid_float_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('float', 'foo'); + $this->mapperBuilder()->mapper()->map('float', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -97,7 +96,7 @@ public function test_invalid_float_throws_exception(): void public function test_invalid_float_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('42.404', 1337); + $this->mapperBuilder()->mapper()->map('42.404', 1337); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -108,7 +107,7 @@ public function test_invalid_float_value_throws_exception(): void public function test_invalid_integer_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('int', 'foo'); + $this->mapperBuilder()->mapper()->map('int', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -119,7 +118,7 @@ public function test_invalid_integer_throws_exception(): void public function test_invalid_integer_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('42', 1337); + $this->mapperBuilder()->mapper()->map('42', 1337); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -130,7 +129,7 @@ public function test_invalid_integer_value_throws_exception(): void public function test_invalid_integer_range_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('int<42, 1337>', 'foo'); + $this->mapperBuilder()->mapper()->map('int<42, 1337>', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -141,7 +140,7 @@ public function test_invalid_integer_range_throws_exception(): void public function test_invalid_enum_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(PureEnum::class, 'foo'); + $this->mapperBuilder()->mapper()->map(PureEnum::class, 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -152,7 +151,7 @@ public function test_invalid_enum_value_throws_exception(): void public function test_invalid_string_enum_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(BackedStringEnum::class, new stdClass()); + $this->mapperBuilder()->mapper()->map(BackedStringEnum::class, new stdClass()); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -163,7 +162,7 @@ public function test_invalid_string_enum_value_throws_exception(): void public function test_invalid_integer_enum_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map(BackedIntegerEnum::class, false); + $this->mapperBuilder()->mapper()->map(BackedIntegerEnum::class, false); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -174,7 +173,7 @@ public function test_invalid_integer_enum_value_throws_exception(): void public function test_invalid_union_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('bool|int|float', 'foo'); + $this->mapperBuilder()->mapper()->map('bool|int|float', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -186,7 +185,7 @@ public function test_invalid_union_value_throws_exception(): void public function test_invalid_utf8_union_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('bool|int|float', 'πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„'); + $this->mapperBuilder()->mapper()->map('bool|int|float', 'πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„πŸ¦„'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -198,7 +197,7 @@ public function test_invalid_utf8_union_value_throws_exception(): void public function test_null_in_union_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('bool|int|float', null); + $this->mapperBuilder()->mapper()->map('bool|int|float', null); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -210,7 +209,7 @@ public function test_null_in_union_value_throws_exception(): void public function test_invalid_array_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array', 'foo'); + $this->mapperBuilder()->mapper()->map('array', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -222,7 +221,7 @@ public function test_invalid_array_value_throws_exception(): void public function test_invalid_list_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('list', 'foo'); + $this->mapperBuilder()->mapper()->map('list', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -234,7 +233,7 @@ public function test_invalid_list_value_throws_exception(): void public function test_invalid_shaped_array_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array{foo: string}', 'foo'); + $this->mapperBuilder()->mapper()->map('array{foo: string}', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; @@ -246,7 +245,7 @@ public function test_invalid_shaped_array_value_throws_exception(): void public function test_invalid_shaped_array_with_object_value_throws_exception(): void { try { - (new MapperBuilder())->mapper()->map('array{foo: stdClass}', 'foo'); + $this->mapperBuilder()->mapper()->map('array{foo: stdClass}', 'foo'); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; diff --git a/tests/Integration/Mapping/Other/SuperfluousKeysMappingTest.php b/tests/Integration/Mapping/Other/SuperfluousKeysMappingTest.php index 26762ccf..1316f0fa 100644 --- a/tests/Integration/Mapping/Other/SuperfluousKeysMappingTest.php +++ b/tests/Integration/Mapping/Other/SuperfluousKeysMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\TreeMapper; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class SuperfluousKeysMappingTest extends IntegrationTestCase @@ -17,7 +16,7 @@ protected function setUp(): void { parent::setUp(); - $this->mapper = (new MapperBuilder())->allowSuperfluousKeys()->mapper(); + $this->mapper = $this->mapperBuilder()->allowSuperfluousKeys()->mapper(); } public function test_superfluous_shaped_array_values_are_mapped_properly(): void diff --git a/tests/Integration/Mapping/ReadonlyMappingTest.php b/tests/Integration/Mapping/ReadonlyMappingTest.php index 4ae79893..cdb07d7f 100644 --- a/tests/Integration/Mapping/ReadonlyMappingTest.php +++ b/tests/Integration/Mapping/ReadonlyMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class ReadonlyMappingTest extends IntegrationTestCase @@ -17,7 +16,7 @@ public function test_single_property_and_constructor_parameter_are_mapped_proper }; try { - $object = (new MapperBuilder())->mapper()->map($class::class, 'foo'); + $object = $this->mapperBuilder()->mapper()->map($class::class, 'foo'); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/SingleNodeMappingTest.php b/tests/Integration/Mapping/SingleNodeMappingTest.php index 697f0013..e93a55f9 100644 --- a/tests/Integration/Mapping/SingleNodeMappingTest.php +++ b/tests/Integration/Mapping/SingleNodeMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; use PHPUnit\Framework\Attributes\DataProvider; @@ -19,7 +18,7 @@ final class SingleNodeMappingTest extends IntegrationTestCase public function test_single_property_and_constructor_parameter_are_mapped_properly(string $className, mixed $value): void { try { - $result = (new MapperBuilder())->mapper()->map($className, $value); + $result = $this->mapperBuilder()->mapper()->map($className, $value); } catch (MappingError $error) { $this->mappingFail($error); } @@ -34,7 +33,7 @@ public function test_single_property_and_constructor_parameter_are_mapped_proper public function test_single_property_and_constructor_parameter_with_default_value_are_mapped_properly(string $className): void { try { - $result = (new MapperBuilder())->mapper()->map($className, ['foo' => []]); + $result = $this->mapperBuilder()->mapper()->map($className, ['foo' => []]); } catch (MappingError $error) { $this->mappingFail($error); } @@ -49,7 +48,7 @@ public function test_single_property_and_constructor_parameter_with_default_valu public function test_single_property_and_constructor_parameter_can_be_mapped_with_array_with_property_name(string $className, mixed $value): void { try { - $result = (new MapperBuilder())->mapper()->map($className, ['value' => $value]); + $result = $this->mapperBuilder()->mapper()->map($className, ['value' => $value]); } catch (MappingError $error) { $this->mappingFail($error); } @@ -60,7 +59,7 @@ public function test_single_property_and_constructor_parameter_can_be_mapped_wit public function test_single_argument_invalid_value_with_key_present_in_source_keeps_path_in_error_nodes(): void { try { - (new MapperBuilder())->mapper()->map(SimpleObject::class, ['value' => 42]); + $this->mapperBuilder()->mapper()->map(SimpleObject::class, ['value' => 42]); } catch (MappingError $exception) { $error = $exception->node()->children()['value']->messages()[0]; @@ -71,7 +70,7 @@ public function test_single_argument_invalid_value_with_key_present_in_source_ke public function test_single_argument_invalid_value_with_key_not_present_in_source_does_not_keep_path_in_error_nodes(): void { try { - (new MapperBuilder())->mapper()->map(SimpleObject::class, 42); + $this->mapperBuilder()->mapper()->map(SimpleObject::class, 42); } catch (MappingError $exception) { $error = $exception->node()->messages()[0]; diff --git a/tests/Integration/Mapping/Source/JsonSourceMappingTest.php b/tests/Integration/Mapping/Source/JsonSourceMappingTest.php index 86a1e20d..5f8636bd 100644 --- a/tests/Integration/Mapping/Source/JsonSourceMappingTest.php +++ b/tests/Integration/Mapping/Source/JsonSourceMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\Source\JsonSource; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class JsonSourceMappingTest extends IntegrationTestCase @@ -20,7 +19,7 @@ public function test_json_source_is_mapped_correctly(): void }; try { - $object = (new MapperBuilder())->mapper()->map( + $object = $this->mapperBuilder()->mapper()->map( $class::class, new JsonSource('{"foo": "foo", "bar": "bar"}') ); diff --git a/tests/Integration/Mapping/Source/Modifier/CamelCaseKeysMappingTest.php b/tests/Integration/Mapping/Source/Modifier/CamelCaseKeysMappingTest.php index cd745e9e..05c4fd2e 100644 --- a/tests/Integration/Mapping/Source/Modifier/CamelCaseKeysMappingTest.php +++ b/tests/Integration/Mapping/Source/Modifier/CamelCaseKeysMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\Source\Modifier\CamelCaseKeys; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\ObjectWithSubProperties; use PHPUnit\Framework\Attributes\DataProvider; @@ -20,7 +19,7 @@ final class CamelCaseKeysMappingTest extends IntegrationTestCase public function test_sources_are_mapped_properly(iterable $source): void { try { - $object = (new MapperBuilder())->mapper()->map(ObjectWithSubProperties::class, $source); + $object = $this->mapperBuilder()->mapper()->map(ObjectWithSubProperties::class, $source); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/Source/Modifier/PathMappingTest.php b/tests/Integration/Mapping/Source/Modifier/PathMappingTest.php index e6c2b3aa..60c2d63a 100644 --- a/tests/Integration/Mapping/Source/Modifier/PathMappingTest.php +++ b/tests/Integration/Mapping/Source/Modifier/PathMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\Source\Modifier\PathMapping; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\Country; @@ -47,7 +46,7 @@ public function test_path_with_sub_paths_are_mapped(): void ]); try { - $countries = (new MapperBuilder())->mapper()->map('list<' . Country::class . '>', $source); + $countries = $this->mapperBuilder()->mapper()->map('list<' . Country::class . '>', $source); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/Source/SourceTest.php b/tests/Integration/Mapping/Source/SourceTest.php index 80e464ee..da614865 100644 --- a/tests/Integration/Mapping/Source/SourceTest.php +++ b/tests/Integration/Mapping/Source/SourceTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\Source\Source; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\ObjectWithSubProperties; use IteratorAggregate; @@ -22,7 +21,7 @@ final class SourceTest extends IntegrationTestCase public function test_sources_are_mapped_properly(iterable $source): void { try { - $object = (new MapperBuilder())->mapper()->map(ObjectWithSubProperties::class, $source); + $object = $this->mapperBuilder()->mapper()->map(ObjectWithSubProperties::class, $source); } catch (MappingError $error) { $this->mappingFail($error); } diff --git a/tests/Integration/Mapping/Source/YamlSourceMappingTest.php b/tests/Integration/Mapping/Source/YamlSourceMappingTest.php index c54b1f09..8ad10cc9 100644 --- a/tests/Integration/Mapping/Source/YamlSourceMappingTest.php +++ b/tests/Integration/Mapping/Source/YamlSourceMappingTest.php @@ -6,7 +6,6 @@ use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\Source\YamlSource; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use PHPUnit\Framework\Attributes\RequiresPhpExtension; @@ -22,7 +21,7 @@ public function test_yaml_source_is_mapped_correctly(): void }; try { - $object = (new MapperBuilder())->mapper()->map( + $object = $this->mapperBuilder()->mapper()->map( $class::class, new YamlSource("foo: foo\nbar: bar") ); diff --git a/tests/Integration/Mapping/UnionMappingTest.php b/tests/Integration/Mapping/UnionMappingTest.php index c9472d61..20ccebb8 100644 --- a/tests/Integration/Mapping/UnionMappingTest.php +++ b/tests/Integration/Mapping/UnionMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\City; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; @@ -15,7 +14,7 @@ final class UnionMappingTest extends IntegrationTestCase public function test_union_with_int_or_object(): void { try { - $array = (new MapperBuilder())->mapper()->map("list", [123, "foo"]); + $array = $this->mapperBuilder()->mapper()->map("list", [123, "foo"]); } catch (MappingError $error) { $this->mappingFail($error); } @@ -27,7 +26,7 @@ public function test_union_with_int_or_object(): void public function test_union_with_string_or_object_prioritizes_string(): void { try { - $array = (new MapperBuilder()) + $array = $this->mapperBuilder() ->mapper() ->map("list", ["foo", "bar", "baz"]); } catch (MappingError $error) { @@ -40,7 +39,7 @@ public function test_union_with_string_or_object_prioritizes_string(): void public function test_union_with_string_literal_or_object_prioritizes_string_literal(): void { try { - $array = (new MapperBuilder()) + $array = $this->mapperBuilder() ->mapper() ->map("list<'foo'|" . SimpleObject::class . "|'bar'>", ["foo", "bar", "baz"]); } catch (MappingError $error) { @@ -55,7 +54,7 @@ public function test_union_with_string_literal_or_object_prioritizes_string_lite public function test_union_of_objects(): void { try { - $array = (new MapperBuilder()) + $array = $this->mapperBuilder() ->mapper() ->map( "list<" . SimpleObject::class . "|" . City::class . ">", diff --git a/tests/Integration/Mapping/ValueAlteringMappingTest.php b/tests/Integration/Mapping/ValueAlteringMappingTest.php index aa194976..43df5aba 100644 --- a/tests/Integration/Mapping/ValueAlteringMappingTest.php +++ b/tests/Integration/Mapping/ValueAlteringMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; @@ -17,7 +16,7 @@ final class ValueAlteringMappingTest extends IntegrationTestCase public function test_alter_string_alters_value(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->alter(fn () => 'bar') ->alter(fn (string $value) => strtolower($value)) ->alter(fn (string $value) => strtoupper($value)) @@ -35,7 +34,7 @@ public function test_alter_string_alters_value(): void public function test_value_not_accepted_by_value_altering_callback_is_not_used(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->alter(fn (string $value) => $value) ->mapper() ->map('string|null', null); @@ -49,7 +48,7 @@ public function test_value_not_accepted_by_value_altering_callback_is_not_used() public function test_alter_function_is_called_when_not_the_first_nor_the_last_one(): void { try { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->alter(fn (int $value) => 404) ->alter(fn (string $value) => $value . '!') ->alter(fn (float $value) => 42.1337) diff --git a/tests/Integration/Mapping/VariadicParameterMappingTest.php b/tests/Integration/Mapping/VariadicParameterMappingTest.php index 33647170..dc6cc1b9 100644 --- a/tests/Integration/Mapping/VariadicParameterMappingTest.php +++ b/tests/Integration/Mapping/VariadicParameterMappingTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Mapping; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; final class VariadicParameterMappingTest extends IntegrationTestCase @@ -13,7 +12,7 @@ final class VariadicParameterMappingTest extends IntegrationTestCase public function test_only_variadic_parameters_are_mapped_properly(): void { try { - $object = (new MapperBuilder())->mapper()->map(SomeClassWithOnlyVariadicParameters::class, ['foo', 'bar', 'baz']); + $object = $this->mapperBuilder()->mapper()->map(SomeClassWithOnlyVariadicParameters::class, ['foo', 'bar', 'baz']); } catch (MappingError $error) { $this->mappingFail($error); } @@ -24,7 +23,7 @@ public function test_only_variadic_parameters_are_mapped_properly(): void public function test_variadic_parameters_are_mapped_properly_when_string_keys_are_given(): void { try { - $object = (new MapperBuilder())->mapper()->map(SomeClassWithOnlyVariadicParameters::class, [ + $object = $this->mapperBuilder()->mapper()->map(SomeClassWithOnlyVariadicParameters::class, [ 'foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz', @@ -39,7 +38,7 @@ public function test_variadic_parameters_are_mapped_properly_when_string_keys_ar public function test_constructor_with_variadic_parameters_with_dots_in_dot_blocks_are_defined_properly(): void { try { - (new MapperBuilder()) + $this->mapperBuilder() ->mapper() ->map(SomeClassWithVariadicParametersInDocBlock::class, ['']); } catch (MappingError $exception) { @@ -52,7 +51,7 @@ public function test_constructor_with_variadic_parameters_with_dots_in_dot_block public function test_non_variadic_and_variadic_parameters_are_mapped_properly(): void { try { - $object = (new MapperBuilder())->mapper()->map(SomeClassWithNonVariadicAndVariadicParameters::class, [ + $object = $this->mapperBuilder()->mapper()->map(SomeClassWithNonVariadicAndVariadicParameters::class, [ 'int' => 42, 'values' => ['foo', 'bar', 'baz'], ]); @@ -67,7 +66,7 @@ public function test_non_variadic_and_variadic_parameters_are_mapped_properly(): public function test_named_constructor_with_non_variadic_and_variadic_parameters_are_mapped_properly(): void { try { - $object = (new MapperBuilder()) + $object = $this->mapperBuilder() ->registerConstructor(SomeClassWithNamedConstructorWithNonVariadicAndVariadicParameters::new(...)) ->mapper() ->map(SomeClassWithNamedConstructorWithNonVariadicAndVariadicParameters::class, [ diff --git a/tests/Integration/Normalizer/CommonExamples/CustomObjectNormalizationTest.php b/tests/Integration/Normalizer/CommonExamples/CustomObjectNormalizationTest.php index 6e5ed78e..e3141693 100644 --- a/tests/Integration/Normalizer/CommonExamples/CustomObjectNormalizationTest.php +++ b/tests/Integration/Normalizer/CommonExamples/CustomObjectNormalizationTest.php @@ -4,17 +4,16 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; -use PHPUnit\Framework\TestCase; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use function dechex; -final class CustomObjectNormalizationTest extends TestCase +final class CustomObjectNormalizationTest extends IntegrationTestCase { public function test_custom_object_normalization_works_properly(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer( fn (HasCustomNormalization $object) => $object->normalize(), ) diff --git a/tests/Integration/Normalizer/CommonExamples/DateFormatFromAttributeTest.php b/tests/Integration/Normalizer/CommonExamples/DateFormatFromAttributeTest.php index 17303276..e03a05e6 100644 --- a/tests/Integration/Normalizer/CommonExamples/DateFormatFromAttributeTest.php +++ b/tests/Integration/Normalizer/CommonExamples/DateFormatFromAttributeTest.php @@ -5,17 +5,16 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; use Attribute; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DateTimeImmutable; use DateTimeInterface; -use PHPUnit\Framework\TestCase; -final class DateFormatFromAttributeTest extends TestCase +final class DateFormatFromAttributeTest extends IntegrationTestCase { public function test_date_format_attribute_works_properly(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer(DateTimeFormat::class) ->normalizer(Format::array()) ->normalize(new class ( diff --git a/tests/Integration/Normalizer/CommonExamples/DateFormatFromGlobalTransformerTest.php b/tests/Integration/Normalizer/CommonExamples/DateFormatFromGlobalTransformerTest.php index ec6a7f69..842ac028 100644 --- a/tests/Integration/Normalizer/CommonExamples/DateFormatFromGlobalTransformerTest.php +++ b/tests/Integration/Normalizer/CommonExamples/DateFormatFromGlobalTransformerTest.php @@ -4,17 +4,16 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DateTimeImmutable; use DateTimeInterface; -use PHPUnit\Framework\TestCase; -final class DateFormatFromGlobalTransformerTest extends TestCase +final class DateFormatFromGlobalTransformerTest extends IntegrationTestCase { public function test_date_format_from_global_transformer_works_properly(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer( fn (DateTimeInterface $date) => $date->format('Y/m/d'), ) diff --git a/tests/Integration/Normalizer/CommonExamples/IgnoreAttributeTest.php b/tests/Integration/Normalizer/CommonExamples/IgnoreAttributeTest.php index 0718eeba..0f1da082 100644 --- a/tests/Integration/Normalizer/CommonExamples/IgnoreAttributeTest.php +++ b/tests/Integration/Normalizer/CommonExamples/IgnoreAttributeTest.php @@ -5,15 +5,14 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; use Attribute; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; -use PHPUnit\Framework\TestCase; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; -final class IgnoreAttributeTest extends TestCase +final class IgnoreAttributeTest extends IntegrationTestCase { public function test_ignore_attribute_works_properly(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer( fn (object $value, callable $next) => array_filter( $next(), diff --git a/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseFromAttributeTest.php b/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseFromAttributeTest.php index a9ec34a2..174e3a92 100644 --- a/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseFromAttributeTest.php +++ b/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseFromAttributeTest.php @@ -5,15 +5,14 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; use Attribute; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; -use PHPUnit\Framework\TestCase; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; -final class ObjectKeysToSnakeCaseFromAttributeTest extends TestCase +final class ObjectKeysToSnakeCaseFromAttributeTest extends IntegrationTestCase { public function test_object_keys_are_converted_to_snake_case(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer(SnakeCaseProperties::class) ->normalizer(Format::array()) ->normalize(new #[SnakeCaseProperties] class () { diff --git a/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseTest.php b/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseTest.php index 336053a8..02ed5860 100644 --- a/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseTest.php +++ b/tests/Integration/Normalizer/CommonExamples/ObjectKeysToSnakeCaseTest.php @@ -4,15 +4,14 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; -use PHPUnit\Framework\TestCase; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; -final class ObjectKeysToSnakeCaseTest extends TestCase +final class ObjectKeysToSnakeCaseTest extends IntegrationTestCase { public function test_object_keys_are_converted_to_snake_case(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer( function (object $object, callable $next) { $result = []; diff --git a/tests/Integration/Normalizer/CommonExamples/RenamePropertyFromAttributeTest.php b/tests/Integration/Normalizer/CommonExamples/RenamePropertyFromAttributeTest.php index b213e704..45303836 100644 --- a/tests/Integration/Normalizer/CommonExamples/RenamePropertyFromAttributeTest.php +++ b/tests/Integration/Normalizer/CommonExamples/RenamePropertyFromAttributeTest.php @@ -5,15 +5,14 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; use Attribute; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; -use PHPUnit\Framework\TestCase; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; -final class RenamePropertyFromAttributeTest extends TestCase +final class RenamePropertyFromAttributeTest extends IntegrationTestCase { public function test_rename_attribute_works_properly(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer(Rename::class) ->normalizer(Format::array()) ->normalize(new class () { diff --git a/tests/Integration/Normalizer/CommonExamples/UppercaseFromAttributeTest.php b/tests/Integration/Normalizer/CommonExamples/UppercaseFromAttributeTest.php index 43adf949..ea1f8cfd 100644 --- a/tests/Integration/Normalizer/CommonExamples/UppercaseFromAttributeTest.php +++ b/tests/Integration/Normalizer/CommonExamples/UppercaseFromAttributeTest.php @@ -5,15 +5,14 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; use Attribute; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; -use PHPUnit\Framework\TestCase; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; -final class UppercaseFromAttributeTest extends TestCase +final class UppercaseFromAttributeTest extends IntegrationTestCase { public function test_uppercase_attribute_works_properly(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer(Uppercase::class) ->normalizer(Format::array()) ->normalize(new class () { diff --git a/tests/Integration/Normalizer/CommonExamples/VersionTransformerTest.php b/tests/Integration/Normalizer/CommonExamples/VersionTransformerTest.php index 14f167ff..f3952cbe 100644 --- a/tests/Integration/Normalizer/CommonExamples/VersionTransformerTest.php +++ b/tests/Integration/Normalizer/CommonExamples/VersionTransformerTest.php @@ -4,15 +4,14 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer\CommonExamples; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; -use PHPUnit\Framework\TestCase; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; -final class VersionTransformerTest extends TestCase +final class VersionTransformerTest extends IntegrationTestCase { public function test_version_transformer_works_properly(): void { - $normalizeWithVersion = fn (string $version) => (new MapperBuilder()) + $normalizeWithVersion = fn (string $version) => $this->mapperBuilder() ->registerTransformer( fn (HasVersionedNormalization $object, callable $next) => $object->normalizeWithVersion($version, $next), ) diff --git a/tests/Integration/Normalizer/NormalizerTest.php b/tests/Integration/Normalizer/NormalizerTest.php index 081b8be8..2a1359d5 100644 --- a/tests/Integration/Normalizer/NormalizerTest.php +++ b/tests/Integration/Normalizer/NormalizerTest.php @@ -5,7 +5,6 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer; use Attribute; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Exception\CircularReferenceFoundDuringNormalization; use CuyZ\Valinor\Normalizer\Exception\KeyTransformerHasTooManyParameters; use CuyZ\Valinor\Normalizer\Exception\KeyTransformerParameterInvalidType; @@ -17,19 +16,19 @@ use CuyZ\Valinor\Tests\Fixture\Enum\BackedIntegerEnum; use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum; use CuyZ\Valinor\Tests\Fixture\Enum\PureEnum; +use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; use DateTimeImmutable; use DateTimeInterface; use DateTimeZone; use IteratorAggregate; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use RuntimeException; use stdClass; use Traversable; use function array_merge; -final class NormalizerTest extends TestCase +final class NormalizerTest extends IntegrationTestCase { /** * @param array> $transformers @@ -43,7 +42,7 @@ public function test_normalize_basic_values_yields_expected_output( array $transformers = [], array $transformerAttributes = [], ): void { - $builder = new MapperBuilder(); + $builder = $this->mapperBuilder(); foreach ($transformers as $priority => $transformersList) { foreach ($transformersList as $transformer) { @@ -635,7 +634,7 @@ public function test_generator_of_scalar_yields_expected_array(): void 'boolean' => true, ]; - $arrayResult = (new MapperBuilder()) + $arrayResult = $this->mapperBuilder() ->normalizer(Format::array()) ->normalize($input); @@ -653,7 +652,7 @@ public function test_generator_of_scalar_yields_expected_json(): void $expected = '{"string":"foo","integer":42,"float":1337.404,"boolean":true}'; - $jsonResult = (new MapperBuilder()) + $jsonResult = $this->mapperBuilder() ->normalizer(Format::json()) ->normalize($input); @@ -671,7 +670,7 @@ public function test_generator_yielding_list_yields_list_json(): void $expected = '["foo",42,1337.404,true]'; - $jsonResult = (new MapperBuilder()) + $jsonResult = $this->mapperBuilder() ->normalizer(Format::json()) ->normalize($input); @@ -689,7 +688,7 @@ public function test_generator_with_first_key_0_yields_list_json(): void $expected = '["foo",42,1337.404,true]'; - $jsonResult = (new MapperBuilder()) + $jsonResult = $this->mapperBuilder() ->normalizer(Format::json()) ->normalize($input); @@ -724,7 +723,7 @@ public function test_nested_generator_of_scalar_yields_expected_array(): void 'booleans' => [true, false], ]; - $arrayResult = (new MapperBuilder()) + $arrayResult = $this->mapperBuilder() ->normalizer(Format::array()) ->normalize($input); @@ -754,7 +753,7 @@ public function test_nested_generator_of_scalar_yields_expected_json(): void $expected = '{"strings":["foo","bar"],"integers":[42,1337],"floats":[42.5,1337.404],"booleans":[true,false]}'; - $jsonResult = (new MapperBuilder()) + $jsonResult = $this->mapperBuilder() ->normalizer(Format::json()) ->normalize($input); @@ -763,7 +762,7 @@ public function test_nested_generator_of_scalar_yields_expected_json(): void public function test_transformer_is_called_only_once_on_object_property_when_using_default_transformer(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer( fn (string $value, callable $next) => $next() . '!', ) @@ -775,7 +774,7 @@ public function test_transformer_is_called_only_once_on_object_property_when_usi public function test_no_priority_given_is_set_to_0(): void { - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->registerTransformer( fn (object $object) => 'foo', -2, @@ -805,7 +804,7 @@ public function test_can_normalize_same_object_in_array_without_throwing_circula $objectB = new stdClass(); $objectB->bar = 'bar'; - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->normalizer(Format::array()) ->normalize([$objectA, $objectB, $objectA]); @@ -826,7 +825,7 @@ public function test_can_normalize_same_object_in_iterable_without_throwing_circ yield $objectA; })(); - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->normalizer(Format::array()) ->normalize($iterable); @@ -854,7 +853,7 @@ public function __construct() } }; - $result = (new MapperBuilder()) + $result = $this->mapperBuilder() ->normalizer(Format::array()) ->normalize($object); @@ -871,7 +870,7 @@ public function test_no_param_in_transformer_throws_exception(): void $this->expectExceptionCode(1695064946); $this->expectExceptionMessageMatches('/Transformer must have at least one parameter, none given for `.*`\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(fn () => 42) ->normalizer(Format::array()) ->normalize(new stdClass()); @@ -883,7 +882,7 @@ public function test_too_many_params_in_transformer_throws_exception(): void $this->expectExceptionCode(1695065433); $this->expectExceptionMessageMatches('/Transformer must have at most 2 parameters, 3 given for `.*`\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(fn (stdClass $object, callable $next, int $unexpectedParameter) => 42) ->normalizer(Format::array()) ->normalize(new stdClass()); @@ -895,7 +894,7 @@ public function test_second_param_in_transformer_is_not_callable_throws_exceptio $this->expectExceptionCode(1695065710); $this->expectExceptionMessageMatches('/Transformer\'s second parameter must be a callable, `int` given for `.*`\./'); - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(fn (stdClass $object, int $unexpectedParameterType) => 42) ->normalizer(Format::array()) ->normalize(new stdClass()); @@ -909,7 +908,7 @@ public function test_no_param_in_transformer_attribute_throws_exception(): void $class = new #[TransformerAttributeWithNoParameter] class () {}; - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(TransformerAttributeWithNoParameter::class) ->normalizer(Format::array()) ->normalize($class); @@ -923,7 +922,7 @@ public function test_too_many_params_in_transformer_attribute_throws_exception() $class = new #[TransformerAttributeWithTooManyParameters] class () {}; - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(TransformerAttributeWithTooManyParameters::class) ->normalizer(Format::array()) ->normalize($class); @@ -937,7 +936,7 @@ public function test_second_param_in_transformer_attribute_is_not_callable_throw $class = new #[TransformerAttributeWithSecondParameterNotCallable] class () {}; - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(TransformerAttributeWithSecondParameterNotCallable::class) ->normalizer(Format::array()) ->normalize($class); @@ -956,7 +955,7 @@ public function __construct( ) {} }; - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(KeyTransformerAttributeWithTooManyParameters::class) ->normalizer(Format::array()) ->normalize($class); @@ -975,7 +974,7 @@ public function __construct( ) {} }; - (new MapperBuilder()) + $this->mapperBuilder() ->registerTransformer(KeyTransformerAttributeParameterNotStringOrInteger::class) ->normalizer(Format::array()) ->normalize($class); @@ -992,7 +991,7 @@ public function test_object_circular_reference_is_detected_and_throws_exception( $a->b = $b; $b->a = $a; - (new MapperBuilder())->normalizer(Format::array())->normalize($a); + $this->mapperBuilder()->normalizer(Format::array())->normalize($a); } public function test_unhandled_type_throws_exception(): void @@ -1001,7 +1000,7 @@ public function test_unhandled_type_throws_exception(): void $this->expectExceptionCode(1695062925); $this->expectExceptionMessage('Value of type `Closure` cannot be normalized.'); - (new MapperBuilder())->normalizer(Format::array())->normalize(fn () => 42); + $this->mapperBuilder()->normalizer(Format::array())->normalize(fn () => 42); } public function test_giving_invalid_resource_to_json_normalizer_throws_exception(): void @@ -1010,7 +1009,7 @@ public function test_giving_invalid_resource_to_json_normalizer_throws_exception $this->expectExceptionMessage('Expected a valid resource, got string'); // @phpstan-ignore-next-line - (new MapperBuilder())->normalizer(Format::json())->streamTo('foo'); + $this->mapperBuilder()->normalizer(Format::json())->streamTo('foo'); } } diff --git a/tests/Integration/Normalizer/StreamNormalizerTest.php b/tests/Integration/Normalizer/StreamNormalizerTest.php index db94dbf6..db84efea 100644 --- a/tests/Integration/Normalizer/StreamNormalizerTest.php +++ b/tests/Integration/Normalizer/StreamNormalizerTest.php @@ -4,7 +4,6 @@ namespace CuyZ\Valinor\Tests\Integration\Normalizer; -use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Normalizer\Format; use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; @@ -18,7 +17,7 @@ public function test_json_normalizer_can_normalize_into_stream(): void /** @var resource $resource */ $resource = fopen('php://memory', 'r+'); - (new MapperBuilder()) + $this->mapperBuilder() ->normalizer(Format::json()) ->streamTo($resource) ->normalize(['foo' => 'bar']); diff --git a/tests/Unit/Cache/FileSystemCacheTest.php b/tests/Unit/Cache/FileSystemCacheTest.php index 7cebe2d1..4c795d00 100644 --- a/tests/Unit/Cache/FileSystemCacheTest.php +++ b/tests/Unit/Cache/FileSystemCacheTest.php @@ -35,7 +35,7 @@ protected function setUp(): void $this->files = vfsStream::setup('cache-dir'); - $this->cache = new FileSystemCache(vfsStream::url('cache-dir')); + $this->cache = new FileSystemCache($this->files->url()); } public function test_warmup_creates_temporary_dir(): void @@ -151,6 +151,38 @@ public function test_clear_cannot_delete_cache_file_returns_false(): void self::assertFalse($this->cache->clear()); } + public function test_clear_cannot_delete_root_dir_returns_false(): void + { + $files = vfsStream::setup('cache-dir/sub-dir'); + $files->chmod(0444); + + $cache = new FileSystemCache(vfsStream::url('cache-dir/sub-dir')); + + self::assertFalse($cache->clear()); + } + + public function test_clear_cache_does_not_delete_unrelated_file(): void + { + $this->files->addChild(vfsStream::newFile('some-unrelated-file.php')->withContent('foo')); + + $this->cache->set('foo', 'foo'); + + self::assertTrue($this->cache->clear()); + self::assertCount(1, $this->files->getChildren()); + self::assertTrue($this->files->hasChild('some-unrelated-file.php')); + } + + public function test_clear_cache_does_not_delete_unrelated_directory(): void + { + $this->files->addChild(vfsStream::newDirectory('some-unrelated-directory')); + + $this->cache->set('foo', 'foo'); + + self::assertTrue($this->cache->clear()); + self::assertCount(1, $this->files->getChildren()); + self::assertTrue($this->files->hasChild('some-unrelated-directory')); + } + public function test_clear_caches_when_cache_directory_does_not_exists_returns_true(): void { rmdir($this->files->url()); @@ -201,17 +233,6 @@ public function test_cannot_delete_cache_files_returns_false(): void self::assertFalse($this->cache->deleteMultiple(['foo', 'bar'])); } - public function test_clear_cache_does_not_delete_unrelated_files(): void - { - (vfsStream::newFile('some-unrelated-file.php'))->withContent('foo')->at($this->files); - - $this->cache->set('foo', 'foo'); - $this->cache->clear(); - - self::assertCount(2, $this->files->getChildren()); - self::assertTrue($this->files->hasChild('some-unrelated-file.php')); - } - public function test_corrupted_file_throws_exception(): void { $this->cache->set('foo', 'foo');