diff --git a/tests/Unit/Context/ArgsContextTest.php b/tests/Unit/Context/ArgsContextTest.php new file mode 100644 index 00000000..bc8e02d2 --- /dev/null +++ b/tests/Unit/Context/ArgsContextTest.php @@ -0,0 +1,22 @@ +buildDependencies(); + + $context = new ArgsContext($deps->metaLoader, $deps->metaResolver); + + self::assertSame($deps->metaLoader, $context->getMetaLoader()); + self::assertSame($deps->metaResolver, $context->getMetaResolver()); + } + +} diff --git a/tests/Unit/Context/ArgsFieldContextTest.php b/tests/Unit/Context/ArgsFieldContextTest.php new file mode 100644 index 00000000..b9c8d059 --- /dev/null +++ b/tests/Unit/Context/ArgsFieldContextTest.php @@ -0,0 +1,38 @@ +buildDependencies(); + $default = DefaultValueMeta::fromNothing(); + + $context = new ArgsFieldContext($deps->metaLoader, $deps->metaResolver, $default); + + self::assertSame($deps->metaLoader, $context->getMetaLoader()); + self::assertSame($deps->metaResolver, $context->getMetaResolver()); + self::assertFalse($context->hasDefaultValue()); + } + + public function testHasDefault(): void + { + $deps = (new ObjectMapperTester())->buildDependencies(); + $default = DefaultValueMeta::fromValue('foo'); + + $context = new ArgsFieldContext($deps->metaLoader, $deps->metaResolver, $default); + + self::assertSame($deps->metaLoader, $context->getMetaLoader()); + self::assertSame($deps->metaResolver, $context->getMetaResolver()); + self::assertTrue($context->hasDefaultValue()); + self::assertSame('foo', $context->getDefaultValue()); + } + +} diff --git a/tests/Unit/Context/FieldContextTest.php b/tests/Unit/Context/FieldContextTest.php new file mode 100644 index 00000000..a529c065 --- /dev/null +++ b/tests/Unit/Context/FieldContextTest.php @@ -0,0 +1,51 @@ +buildDependencies(); + $options = new Options(); + $typeCreator = static fn (): Type => new SimpleValueType('string'); + $default = DefaultValueMeta::fromNothing(); + $fieldName = 'field'; + $property = new ReflectionProperty(DefaultsVO::class, 'string'); + + $context = new FieldContext( + $deps->metaLoader, + $deps->ruleManager, + $deps->processor, + $options, + $typeCreator, + $default, + true, + $fieldName, + $property, + ); + + self::assertSame($deps->processor, $context->getProcessor()); + self::assertTrue($context->shouldInitializeObjects()); + + self::assertFalse($context->hasDefaultValue()); + self::assertSame($property->getName(), $context->getPropertyName()); + self::assertSame($fieldName, $context->getFieldName()); + + self::assertEquals($typeCreator(), $context->getType()); + self::assertNotSame($typeCreator(), $context->getType()); + self::assertSame($context->getType(), $context->getType()); + } + +} diff --git a/tests/Unit/Context/MappedObjectContextTest.php b/tests/Unit/Context/MappedObjectContextTest.php new file mode 100644 index 00000000..fdd7d1ad --- /dev/null +++ b/tests/Unit/Context/MappedObjectContextTest.php @@ -0,0 +1,39 @@ +buildDependencies(); + $options = new Options(); + $typeCreator = static fn (): Type => new MappedObjectType(DefaultsVO::class); + + $context = new MappedObjectContext( + $deps->metaLoader, + $deps->ruleManager, + $deps->processor, + $options, + $typeCreator, + true, + ); + + self::assertSame($deps->processor, $context->getProcessor()); + self::assertTrue($context->shouldInitializeObjects()); + + self::assertEquals($typeCreator(), $context->getType()); + self::assertNotSame($typeCreator(), $context->getType()); + self::assertSame($context->getType(), $context->getType()); + } + +} diff --git a/tests/Unit/Context/ProcessorCallContextTest.php b/tests/Unit/Context/ProcessorCallContextTest.php new file mode 100644 index 00000000..6f928eb7 --- /dev/null +++ b/tests/Unit/Context/ProcessorCallContextTest.php @@ -0,0 +1,33 @@ +buildDependencies(); + $meta = $deps->metaLoader->load(DefaultsVO::class); + $holder = new ObjectHolder( + new ObjectCreator($deps->dependencyInjectorManager), + DefaultsVO::class, + $meta->getClass(), + null, + ); + + $context = new ProcessorCallContext(DefaultsVO::class, $holder, $meta); + + self::assertSame(DefaultsVO::class, $context->getClass()); + self::assertSame($holder, $context->getObjectHolder()); + self::assertSame($meta, $context->getMeta()); + } + +} diff --git a/tests/Unit/Context/TypeContextTest.php b/tests/Unit/Context/TypeContextTest.php new file mode 100644 index 00000000..c409290f --- /dev/null +++ b/tests/Unit/Context/TypeContextTest.php @@ -0,0 +1,91 @@ +buildDependencies(); + $options = new Options(); + + $context = new TypeContext($deps->metaLoader, $deps->ruleManager, $options); + + try { + $context->getMeta(DefaultsVO::class); + } catch (Throwable $e) { + // Handled bellow + } + + self::assertFalse(isset($e)); + + try { + $context->getRule(StringRule::class); + } catch (Throwable $e) { + // Handled bellow + } + + self::assertFalse(isset($e)); + } + + public function testClone(): void + { + $deps = (new ObjectMapperTester())->buildDependencies(); + $options = new Options(); + + $initContext = new TypeContext($deps->metaLoader, $deps->ruleManager, $options); + $context = $initContext->createClone(); + + self::assertEquals($initContext, $context); + self::assertNotSame($initContext, $context); + } + + public function testOptions(): void + { + $deps = (new ObjectMapperTester())->buildDependencies(); + $options = new Options(); + + $initContext = new TypeContext($deps->metaLoader, $deps->ruleManager, $options); + + self::assertSame($options, $initContext->getOptions()); + + $context = $initContext->createClone(); + self::assertSame($options, $initContext->getOptions()); + self::assertEquals($options, $context->getOptions()); + self::assertNotSame($options, $context->getOptions()); + } + + public function testProcessedClasses(): void + { + $deps = (new ObjectMapperTester())->buildDependencies(); + $options = new Options(); + + $initContext = new TypeContext($deps->metaLoader, $deps->ruleManager, $options); + + self::assertSame([], $initContext->getProcessedClasses()); + + $context = $initContext + ->withProcessedClass(DefaultsVO::class) + ->withProcessedClass(NoDefaultsVO::class); + + self::assertSame([], $initContext->getProcessedClasses()); + self::assertSame( + [ + DefaultsVO::class, + NoDefaultsVO::class, + ], + $context->getProcessedClasses(), + ); + } + +}