Skip to content

Commit

Permalink
Context: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jan 5, 2025
1 parent e321858 commit 65316c8
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/Unit/Context/ArgsContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\ObjectMapper\Unit\Context;

use Orisai\ObjectMapper\Context\ArgsContext;
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
use PHPUnit\Framework\TestCase;

final class ArgsContextTest extends TestCase
{

public function test(): void
{
$deps = (new ObjectMapperTester())->buildDependencies();

$context = new ArgsContext($deps->metaLoader, $deps->metaResolver);

self::assertSame($deps->metaLoader, $context->getMetaLoader());
self::assertSame($deps->metaResolver, $context->getMetaResolver());
}

}
38 changes: 38 additions & 0 deletions tests/Unit/Context/ArgsFieldContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\ObjectMapper\Unit\Context;

use Orisai\ObjectMapper\Context\ArgsFieldContext;
use Orisai\ObjectMapper\Meta\Shared\DefaultValueMeta;
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
use PHPUnit\Framework\TestCase;

final class ArgsFieldContextTest extends TestCase
{

public function test(): void
{
$deps = (new ObjectMapperTester())->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());
}

}
51 changes: 51 additions & 0 deletions tests/Unit/Context/FieldContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\ObjectMapper\Unit\Context;

use Orisai\ObjectMapper\Context\FieldContext;
use Orisai\ObjectMapper\Meta\Shared\DefaultValueMeta;
use Orisai\ObjectMapper\Processing\Options;
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
use Orisai\ObjectMapper\Types\SimpleValueType;
use Orisai\ObjectMapper\Types\Type;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Tests\Orisai\ObjectMapper\Doubles\DefaultsVO;

final class FieldContextTest extends TestCase
{

public function test(): void
{
$deps = (new ObjectMapperTester())->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());
}

}
39 changes: 39 additions & 0 deletions tests/Unit/Context/MappedObjectContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\ObjectMapper\Unit\Context;

use Orisai\ObjectMapper\Context\MappedObjectContext;
use Orisai\ObjectMapper\Processing\Options;
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
use Orisai\ObjectMapper\Types\MappedObjectType;
use Orisai\ObjectMapper\Types\Type;
use PHPUnit\Framework\TestCase;
use Tests\Orisai\ObjectMapper\Doubles\DefaultsVO;

final class MappedObjectContextTest extends TestCase
{

public function test(): void
{
$deps = (new ObjectMapperTester())->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());
}

}
33 changes: 33 additions & 0 deletions tests/Unit/Context/ProcessorCallContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\ObjectMapper\Unit\Context;

use Orisai\ObjectMapper\Context\ProcessorCallContext;
use Orisai\ObjectMapper\Processing\ObjectCreator;
use Orisai\ObjectMapper\Processing\ObjectHolder;
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
use PHPUnit\Framework\TestCase;
use Tests\Orisai\ObjectMapper\Doubles\DefaultsVO;

final class ProcessorCallContextTest extends TestCase
{

public function test(): void
{
$deps = (new ObjectMapperTester())->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());
}

}
91 changes: 91 additions & 0 deletions tests/Unit/Context/TypeContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\ObjectMapper\Unit\Context;

use Orisai\ObjectMapper\Context\TypeContext;
use Orisai\ObjectMapper\Processing\Options;
use Orisai\ObjectMapper\Rules\StringRule;
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
use PHPUnit\Framework\TestCase;
use Tests\Orisai\ObjectMapper\Doubles\DefaultsVO;
use Tests\Orisai\ObjectMapper\Doubles\NoDefaultsVO;
use Throwable;

final class TypeContextTest extends TestCase
{

public function testDependencies(): void
{
$deps = (new ObjectMapperTester())->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(),
);
}

}

0 comments on commit 65316c8

Please sign in to comment.