From 9ee2cc471ebded7b0b4416d1ed245752090f32be Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Mon, 6 Dec 2021 13:03:19 +0100 Subject: [PATCH] fix: handle integer value match properly --- src/Type/Types/IntegerValueType.php | 15 ++++++++-- .../Unit/Type/Types/IntegerValueTypeTest.php | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Type/Types/IntegerValueType.php b/src/Type/Types/IntegerValueType.php index bf0d656..c511bd1 100644 --- a/src/Type/Types/IntegerValueType.php +++ b/src/Type/Types/IntegerValueType.php @@ -42,8 +42,19 @@ final class IntegerValueType implements IntegerType, FixedType return $other->isMatchedBy($this); } - return $other instanceof IntegerType - || $other instanceof MixedType; + if ($other instanceof NativeIntegerType || $other instanceof MixedType) { + return true; + } + + if ($other instanceof NegativeIntegerType && $this->value < 0) { + return true; + } + + if ($other instanceof PositiveIntegerType && $this->value > 0) { + return true; + } + + return false; } public function canCast($value): bool diff --git a/tests/Unit/Type/Types/IntegerValueTypeTest.php b/tests/Unit/Type/Types/IntegerValueTypeTest.php index 76cb547..1ecc6c0 100644 --- a/tests/Unit/Type/Types/IntegerValueTypeTest.php +++ b/tests/Unit/Type/Types/IntegerValueTypeTest.php @@ -9,6 +9,9 @@ use CuyZ\Valinor\Type\Types\Exception\InvalidIntegerValue; use CuyZ\Valinor\Type\Types\Exception\InvalidIntegerValueType; use CuyZ\Valinor\Type\Types\IntegerValueType; use CuyZ\Valinor\Type\Types\MixedType; +use CuyZ\Valinor\Type\Types\NativeIntegerType; +use CuyZ\Valinor\Type\Types\NegativeIntegerType; +use CuyZ\Valinor\Type\Types\PositiveIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\TestCase; use stdClass; @@ -119,6 +122,26 @@ final class IntegerValueTypeTest extends TestCase self::assertFalse((new IntegerValueType(1337))->matches(new IntegerValueType(42))); } + public function test_matches_positive_integer_when_value_is_positive(): void + { + self::assertTrue((new IntegerValueType(1337))->matches(new PositiveIntegerType())); + } + + public function test_does_not_match_positive_integer_when_value_is_negative(): void + { + self::assertFalse((new IntegerValueType(-1337))->matches(new PositiveIntegerType())); + } + + public function test_matches_negative_integer_when_value_is_negative(): void + { + self::assertTrue((new IntegerValueType(-1337))->matches(new NegativeIntegerType())); + } + + public function test_does_not_match_negative_integer_when_value_is_positive(): void + { + self::assertFalse((new IntegerValueType(1337))->matches(new NegativeIntegerType())); + } + public function test_does_not_match_other_type(): void { self::assertFalse($this->type->matches(new FakeType())); @@ -129,6 +152,11 @@ final class IntegerValueTypeTest extends TestCase self::assertTrue($this->type->matches(new MixedType())); } + public function test_matches_native_integer_type(): void + { + self::assertTrue($this->type->matches(new NativeIntegerType())); + } + public function test_matches_union_type_containing_integer_type(): void { $union = new UnionType(new FakeType(), new IntegerValueType(1337), new FakeType());