Skip to content

Commit

Permalink
Rules: unit test args resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jan 1, 2025
1 parent e014ee2 commit 5362d3c
Show file tree
Hide file tree
Showing 18 changed files with 621 additions and 51 deletions.
70 changes: 45 additions & 25 deletions tests/Unit/Rules/AllOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

namespace Tests\Orisai\ObjectMapper\Unit\Rules;

use Orisai\Exceptions\Logic\InvalidArgument;
use Generator;
use Orisai\ObjectMapper\Args\EmptyArgs;
use Orisai\ObjectMapper\Exception\ValueDoesNotMatch;
use Orisai\ObjectMapper\Meta\Compile\RuleCompileMeta;
use Orisai\ObjectMapper\Meta\Runtime\RuleRuntimeMeta;
use Orisai\ObjectMapper\Rules\AllOfRule;
use Orisai\ObjectMapper\Rules\CompoundArgs;
use Orisai\ObjectMapper\Rules\IntArgs;
use Orisai\ObjectMapper\Rules\IntRule;
use Orisai\ObjectMapper\Rules\MappedObjectArgs;
use Orisai\ObjectMapper\Rules\MappedObjectRule;
use Orisai\ObjectMapper\Rules\MixedRule;
use Orisai\ObjectMapper\Rules\NullArgs;
use Orisai\ObjectMapper\Rules\NullRule;
use Orisai\ObjectMapper\Types\CompoundType;
use Orisai\ObjectMapper\Types\CompoundTypeOperator;
use Orisai\ObjectMapper\Types\MessageType;
use Orisai\ObjectMapper\Types\SimpleValueType;
use Tests\Orisai\ObjectMapper\Doubles\DefaultsVO;
use Tests\Orisai\ObjectMapper\Doubles\Rules\AlwaysInvalidRule;
use Tests\Orisai\ObjectMapper\Toolkit\ProcessingTestCase;
use function sprintf;

final class AllOfRuleTest extends ProcessingTestCase
{
Expand All @@ -33,6 +36,46 @@ protected function setUp(): void
$this->ruleManager->addRule(new AlwaysInvalidRule());
}

/**
* @param array<mixed> $args
*
* @dataProvider provideResolveValid
*/
public function testResolveValid(array $args, CompoundArgs $expectedArgs): void
{
$resolvedArgs = $this->rule->resolveArgs($args, $this->argsFieldContext());
self::assertEquals($expectedArgs, $resolvedArgs);
}

public static function provideResolveValid(): Generator
{
yield [
[
AllOfRule::Rules => [
new RuleCompileMeta(MixedRule::class),
new RuleCompileMeta(MixedRule::class),
],
],
new CompoundArgs([
new RuleRuntimeMeta(MixedRule::class, new EmptyArgs()),
new RuleRuntimeMeta(MixedRule::class, new EmptyArgs()),
]),
];

yield [
[
AllOfRule::Rules => [
new RuleCompileMeta(IntRule::class),
new RuleCompileMeta(NullRule::class),
],
],
new CompoundArgs([
new RuleRuntimeMeta(IntRule::class, new IntArgs(0, 0, false, false)),
new RuleRuntimeMeta(NullRule::class, new NullArgs(false)),
]),
];
}

public function testProcessValid(): void
{
$processed = $this->rule->processValue(
Expand Down Expand Up @@ -147,27 +190,4 @@ public function testType(): void
self::assertInstanceOf(MessageType::class, $subtypes[2]);
}

public function testInnerRuleResolved(): void
{
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage(
sprintf(
'"%s" does not accept any arguments, "foo" given',
MixedRule::class,
),
);

$this->rule->resolveArgs(
[
AllOfRule::Rules => [
new RuleCompileMeta(MixedRule::class),
new RuleCompileMeta(MixedRule::class, [
'foo' => 'bar',
]),
],
],
$this->argsFieldContext(),
);
}

}
70 changes: 45 additions & 25 deletions tests/Unit/Rules/AnyOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

namespace Tests\Orisai\ObjectMapper\Unit\Rules;

use Orisai\Exceptions\Logic\InvalidArgument;
use Generator;
use Orisai\ObjectMapper\Args\EmptyArgs;
use Orisai\ObjectMapper\Exception\ValueDoesNotMatch;
use Orisai\ObjectMapper\Meta\Compile\RuleCompileMeta;
use Orisai\ObjectMapper\Meta\Runtime\RuleRuntimeMeta;
use Orisai\ObjectMapper\Rules\AnyOfRule;
use Orisai\ObjectMapper\Rules\CompoundArgs;
use Orisai\ObjectMapper\Rules\IntArgs;
use Orisai\ObjectMapper\Rules\IntRule;
use Orisai\ObjectMapper\Rules\MappedObjectArgs;
use Orisai\ObjectMapper\Rules\MappedObjectRule;
use Orisai\ObjectMapper\Rules\MixedRule;
use Orisai\ObjectMapper\Rules\NullArgs;
use Orisai\ObjectMapper\Rules\NullRule;
use Orisai\ObjectMapper\Types\CompoundType;
use Orisai\ObjectMapper\Types\CompoundTypeOperator;
use Orisai\ObjectMapper\Types\MessageType;
use Orisai\ObjectMapper\Types\SimpleValueType;
use Tests\Orisai\ObjectMapper\Doubles\DefaultsVO;
use Tests\Orisai\ObjectMapper\Doubles\Rules\AlwaysInvalidRule;
use Tests\Orisai\ObjectMapper\Toolkit\ProcessingTestCase;
use function sprintf;

final class AnyOfRuleTest extends ProcessingTestCase
{
Expand All @@ -33,6 +36,46 @@ protected function setUp(): void
$this->ruleManager->addRule(new AlwaysInvalidRule());
}

/**
* @param array<mixed> $args
*
* @dataProvider provideResolveValid
*/
public function testResolveValid(array $args, CompoundArgs $expectedArgs): void
{
$resolvedArgs = $this->rule->resolveArgs($args, $this->argsFieldContext());
self::assertEquals($expectedArgs, $resolvedArgs);
}

public static function provideResolveValid(): Generator
{
yield [
[
AnyOfRule::Rules => [
new RuleCompileMeta(MixedRule::class),
new RuleCompileMeta(MixedRule::class),
],
],
new CompoundArgs([
new RuleRuntimeMeta(MixedRule::class, new EmptyArgs()),
new RuleRuntimeMeta(MixedRule::class, new EmptyArgs()),
]),
];

yield [
[
AnyOfRule::Rules => [
new RuleCompileMeta(IntRule::class),
new RuleCompileMeta(NullRule::class),
],
],
new CompoundArgs([
new RuleRuntimeMeta(IntRule::class, new IntArgs(0, 0, false, false)),
new RuleRuntimeMeta(NullRule::class, new NullArgs(false)),
]),
];
}

public function testProcessValid(): void
{
$processed = $this->rule->processValue(
Expand Down Expand Up @@ -143,27 +186,4 @@ public function testType(): void
self::assertInstanceOf(MessageType::class, $subtypes[2]);
}

public function testInnerRuleResolved(): void
{
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage(
sprintf(
'"%s" does not accept any arguments, "foo" given',
MixedRule::class,
),
);

$this->rule->resolveArgs(
[
AnyOfRule::Rules => [
new RuleCompileMeta(MixedRule::class),
new RuleCompileMeta(MixedRule::class, [
'foo' => 'bar',
]),
],
],
$this->argsFieldContext(),
);
}

}
39 changes: 39 additions & 0 deletions tests/Unit/Rules/ArrayEnumRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,45 @@ protected function setUp(): void
$this->rule = new ArrayEnumRule();
}

/**
* @param array<mixed> $args
*
* @dataProvider provideResolveValid
*/
public function testResolveValid(array $args, ArrayEnumArgs $expectedArgs): void
{
$resolvedArgs = $this->rule->resolveArgs($args, $this->argsFieldContext());
self::assertEquals($expectedArgs, $resolvedArgs);
}

public static function provideResolveValid(): Generator
{
yield [
[
ArrayEnumRule::Cases => ['foo'],
ArrayEnumRule::UseKeys => false,
ArrayEnumRule::AllowUnknown => true,
],
new ArrayEnumArgs(['foo'], false, true),
];

yield [
[
ArrayEnumRule::Cases => [123, 456],
ArrayEnumRule::UseKeys => true,
ArrayEnumRule::AllowUnknown => false,
],
new ArrayEnumArgs([123, 456], true, false),
];

yield [
[
ArrayEnumRule::Cases => ['foo', 456],
],
new ArrayEnumArgs(['foo', 456], false, false),
];
}

/**
* @param mixed $given
* @param mixed $expected
Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/Rules/ArrayOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace Tests\Orisai\ObjectMapper\Unit\Rules;

use Generator;
use Orisai\ObjectMapper\Args\EmptyArgs;
use Orisai\ObjectMapper\Exception\ValueDoesNotMatch;
use Orisai\ObjectMapper\Exception\WithTypeAndValue;
use Orisai\ObjectMapper\Meta\Compile\RuleCompileMeta;
use Orisai\ObjectMapper\Meta\Runtime\RuleRuntimeMeta;
use Orisai\ObjectMapper\Meta\Shared\DefaultValueMeta;
use Orisai\ObjectMapper\Rules\ArrayOfArgs;
use Orisai\ObjectMapper\Rules\ArrayOfRule;
use Orisai\ObjectMapper\Rules\IntRule;
use Orisai\ObjectMapper\Rules\MixedRule;
use Orisai\ObjectMapper\Rules\ScalarRule;
use Orisai\ObjectMapper\Rules\StringArgs;
use Orisai\ObjectMapper\Rules\StringRule;
use Orisai\ObjectMapper\Types\GenericArrayType;
use Orisai\ObjectMapper\Types\SimpleValueType;
Expand All @@ -30,6 +34,50 @@ protected function setUp(): void
$this->ruleManager->addRule(new AlwaysInvalidRule());
}

/**
* @param array<mixed> $args
*
* @dataProvider provideResolveValid
*/
public function testResolveValid(array $args, ArrayOfArgs $expectedArgs): void
{
$resolvedArgs = $this->rule->resolveArgs($args, $this->argsFieldContext());
self::assertEquals($expectedArgs, $resolvedArgs);
}

public static function provideResolveValid(): Generator
{
yield [
[
ArrayOfRule::ItemRule => new RuleCompileMeta(MixedRule::class),
],
new ArrayOfArgs(
new RuleRuntimeMeta(MixedRule::class, new EmptyArgs()),
null,
null,
null,
false,
),
];

yield [
[
ArrayOfRule::ItemRule => new RuleCompileMeta(ScalarRule::class),
ArrayOfRule::KeyRule => new RuleCompileMeta(StringRule::class),
ArrayOfRule::MinItems => 1,
ArrayOfRule::MaxItems => 10,
ArrayOfRule::MergeDefaults => true,
],
new ArrayOfArgs(
new RuleRuntimeMeta(ScalarRule::class, new EmptyArgs()),
new RuleRuntimeMeta(StringRule::class, new StringArgs(null, false, null, null)),
1,
10,
true,
),
];
}

public function testProcessValid(): void
{
$value = ['foo', 'bar', 'baz', 123];
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Rules/ArrayShapeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Tests\Orisai\ObjectMapper\Unit\Rules;

use Generator;
use Orisai\ObjectMapper\Args\EmptyArgs;
use Orisai\ObjectMapper\Exception\ValueDoesNotMatch;
use Orisai\ObjectMapper\Meta\Compile\RuleCompileMeta;
use Orisai\ObjectMapper\Meta\Runtime\RuleRuntimeMeta;
use Orisai\ObjectMapper\Meta\Shared\DefaultValueMeta;
use Orisai\ObjectMapper\Rules\ArrayShapeArgs;
Expand All @@ -13,6 +15,8 @@
use Orisai\ObjectMapper\Rules\MixedRule;
use Orisai\ObjectMapper\Rules\NullArgs;
use Orisai\ObjectMapper\Rules\NullRule;
use Orisai\ObjectMapper\Rules\ScalarRule;
use Orisai\ObjectMapper\Rules\StringArgs;
use Orisai\ObjectMapper\Rules\StringRule;
use Orisai\ObjectMapper\Types\ArrayShapeType;
use Orisai\ObjectMapper\Types\SimpleValueType;
Expand All @@ -32,6 +36,44 @@ protected function setUp(): void
$this->ruleManager->addRule(new AlwaysInvalidRule());
}

/**
* @param array<mixed> $args
*
* @dataProvider provideResolveValid
*/
public function testResolveValid(array $args, ArrayShapeArgs $expectedArgs): void
{
$resolvedArgs = $this->rule->resolveArgs($args, $this->argsFieldContext());
self::assertEquals($expectedArgs, $resolvedArgs);
}

public static function provideResolveValid(): Generator
{
yield [
[
ArrayShapeRule::Fields => [
'foo' => new RuleCompileMeta(MixedRule::class),
],
],
new ArrayShapeArgs([
'foo' => new RuleRuntimeMeta(MixedRule::class, new EmptyArgs()),
]),
];

yield [
[
ArrayShapeRule::Fields => [
1 => new RuleCompileMeta(ScalarRule::class),
'foo' => new RuleCompileMeta(StringRule::class),
],
],
new ArrayShapeArgs([
1 => new RuleRuntimeMeta(ScalarRule::class, new EmptyArgs()),
'foo' => new RuleRuntimeMeta(StringRule::class, new StringArgs(null, false, null, null)),
]),
];
}

public function testProcessValid(): void
{
$value = [
Expand Down
Loading

0 comments on commit 5362d3c

Please sign in to comment.