diff --git a/tests/Unit/Type/Types/ArrayKeyTypeTest.php b/tests/Unit/Type/Types/ArrayKeyTypeTest.php index 050591ad..0268a54e 100644 --- a/tests/Unit/Type/Types/ArrayKeyTypeTest.php +++ b/tests/Unit/Type/Types/ArrayKeyTypeTest.php @@ -11,6 +11,7 @@ use CuyZ\Valinor\Type\Types\NativeIntegerType; use CuyZ\Valinor\Type\Types\NativeStringType; use LogicException; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -32,29 +33,37 @@ public function test_string_values_are_correct(): void self::assertSame('string', ArrayKeyType::string()->toString()); } - public function test_accepts_correct_values(): void + #[TestWith(['accepts' => true, 'value' => 42])] + #[TestWith(['accepts' => true, 'value' => 'foo'])] + public function test_default_array_key_type_accepts_correct_values(bool $accepts, mixed $value): void { - $arrayKeyDefault = ArrayKeyType::default(); - $arrayKeyInteger = ArrayKeyType::integer(); - $arrayKeyString = ArrayKeyType::string(); - - self::assertTrue($arrayKeyDefault->accepts('foo')); - self::assertTrue($arrayKeyDefault->accepts(42)); + self::assertSame($accepts, ArrayKeyType::default()->accepts($value)); + } - self::assertFalse($arrayKeyInteger->accepts('foo')); - self::assertTrue($arrayKeyInteger->accepts(42)); + #[TestWith(['accepts' => true, 'value' => 42])] + #[TestWith(['accepts' => false, 'value' => 'foo'])] + public function test_integer_array_key_type_accepts_correct_values(bool $accepts, mixed $value): void + { + self::assertSame($accepts, ArrayKeyType::integer()->accepts($value)); + } - self::assertTrue($arrayKeyString->accepts('foo')); - self::assertTrue($arrayKeyString->accepts(42)); + #[TestWith(['accepts' => true, 'value' => 'foo'])] + #[TestWith(['accepts' => true, 'value' => 42])] + public function test_string_array_key_type_accepts_correct_values(bool $accepts, mixed $value): void + { + self::assertSame($accepts, ArrayKeyType::string()->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse(ArrayKeyType::default()->accepts(null)); - self::assertFalse(ArrayKeyType::default()->accepts(42.1337)); - self::assertFalse(ArrayKeyType::default()->accepts(['foo' => 'bar'])); - self::assertFalse(ArrayKeyType::default()->accepts(false)); - self::assertFalse(ArrayKeyType::default()->accepts(new stdClass())); + self::assertFalse(ArrayKeyType::default()->accepts($value)); + self::assertFalse(ArrayKeyType::integer()->accepts($value)); + self::assertFalse(ArrayKeyType::string()->accepts($value)); } public function test_default_array_key_can_cast_numeric_and_string_value(): void diff --git a/tests/Unit/Type/Types/ArrayTypeTest.php b/tests/Unit/Type/Types/ArrayTypeTest.php index 513dc951..1ec2025a 100644 --- a/tests/Unit/Type/Types/ArrayTypeTest.php +++ b/tests/Unit/Type/Types/ArrayTypeTest.php @@ -12,6 +12,7 @@ use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\NativeStringType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -71,38 +72,62 @@ public function test_simple_array_subtype_is_correct(): void self::assertSame($subType, ArrayType::simple($subType)->subType()); } - public function test_accepts_correct_values(): void + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 1337.404]])] + public function test_native_array_accepts_correct_values(bool $accepts, mixed $value): void { - $type = FakeType::accepting('Some value'); + $type = ArrayType::native(); - $arrayWithDefaultKey = new ArrayType(ArrayKeyType::default(), $type); - $arrayWithIntegerKey = new ArrayType(ArrayKeyType::integer(), $type); - $arrayWithStringKey = new ArrayType(ArrayKeyType::string(), $type); + self::assertSame($accepts, $type->accepts($value)); + } + + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_default_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new ArrayType(ArrayKeyType::default(), FakeType::accepting('Some value')); + + self::assertSame($accepts, $type->accepts($value)); + } - self::assertTrue($arrayWithDefaultKey->accepts([42 => 'Some value'])); - self::assertTrue($arrayWithDefaultKey->accepts(['foo' => 'Some value'])); - self::assertFalse($arrayWithDefaultKey->accepts([42 => 1337.404])); - self::assertFalse($arrayWithDefaultKey->accepts(['foo' => 1337.404])); + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_integer_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new ArrayType(ArrayKeyType::integer(), FakeType::accepting('Some value')); - self::assertTrue($arrayWithIntegerKey->accepts([42 => 'Some value'])); - self::assertFalse($arrayWithIntegerKey->accepts(['foo' => 'Some value'])); - self::assertFalse($arrayWithIntegerKey->accepts([42 => 1337.404])); + self::assertSame($accepts, $type->accepts($value)); + } - self::assertTrue($arrayWithStringKey->accepts([42 => 'Some value'])); - self::assertTrue($arrayWithStringKey->accepts(['foo' => 'Some value'])); - self::assertFalse($arrayWithIntegerKey->accepts([42 => 1337.404])); + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_string_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new ArrayType(ArrayKeyType::string(), FakeType::accepting('Some value')); - self::assertFalse(ArrayType::native()->accepts('foo')); + self::assertSame($accepts, $type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse(ArrayType::native()->accepts(null)); - self::assertFalse(ArrayType::native()->accepts('Schwifty!')); - self::assertFalse(ArrayType::native()->accepts(42.1337)); - self::assertFalse(ArrayType::native()->accepts(404)); - self::assertFalse(ArrayType::native()->accepts(false)); - self::assertFalse(ArrayType::native()->accepts(new stdClass())); + self::assertFalse(ArrayType::native()->accepts($value)); + self::assertFalse((new ArrayType(ArrayKeyType::default(), FakeType::accepting('Some value')))->accepts($value)); + self::assertFalse((new ArrayType(ArrayKeyType::integer(), FakeType::accepting('Some value')))->accepts($value)); + self::assertFalse((new ArrayType(ArrayKeyType::string(), FakeType::accepting('Some value')))->accepts($value)); } public function test_matches_valid_array_type(): void diff --git a/tests/Unit/Type/Types/BooleanValueTypeTest.php b/tests/Unit/Type/Types/BooleanValueTypeTest.php index d0e35f44..a846fc60 100644 --- a/tests/Unit/Type/Types/BooleanValueTypeTest.php +++ b/tests/Unit/Type/Types/BooleanValueTypeTest.php @@ -10,6 +10,7 @@ use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\NativeBooleanType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -27,29 +28,30 @@ public function test_string_value_is_correct(): void self::assertSame('false', BooleanValueType::false()->toString()); } - public function test_accepts_correct_values(): void + #[TestWith(['accepts' => true, 'value' => true])] + #[TestWith(['accepts' => false, 'value' => false])] + public function test_true_accepts_correct_values(bool $accepts, mixed $value): void { - self::assertTrue(BooleanValueType::true()->accepts(true)); - self::assertTrue(BooleanValueType::false()->accepts(false)); + self::assertSame($accepts, BooleanValueType::true()->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith(['accepts' => true, 'value' => false])] + #[TestWith(['accepts' => false, 'value' => true])] + public function test_false_accepts_correct_values(bool $accepts, mixed $value): void { - self::assertFalse(BooleanValueType::true()->accepts('Schwifty!')); - self::assertFalse(BooleanValueType::true()->accepts(42.1337)); - self::assertFalse(BooleanValueType::true()->accepts(404)); - self::assertFalse(BooleanValueType::true()->accepts(['foo' => 'bar'])); - self::assertFalse(BooleanValueType::true()->accepts(false)); - self::assertFalse(BooleanValueType::true()->accepts(null)); - self::assertFalse(BooleanValueType::true()->accepts(new stdClass())); + self::assertSame($accepts, BooleanValueType::false()->accepts($value)); + } - self::assertFalse(BooleanValueType::false()->accepts('Schwifty!')); - self::assertFalse(BooleanValueType::false()->accepts(42.1337)); - self::assertFalse(BooleanValueType::false()->accepts(404)); - self::assertFalse(BooleanValueType::false()->accepts(['foo' => 'bar'])); - self::assertFalse(BooleanValueType::false()->accepts(true)); - self::assertFalse(BooleanValueType::false()->accepts(null)); - self::assertFalse(BooleanValueType::false()->accepts(new stdClass())); + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([null])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void + { + self::assertFalse(BooleanValueType::true()->accepts($value)); + self::assertFalse(BooleanValueType::false()->accepts($value)); } public function test_can_cast_boolean_value(): void diff --git a/tests/Unit/Type/Types/CallableTypeTest.php b/tests/Unit/Type/Types/CallableTypeTest.php index e9bcb15f..770cf921 100644 --- a/tests/Unit/Type/Types/CallableTypeTest.php +++ b/tests/Unit/Type/Types/CallableTypeTest.php @@ -9,6 +9,7 @@ use CuyZ\Valinor\Type\Types\CallableType; use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -30,15 +31,16 @@ public function test_accepts_correct_values(): void self::assertTrue($this->callableType->accepts(fn () => null)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([true])] + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->callableType->accepts(true)); - self::assertFalse($this->callableType->accepts(null)); - self::assertFalse($this->callableType->accepts('Schwifty!')); - self::assertFalse($this->callableType->accepts(42.1337)); - self::assertFalse($this->callableType->accepts(404)); - self::assertFalse($this->callableType->accepts(['foo' => 'bar'])); - self::assertFalse($this->callableType->accepts(new stdClass())); + self::assertFalse($this->callableType->accepts($value)); } public function test_string_value_is_correct(): void diff --git a/tests/Unit/Type/Types/ClassStringTypeTest.php b/tests/Unit/Type/Types/ClassStringTypeTest.php index 857d012f..37142201 100644 --- a/tests/Unit/Type/Types/ClassStringTypeTest.php +++ b/tests/Unit/Type/Types/ClassStringTypeTest.php @@ -19,6 +19,7 @@ use DateTime; use DateTimeImmutable; use DateTimeInterface; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -51,57 +52,46 @@ public function test_union_with_invalid_type_throws_exception(): void new ClassStringType($type); } - public function test_accepts_correct_values(): void + #[TestWith([stdClass::class])] + #[TestWith([DateTimeInterface::class])] + public function test_basic_class_string_accepts_correct_values(mixed $value): void { - $classStringType = new ClassStringType(); - - self::assertTrue($classStringType->accepts(stdClass::class)); - self::assertTrue($classStringType->accepts(DateTimeInterface::class)); - } - - public function test_does_not_accept_incorrect_values(): void - { - $classStringType = new ClassStringType(); - - self::assertFalse($classStringType->accepts(null)); - self::assertFalse($classStringType->accepts('Schwifty!')); - self::assertFalse($classStringType->accepts(42.1337)); - self::assertFalse($classStringType->accepts(404)); - self::assertFalse($classStringType->accepts(['foo' => 'bar'])); - self::assertFalse($classStringType->accepts(false)); - self::assertFalse($classStringType->accepts(new stdClass())); + self::assertTrue((new ClassStringType())->accepts($value)); } - public function test_accepts_correct_values_with_sub_type(): void + #[TestWith(['accepts' => true, 'value' => DateTime::class])] + #[TestWith(['accepts' => true, 'value' => DateTimeImmutable::class])] + #[TestWith(['accepts' => true, 'value' => DateTimeInterface::class])] + #[TestWith(['accepts' => false, 'value' => stdClass::class])] + public function test_object_class_string_accepts_correct_values(bool $accepts, mixed $value): void { - $objectType = new FakeObjectType(DateTimeInterface::class); - $classStringType = new ClassStringType($objectType); - - self::assertTrue($classStringType->accepts(DateTime::class)); - self::assertTrue($classStringType->accepts(DateTimeImmutable::class)); - self::assertTrue($classStringType->accepts(DateTimeInterface::class)); + $type = new ClassStringType(new FakeObjectType(DateTimeInterface::class)); - self::assertFalse($classStringType->accepts(stdClass::class)); + self::assertSame($accepts, $type->accepts($value)); } - public function test_accepts_correct_values_with_union_sub_type(): void + #[TestWith(['accepts' => true, 'value' => DateTime::class])] + #[TestWith(['accepts' => true, 'value' => stdClass::class])] + #[TestWith(['accepts' => false, 'value' => DateTimeImmutable::class])] + public function test_union_of_objects_class_string_accepts_correct_values(bool $accepts, mixed $value): void { - $type = new UnionType(new FakeObjectType(DateTimeInterface::class), new FakeObjectType(stdClass::class)); - $classStringType = new ClassStringType($type); + $type = new ClassStringType(new UnionType(new FakeObjectType(DateTime::class), new FakeObjectType(stdClass::class))); - self::assertTrue($classStringType->accepts(DateTime::class)); - self::assertTrue($classStringType->accepts(DateTimeImmutable::class)); - self::assertTrue($classStringType->accepts(DateTimeInterface::class)); - - self::assertTrue($classStringType->accepts(stdClass::class)); + self::assertSame($accepts, $type->accepts($value)); } - public function test_does_not_accept_incorrect_values_with_union_sub_type(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - $unionType = new UnionType(new FakeObjectType(DateTime::class), new FakeObjectType(stdClass::class)); - $classStringType = new ClassStringType($unionType); - - self::assertFalse($classStringType->accepts(DateTimeImmutable::class)); + self::assertFalse((new ClassStringType())->accepts($value)); + self::assertFalse((new ClassStringType(new FakeObjectType()))->accepts($value)); + self::assertFalse((new ClassStringType(new UnionType(new FakeObjectType(), new FakeObjectType())))->accepts($value)); } public function test_can_cast_stringable_value(): void diff --git a/tests/Unit/Type/Types/EnumTypeTest.php b/tests/Unit/Type/Types/EnumTypeTest.php index b906a3bb..65634d98 100644 --- a/tests/Unit/Type/Types/EnumTypeTest.php +++ b/tests/Unit/Type/Types/EnumTypeTest.php @@ -12,6 +12,7 @@ use CuyZ\Valinor\Type\Types\EnumType; use CuyZ\Valinor\Type\Types\UndefinedObjectType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -39,41 +40,42 @@ public function test_class_name_can_be_retrieved(): void self::assertSame(BackedIntegerEnum::class, $this->backedIntegerEnumType->className()); } - public function test_accepts_correct_values(): void + #[TestWith(['accepts' => true, 'value' => PureEnum::FOO])] + #[TestWith(['accepts' => false, 'value' => BackedStringEnum::FOO])] + #[TestWith(['accepts' => false, 'value' => BackedIntegerEnum::FOO])] + public function test_pure_enum_accepts_correct_values(bool $accepts, mixed $value): void { - self::assertTrue($this->pureEnumType->accepts(PureEnum::FOO)); - self::assertTrue($this->backedStringEnumType->accepts(BackedStringEnum::FOO)); - self::assertTrue($this->backedIntegerEnumType->accepts(BackedIntegerEnum::FOO)); + self::assertSame($accepts, $this->pureEnumType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith(['accepts' => true, 'value' => BackedStringEnum::FOO])] + #[TestWith(['accepts' => false, 'value' => PureEnum::FOO])] + #[TestWith(['accepts' => false, 'value' => BackedIntegerEnum::FOO])] + public function test_backed_string_enum_accepts_correct_values(bool $accepts, mixed $value): void { - self::assertFalse($this->pureEnumType->accepts(null)); - self::assertFalse($this->pureEnumType->accepts('Schwifty!')); - self::assertFalse($this->pureEnumType->accepts(42.1337)); - self::assertFalse($this->pureEnumType->accepts(404)); - self::assertFalse($this->pureEnumType->accepts(['foo' => 'bar'])); - self::assertFalse($this->pureEnumType->accepts(false)); - self::assertFalse($this->pureEnumType->accepts(new stdClass())); - self::assertFalse($this->pureEnumType->accepts(BackedIntegerEnum::FOO)); - - self::assertFalse($this->backedStringEnumType->accepts(null)); - self::assertFalse($this->backedStringEnumType->accepts('Schwifty!')); - self::assertFalse($this->backedStringEnumType->accepts(42.1337)); - self::assertFalse($this->backedStringEnumType->accepts(404)); - self::assertFalse($this->backedStringEnumType->accepts(['foo' => 'bar'])); - self::assertFalse($this->backedStringEnumType->accepts(false)); - self::assertFalse($this->backedStringEnumType->accepts(new stdClass())); - self::assertFalse($this->backedStringEnumType->accepts(PureEnum::FOO)); - - self::assertFalse($this->backedIntegerEnumType->accepts(null)); - self::assertFalse($this->backedIntegerEnumType->accepts('Schwifty!')); - self::assertFalse($this->backedIntegerEnumType->accepts(42.1337)); - self::assertFalse($this->backedIntegerEnumType->accepts(404)); - self::assertFalse($this->backedIntegerEnumType->accepts(['foo' => 'bar'])); - self::assertFalse($this->backedIntegerEnumType->accepts(false)); - self::assertFalse($this->backedIntegerEnumType->accepts(new stdClass())); - self::assertFalse($this->backedIntegerEnumType->accepts(BackedStringEnum::FOO)); + self::assertSame($accepts, $this->backedStringEnumType->accepts($value)); + } + + #[TestWith(['accepts' => true, 'value' => BackedIntegerEnum::FOO])] + #[TestWith(['accepts' => false, 'value' => PureEnum::FOO])] + #[TestWith(['accepts' => false, 'value' => BackedStringEnum::FOO])] + public function test_backed_integer_enum_accepts_correct_values(bool $accepts, mixed $value): void + { + self::assertSame($accepts, $this->backedIntegerEnumType->accepts($value)); + } + + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void + { + self::assertFalse($this->pureEnumType->accepts($value)); + self::assertFalse($this->backedStringEnumType->accepts($value)); + self::assertFalse($this->backedIntegerEnumType->accepts($value)); } public function test_string_value_is_correct(): void diff --git a/tests/Unit/Type/Types/FloatValueTypeTest.php b/tests/Unit/Type/Types/FloatValueTypeTest.php index e70591d0..51bb3e25 100644 --- a/tests/Unit/Type/Types/FloatValueTypeTest.php +++ b/tests/Unit/Type/Types/FloatValueTypeTest.php @@ -10,6 +10,7 @@ use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\NativeFloatType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -29,20 +30,22 @@ public function test_value_can_be_retrieved(): void self::assertSame(1337.42, $this->floatValueType->value()); } - public function test_accepts_correct_values(): void + #[TestWith([1337.42])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->floatValueType->accepts(1337.42)); + self::assertTrue($this->floatValueType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([404])] + #[TestWith([404.42])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->floatValueType->accepts(null)); - self::assertFalse($this->floatValueType->accepts('Schwifty!')); - self::assertFalse($this->floatValueType->accepts(404)); - self::assertFalse($this->floatValueType->accepts(404.42)); - self::assertFalse($this->floatValueType->accepts(['foo' => 'bar'])); - self::assertFalse($this->floatValueType->accepts(false)); - self::assertFalse($this->floatValueType->accepts(new stdClass())); + self::assertFalse($this->floatValueType->accepts($value)); } public function test_can_cast_float_value(): void diff --git a/tests/Unit/Type/Types/IntegerRangeTypeTest.php b/tests/Unit/Type/Types/IntegerRangeTypeTest.php index 9ba9a508..abbe2856 100644 --- a/tests/Unit/Type/Types/IntegerRangeTypeTest.php +++ b/tests/Unit/Type/Types/IntegerRangeTypeTest.php @@ -15,6 +15,7 @@ use CuyZ\Valinor\Type\Types\PositiveIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -47,23 +48,25 @@ public function test_range_with_same_min_greater_than_max_throws_exception(): vo new IntegerRangeType(1337, 42); } - public function test_accepts_correct_values(): void + #[TestWith([-42])] + #[TestWith([0])] + #[TestWith([42])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->type->accepts(-42)); - self::assertTrue($this->type->accepts(0)); - self::assertTrue($this->type->accepts(42)); + self::assertTrue($this->type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([-1337])] + #[TestWith([1337])] + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->type->accepts(-1337)); - self::assertFalse($this->type->accepts(1337)); - self::assertFalse($this->type->accepts(null)); - self::assertFalse($this->type->accepts('Schwifty!')); - self::assertFalse($this->type->accepts(42.1337)); - self::assertFalse($this->type->accepts(['foo' => 'bar'])); - self::assertFalse($this->type->accepts(false)); - self::assertFalse($this->type->accepts(new stdClass())); + self::assertFalse($this->type->accepts($value)); } public function test_can_cast_integer_value(): void diff --git a/tests/Unit/Type/Types/IntegerValueTypeTest.php b/tests/Unit/Type/Types/IntegerValueTypeTest.php index cdd81d9a..92589e36 100644 --- a/tests/Unit/Type/Types/IntegerValueTypeTest.php +++ b/tests/Unit/Type/Types/IntegerValueTypeTest.php @@ -13,6 +13,7 @@ use CuyZ\Valinor\Type\Types\PositiveIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -32,21 +33,23 @@ public function test_value_can_be_retrieved(): void self::assertSame(1337, $this->type->value()); } - public function test_accepts_correct_values(): void + #[TestWith([1337])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->type->accepts(1337)); + self::assertTrue($this->type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([404])] + #[TestWith([-404])] + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->type->accepts(404)); - self::assertFalse($this->type->accepts(-404)); - self::assertFalse($this->type->accepts(null)); - self::assertFalse($this->type->accepts('Schwifty!')); - self::assertFalse($this->type->accepts(42.1337)); - self::assertFalse($this->type->accepts(['foo' => 'bar'])); - self::assertFalse($this->type->accepts(false)); - self::assertFalse($this->type->accepts(new stdClass())); + self::assertFalse($this->type->accepts($value)); } public function test_can_cast_integer_value(): void diff --git a/tests/Unit/Type/Types/InterfaceTypeTest.php b/tests/Unit/Type/Types/InterfaceTypeTest.php index 9f2837dd..7ffb5e97 100644 --- a/tests/Unit/Type/Types/InterfaceTypeTest.php +++ b/tests/Unit/Type/Types/InterfaceTypeTest.php @@ -15,6 +15,7 @@ use DateTimeImmutable; use DateTimeInterface; use Iterator; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -35,25 +36,27 @@ public function test_string_value_is_correct(): void self::assertSame(stdClass::class . "<{$generic->toString()}>", $type->toString()); } - public function test_accepts_correct_values(): void + #[TestWith([new DateTime()])] + #[TestWith([new DateTimeImmutable()])] + public function test_accepts_correct_values(mixed $value): void { $type = new InterfaceType(DateTimeInterface::class); - self::assertTrue($type->accepts(new DateTime())); - self::assertTrue($type->accepts(new DateTimeImmutable())); + self::assertTrue($type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { $type = new InterfaceType(DateTimeInterface::class); - self::assertFalse($type->accepts(null)); - self::assertFalse($type->accepts('Schwifty!')); - self::assertFalse($type->accepts(42.1337)); - self::assertFalse($type->accepts(404)); - self::assertFalse($type->accepts(['foo' => 'bar'])); - self::assertFalse($type->accepts(false)); - self::assertFalse($type->accepts(new stdClass())); + self::assertFalse($type->accepts($value)); } public function test_matches_other_identical_interface(): void diff --git a/tests/Unit/Type/Types/IntersectionTypeTest.php b/tests/Unit/Type/Types/IntersectionTypeTest.php index 5f41767a..7d540e35 100644 --- a/tests/Unit/Type/Types/IntersectionTypeTest.php +++ b/tests/Unit/Type/Types/IntersectionTypeTest.php @@ -10,6 +10,7 @@ use CuyZ\Valinor\Type\Types\IntersectionType; use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -58,21 +59,18 @@ public function test_accepts_correct_values(): void self::assertTrue($intersectionType->accepts($object)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - $typeA = new FakeObjectType(); - $typeB = new FakeObjectType(); - $typeC = new FakeObjectType(); - - $intersectionType = new IntersectionType($typeA, $typeB, $typeC); + $intersectionType = new IntersectionType(new FakeObjectType(), new FakeObjectType()); - self::assertFalse($intersectionType->accepts(null)); - self::assertFalse($intersectionType->accepts('Schwifty!')); - self::assertFalse($intersectionType->accepts(42.1337)); - self::assertFalse($intersectionType->accepts(404)); - self::assertFalse($intersectionType->accepts(['foo' => 'bar'])); - self::assertFalse($intersectionType->accepts(false)); - self::assertFalse($intersectionType->accepts(new stdClass())); + self::assertFalse($intersectionType->accepts($value)); } public function test_matches_valid_type(): void diff --git a/tests/Unit/Type/Types/IterableTypeTest.php b/tests/Unit/Type/Types/IterableTypeTest.php index e057f206..d1ef094e 100644 --- a/tests/Unit/Type/Types/IterableTypeTest.php +++ b/tests/Unit/Type/Types/IterableTypeTest.php @@ -7,11 +7,13 @@ use CuyZ\Valinor\Tests\Fake\Type\FakeCompositeType; use CuyZ\Valinor\Tests\Fake\Type\FakeType; use CuyZ\Valinor\Type\Types\ArrayKeyType; -use CuyZ\Valinor\Type\Types\NativeStringType; use CuyZ\Valinor\Type\Types\IterableType; use CuyZ\Valinor\Type\Types\MixedType; +use CuyZ\Valinor\Type\Types\NativeStringType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; +use stdClass; final class IterableTypeTest extends TestCase { @@ -55,38 +57,90 @@ public function test_subtype_is_correct(): void self::assertSame($subType, (new IterableType(ArrayKeyType::default(), $subType))->subType()); } - public function test_accepts_correct_values(): void + public function test_accepts_generators_with_correct_values(): void { $type = FakeType::accepting('Some value'); + $iterableNative = IterableType::native(); $iterableWithDefaultKey = new IterableType(ArrayKeyType::default(), $type); $iterableWithIntegerKey = new IterableType(ArrayKeyType::integer(), $type); $iterableWithStringKey = new IterableType(ArrayKeyType::string(), $type); - self::assertTrue($iterableWithDefaultKey->accepts([42 => 'Some value'])); - self::assertTrue($iterableWithDefaultKey->accepts(['foo' => 'Some value'])); - self::assertFalse($iterableWithDefaultKey->accepts([42 => 1337.404])); - self::assertFalse($iterableWithDefaultKey->accepts(['foo' => 1337.404])); self::assertTrue($iterableWithDefaultKey->accepts((static fn () => yield 42 => 'Some value')())); self::assertTrue($iterableWithDefaultKey->accepts((static fn () => yield 'foo' => 'Some value')())); self::assertFalse($iterableWithDefaultKey->accepts((static fn () => yield 42 => 1337.404)())); self::assertFalse($iterableWithDefaultKey->accepts((static fn () => yield 'foo' => 1337.404)())); - self::assertTrue($iterableWithIntegerKey->accepts([42 => 'Some value'])); - self::assertFalse($iterableWithIntegerKey->accepts(['foo' => 'Some value'])); - self::assertFalse($iterableWithIntegerKey->accepts([42 => 1337.404])); self::assertTrue($iterableWithIntegerKey->accepts((static fn () => yield 42 => 'Some value')())); self::assertFalse($iterableWithIntegerKey->accepts((static fn () => yield 'foo' => 'Some value')())); self::assertFalse($iterableWithIntegerKey->accepts((static fn () => yield 42 => 1337.404)())); - self::assertTrue($iterableWithStringKey->accepts([42 => 'Some value'])); - self::assertTrue($iterableWithStringKey->accepts(['foo' => 'Some value'])); - self::assertFalse($iterableWithIntegerKey->accepts([42 => 1337.404])); self::assertTrue($iterableWithStringKey->accepts((static fn () => yield 42 => 'Some value')())); self::assertTrue($iterableWithStringKey->accepts((static fn () => yield 'foo' => 'Some value')())); self::assertFalse($iterableWithStringKey->accepts((static fn () => yield 42 => 1337.404)())); - self::assertFalse(IterableType::native()->accepts('foo')); + self::assertTrue($iterableNative->accepts((static fn () => yield 42 => 'Some value')())); + self::assertTrue($iterableNative->accepts((static fn () => yield 'foo' => 'Some value')())); + self::assertTrue($iterableNative->accepts((static fn () => yield 42 => 1337.404)())); + self::assertTrue($iterableNative->accepts((static fn () => yield 'foo' => 1337.404)())); + } + + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 1337.404]])] + public function test_native_array_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = IterableType::native(); + + self::assertSame($accepts, $type->accepts($value)); + } + + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_default_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new IterableType(ArrayKeyType::default(), FakeType::accepting('Some value')); + + self::assertSame($accepts, $type->accepts($value)); + } + + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_integer_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new IterableType(ArrayKeyType::integer(), FakeType::accepting('Some value')); + + self::assertSame($accepts, $type->accepts($value)); + } + + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_string_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new IterableType(ArrayKeyType::string(), FakeType::accepting('Some value')); + + self::assertSame($accepts, $type->accepts($value)); + } + + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void + { + self::assertFalse(IterableType::native()->accepts($value)); + self::assertFalse((new IterableType(ArrayKeyType::default(), FakeType::accepting('Some value')))->accepts($value)); + self::assertFalse((new IterableType(ArrayKeyType::integer(), FakeType::accepting('Some value')))->accepts($value)); + self::assertFalse((new IterableType(ArrayKeyType::string(), FakeType::accepting('Some value')))->accepts($value)); } public function test_matches_valid_iterable_type(): void diff --git a/tests/Unit/Type/Types/ListTypeTest.php b/tests/Unit/Type/Types/ListTypeTest.php index faaabc58..25d982f7 100644 --- a/tests/Unit/Type/Types/ListTypeTest.php +++ b/tests/Unit/Type/Types/ListTypeTest.php @@ -13,6 +13,7 @@ use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\NativeStringType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -57,34 +58,38 @@ public function test_subtype_is_correct(): void self::assertSame($subType, (new ListType($subType))->subType()); } - public function test_accepts_correct_values(): void + #[TestWith(['accepts' => true, 'value' => []])] + #[TestWith(['accepts' => true, 'value' => ['Some value', 'Some value', 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['Some value', 'Schwifty!', 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [1 => 'Some value', 2 => 'Some value']])] + public function test_native_list_type_accepts_correct_values(bool $accepts, mixed $value): void { - $type = FakeType::accepting('Some value'); + self::assertSame($accepts, ListType::native()->accepts($value)); + } - self::assertTrue((new ListType($type))->accepts([ - 'Some value', - 'Some value', - 'Some value', - ])); + #[TestWith(['accepts' => true, 'value' => []])] + #[TestWith(['accepts' => true, 'value' => ['Some value', 'Some value', 'Some value']])] + #[TestWith(['accepts' => false, 'value' => ['Some value', 'Schwifty!', 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [1 => 'Some value', 2 => 'Some value']])] + public function test_list_of_type_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new ListType(FakeType::accepting('Some value')); - self::assertFalse((new ListType($type))->accepts([ - 'Some value', - 'Schwifty!', - 'Some value', - ])); + self::assertSame($accepts, $type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([[1 => 'foo']])] + #[TestWith([['foo' => 'foo']])] + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse(ListType::native()->accepts([1 => 'foo'])); - self::assertFalse(ListType::native()->accepts(['foo' => 'foo'])); - - self::assertFalse(ListType::native()->accepts(null)); - self::assertFalse(ListType::native()->accepts('Schwifty!')); - self::assertFalse(ListType::native()->accepts(42.1337)); - self::assertFalse(ListType::native()->accepts(404)); - self::assertFalse(ListType::native()->accepts(false)); - self::assertFalse(ListType::native()->accepts(new stdClass())); + self::assertFalse(ListType::native()->accepts($value)); + self::assertFalse((new ListType(FakeType::accepting('Some value')))->accepts($value)); } public function test_matches_valid_list_type(): void diff --git a/tests/Unit/Type/Types/MixedTypeTest.php b/tests/Unit/Type/Types/MixedTypeTest.php index 4f14427c..9a388c6c 100644 --- a/tests/Unit/Type/Types/MixedTypeTest.php +++ b/tests/Unit/Type/Types/MixedTypeTest.php @@ -7,6 +7,7 @@ use CuyZ\Valinor\Tests\Fake\Type\FakeType; use CuyZ\Valinor\Tests\Traits\TestIsSingleton; use CuyZ\Valinor\Type\Types\MixedType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -28,15 +29,16 @@ public function test_string_value_is_correct(): void self::assertSame('mixed', $this->mixedType->toString()); } - public function test_accepts_every_value(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_accepts_every_value(mixed $value): void { - self::assertTrue($this->mixedType->accepts(null)); - self::assertTrue($this->mixedType->accepts('Schwifty!')); - self::assertTrue($this->mixedType->accepts(42.1337)); - self::assertTrue($this->mixedType->accepts(404)); - self::assertTrue($this->mixedType->accepts(['foo' => 'bar'])); - self::assertTrue($this->mixedType->accepts(false)); - self::assertTrue($this->mixedType->accepts(new stdClass())); + self::assertTrue($this->mixedType->accepts($value)); } public function test_matches_same_type(): void diff --git a/tests/Unit/Type/Types/NativeBooleanTypeTest.php b/tests/Unit/Type/Types/NativeBooleanTypeTest.php index d73d1ee8..e6a3f1ad 100644 --- a/tests/Unit/Type/Types/NativeBooleanTypeTest.php +++ b/tests/Unit/Type/Types/NativeBooleanTypeTest.php @@ -11,6 +11,7 @@ use CuyZ\Valinor\Type\Types\NativeBooleanType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -27,20 +28,22 @@ protected function setUp(): void $this->booleanType = new NativeBooleanType(); } - public function test_accepts_correct_values(): void + #[TestWith([true])] + #[TestWith([false])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->booleanType->accepts(true)); - self::assertTrue($this->booleanType->accepts(false)); + self::assertTrue($this->booleanType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->booleanType->accepts(null)); - self::assertFalse($this->booleanType->accepts('Schwifty!')); - self::assertFalse($this->booleanType->accepts(42.1337)); - self::assertFalse($this->booleanType->accepts(404)); - self::assertFalse($this->booleanType->accepts(['foo' => 'bar'])); - self::assertFalse($this->booleanType->accepts(new stdClass())); + self::assertFalse($this->booleanType->accepts($value)); } public function test_can_cast_boolean_value(): void diff --git a/tests/Unit/Type/Types/NativeClassTypeTest.php b/tests/Unit/Type/Types/NativeClassTypeTest.php index 53245af0..7cd151e8 100644 --- a/tests/Unit/Type/Types/NativeClassTypeTest.php +++ b/tests/Unit/Type/Types/NativeClassTypeTest.php @@ -12,6 +12,7 @@ use CuyZ\Valinor\Type\Types\UnionType; use DateTime; use DateTimeInterface; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -32,22 +33,26 @@ public function test_string_value_is_signature(): void self::assertSame(stdClass::class . "<{$generic->toString()}>", $type->toString()); } - public function test_accepts_correct_values(): void + #[TestWith([new stdClass()])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue((new NativeClassType(stdClass::class))->accepts(new stdClass())); + $type = new NativeClassType(stdClass::class); + + self::assertTrue($type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new DateTime()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { $type = new NativeClassType(stdClass::class); - self::assertFalse($type->accepts(null)); - self::assertFalse($type->accepts('Schwifty!')); - self::assertFalse($type->accepts(42.1337)); - self::assertFalse($type->accepts(404)); - self::assertFalse($type->accepts(['foo' => 'bar'])); - self::assertFalse($type->accepts(false)); - self::assertFalse($type->accepts(new DateTime())); + self::assertFalse($type->accepts($value)); } public function test_matches_other_identical_class(): void diff --git a/tests/Unit/Type/Types/NativeFloatTypeTest.php b/tests/Unit/Type/Types/NativeFloatTypeTest.php index fe1623cd..1a01a8ff 100644 --- a/tests/Unit/Type/Types/NativeFloatTypeTest.php +++ b/tests/Unit/Type/Types/NativeFloatTypeTest.php @@ -11,6 +11,7 @@ use CuyZ\Valinor\Type\Types\NativeFloatType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -27,19 +28,21 @@ protected function setUp(): void $this->floatType = new NativeFloatType(); } - public function test_accepts_correct_values(): void + #[TestWith([42.1337])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->floatType->accepts(42.1337)); + self::assertTrue($this->floatType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->floatType->accepts(null)); - self::assertFalse($this->floatType->accepts('Schwifty!')); - self::assertFalse($this->floatType->accepts(404)); - self::assertFalse($this->floatType->accepts(['foo' => 'bar'])); - self::assertFalse($this->floatType->accepts(false)); - self::assertFalse($this->floatType->accepts(new stdClass())); + self::assertFalse($this->floatType->accepts($value)); } public function test_can_cast_float_value(): void diff --git a/tests/Unit/Type/Types/NativeIntegerTypeTest.php b/tests/Unit/Type/Types/NativeIntegerTypeTest.php index f3d12759..0857e246 100644 --- a/tests/Unit/Type/Types/NativeIntegerTypeTest.php +++ b/tests/Unit/Type/Types/NativeIntegerTypeTest.php @@ -11,6 +11,7 @@ use CuyZ\Valinor\Type\Types\NativeIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -27,21 +28,23 @@ protected function setUp(): void $this->integerType = new NativeIntegerType(); } - public function test_accepts_correct_values(): void + #[TestWith([404])] + #[TestWith([-404])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->integerType->accepts(404)); - self::assertTrue($this->integerType->accepts(-404)); + self::assertTrue($this->integerType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([-42.1337])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->integerType->accepts(null)); - self::assertFalse($this->integerType->accepts('Schwifty!')); - self::assertFalse($this->integerType->accepts(-42.1337)); - self::assertFalse($this->integerType->accepts(42.1337)); - self::assertFalse($this->integerType->accepts(['foo' => 'bar'])); - self::assertFalse($this->integerType->accepts(false)); - self::assertFalse($this->integerType->accepts(new stdClass())); + self::assertFalse($this->integerType->accepts($value)); } public function test_can_cast_integer_value(): void diff --git a/tests/Unit/Type/Types/NativeStringTypeTest.php b/tests/Unit/Type/Types/NativeStringTypeTest.php index bb876904..73fec19d 100644 --- a/tests/Unit/Type/Types/NativeStringTypeTest.php +++ b/tests/Unit/Type/Types/NativeStringTypeTest.php @@ -12,6 +12,7 @@ use CuyZ\Valinor\Type\Types\NativeStringType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -28,19 +29,21 @@ protected function setUp(): void $this->stringType = new NativeStringType(); } - public function test_accepts_correct_values(): void + #[TestWith(['Schwifty!'])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->stringType->accepts('Schwifty!')); + self::assertTrue($this->stringType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->stringType->accepts(null)); - self::assertFalse($this->stringType->accepts(42.1337)); - self::assertFalse($this->stringType->accepts(404)); - self::assertFalse($this->stringType->accepts(['foo' => 'bar'])); - self::assertFalse($this->stringType->accepts(false)); - self::assertFalse($this->stringType->accepts(new stdClass())); + self::assertFalse($this->stringType->accepts($value)); } public function test_can_cast_stringable_value(): void diff --git a/tests/Unit/Type/Types/NegativeIntegerTypeTest.php b/tests/Unit/Type/Types/NegativeIntegerTypeTest.php index ef42727e..b4a9c91e 100644 --- a/tests/Unit/Type/Types/NegativeIntegerTypeTest.php +++ b/tests/Unit/Type/Types/NegativeIntegerTypeTest.php @@ -13,6 +13,7 @@ use CuyZ\Valinor\Type\Types\PositiveIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -29,21 +30,23 @@ protected function setUp(): void $this->negativeIntegerType = new NegativeIntegerType(); } - public function test_accepts_correct_values(): void + #[TestWith([-404])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->negativeIntegerType->accepts(-404)); + self::assertTrue($this->negativeIntegerType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([0])] + #[TestWith([404])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->negativeIntegerType->accepts(null)); - self::assertFalse($this->negativeIntegerType->accepts('Schwifty!')); - self::assertFalse($this->negativeIntegerType->accepts(0)); - self::assertFalse($this->negativeIntegerType->accepts(404)); - self::assertFalse($this->negativeIntegerType->accepts(42.1337)); - self::assertFalse($this->negativeIntegerType->accepts(['foo' => 'bar'])); - self::assertFalse($this->negativeIntegerType->accepts(false)); - self::assertFalse($this->negativeIntegerType->accepts(new stdClass())); + self::assertFalse($this->negativeIntegerType->accepts($value)); } public function test_can_cast_integer_value(): void diff --git a/tests/Unit/Type/Types/NonEmptyArrayTypeTest.php b/tests/Unit/Type/Types/NonEmptyArrayTypeTest.php index 0be46e4d..3dfddc3e 100644 --- a/tests/Unit/Type/Types/NonEmptyArrayTypeTest.php +++ b/tests/Unit/Type/Types/NonEmptyArrayTypeTest.php @@ -11,6 +11,7 @@ use CuyZ\Valinor\Type\Types\NativeStringType; use CuyZ\Valinor\Type\Types\NonEmptyArrayType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -56,39 +57,63 @@ public function test_subtype_is_correct(): void self::assertSame($subType, (new NonEmptyArrayType(ArrayKeyType::default(), $subType))->subType()); } - public function test_accepts_correct_values(): void + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 1337.404]])] + public function test_native_array_accepts_correct_values(bool $accepts, mixed $value): void { - $type = FakeType::accepting('Some value'); + $type = NonEmptyArrayType::native(); - $arrayWithDefaultKey = new NonEmptyArrayType(ArrayKeyType::default(), $type); - $arrayWithIntegerKey = new NonEmptyArrayType(ArrayKeyType::integer(), $type); - $arrayWithStringKey = new NonEmptyArrayType(ArrayKeyType::string(), $type); + self::assertSame($accepts, $type->accepts($value)); + } + + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_default_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new NonEmptyArrayType(ArrayKeyType::default(), FakeType::accepting('Some value')); + + self::assertSame($accepts, $type->accepts($value)); + } - self::assertTrue($arrayWithDefaultKey->accepts([42 => 'Some value'])); - self::assertTrue($arrayWithDefaultKey->accepts(['foo' => 'Some value'])); - self::assertFalse($arrayWithDefaultKey->accepts([42 => 1337.404])); - self::assertFalse($arrayWithDefaultKey->accepts(['foo' => 1337.404])); + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_integer_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new NonEmptyArrayType(ArrayKeyType::integer(), FakeType::accepting('Some value')); - self::assertTrue($arrayWithIntegerKey->accepts([42 => 'Some value'])); - self::assertFalse($arrayWithIntegerKey->accepts(['foo' => 'Some value'])); - self::assertFalse($arrayWithIntegerKey->accepts([42 => 1337.404])); + self::assertSame($accepts, $type->accepts($value)); + } - self::assertTrue($arrayWithStringKey->accepts([42 => 'Some value'])); - self::assertTrue($arrayWithStringKey->accepts(['foo' => 'Some value'])); - self::assertFalse($arrayWithIntegerKey->accepts([42 => 1337.404])); + #[TestWith(['accepts' => true, 'value' => [42 => 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['foo' => 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [42 => 1337.404]])] + #[TestWith(['accepts' => false, 'value' => ['foo' => 1337.404]])] + public function test_string_array_key_accepts_correct_values(bool $accepts, mixed $value): void + { + $type = new NonEmptyArrayType(ArrayKeyType::string(), FakeType::accepting('Some value')); - self::assertFalse(NonEmptyArrayType::native()->accepts('foo')); + self::assertSame($accepts, $type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([[]])] + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse(NonEmptyArrayType::native()->accepts([])); - self::assertFalse(NonEmptyArrayType::native()->accepts(null)); - self::assertFalse(NonEmptyArrayType::native()->accepts('Schwifty!')); - self::assertFalse(NonEmptyArrayType::native()->accepts(42.1337)); - self::assertFalse(NonEmptyArrayType::native()->accepts(404)); - self::assertFalse(NonEmptyArrayType::native()->accepts(false)); - self::assertFalse(NonEmptyArrayType::native()->accepts(new stdClass())); + self::assertFalse(NonEmptyArrayType::native()->accepts($value)); + self::assertFalse((new NonEmptyArrayType(ArrayKeyType::default(), FakeType::accepting('Some value')))->accepts($value)); + self::assertFalse((new NonEmptyArrayType(ArrayKeyType::integer(), FakeType::accepting('Some value')))->accepts($value)); + self::assertFalse((new NonEmptyArrayType(ArrayKeyType::string(), FakeType::accepting('Some value')))->accepts($value)); } public function test_matches_valid_array_type(): void diff --git a/tests/Unit/Type/Types/NonEmptyListTypeTest.php b/tests/Unit/Type/Types/NonEmptyListTypeTest.php index 46fb6edb..0e82218c 100644 --- a/tests/Unit/Type/Types/NonEmptyListTypeTest.php +++ b/tests/Unit/Type/Types/NonEmptyListTypeTest.php @@ -15,6 +15,7 @@ use CuyZ\Valinor\Type\Types\NonEmptyArrayType; use CuyZ\Valinor\Type\Types\NonEmptyListType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -59,35 +60,37 @@ public function test_subtype_is_correct(): void self::assertSame($subType, (new NonEmptyListType($subType))->subType()); } - public function test_accepts_correct_values(): void + #[TestWith(['accepts' => true, 'value' => ['Some value', 'Some value', 'Some value']])] + #[TestWith(['accepts' => true, 'value' => ['Some value', 'Schwifty!', 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [1 => 'Some value', 2 => 'Some value']])] + public function test_native_list_type_accepts_correct_values(bool $accepts, mixed $value): void { - $type = FakeType::accepting('Some value'); - - self::assertTrue((new NonEmptyListType($type))->accepts([ - 'Some value', - 'Some value', - 'Some value', - ])); - - self::assertFalse((new NonEmptyListType($type))->accepts([ - 'Some value', - 'Schwifty!', - 'Some value', - ])); + self::assertSame($accepts, NonEmptyListType::native()->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith(['accepts' => true, 'value' => ['Some value', 'Some value', 'Some value']])] + #[TestWith(['accepts' => false, 'value' => ['Some value', 'Schwifty!', 'Some value']])] + #[TestWith(['accepts' => false, 'value' => [1 => 'Some value', 2 => 'Some value']])] + public function test_list_of_type_accepts_correct_values(bool $accepts, mixed $value): void { - self::assertFalse(NonEmptyListType::native()->accepts([])); - self::assertFalse(NonEmptyListType::native()->accepts([1 => 'foo'])); - self::assertFalse(NonEmptyListType::native()->accepts(['foo' => 'foo'])); - - self::assertFalse(NonEmptyListType::native()->accepts(null)); - self::assertFalse(NonEmptyListType::native()->accepts('Schwifty!')); - self::assertFalse(NonEmptyListType::native()->accepts(42.1337)); - self::assertFalse(NonEmptyListType::native()->accepts(404)); - self::assertFalse(NonEmptyListType::native()->accepts(false)); - self::assertFalse(NonEmptyListType::native()->accepts(new stdClass())); + $type = new NonEmptyListType(FakeType::accepting('Some value')); + + self::assertSame($accepts, $type->accepts($value)); + } + + #[TestWith([[]])] + #[TestWith([[1 => 'foo']])] + #[TestWith([['foo' => 'foo']])] + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void + { + self::assertFalse(NonEmptyListType::native()->accepts($value)); + self::assertFalse((new NonEmptyListType(FakeType::accepting('Some value')))->accepts($value)); } public function test_matches_valid_list_type(): void diff --git a/tests/Unit/Type/Types/NonEmptyStringTypeTest.php b/tests/Unit/Type/Types/NonEmptyStringTypeTest.php index 56962983..bfa868b3 100644 --- a/tests/Unit/Type/Types/NonEmptyStringTypeTest.php +++ b/tests/Unit/Type/Types/NonEmptyStringTypeTest.php @@ -13,6 +13,7 @@ use CuyZ\Valinor\Type\Types\NonEmptyStringType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -29,20 +30,22 @@ protected function setUp(): void $this->nonEmptyStringType = new NonEmptyStringType(); } - public function test_accepts_correct_values(): void + #[TestWith(['Schwifty!'])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->nonEmptyStringType->accepts('Schwifty!')); + self::assertTrue($this->nonEmptyStringType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith([''])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->nonEmptyStringType->accepts(null)); - self::assertFalse($this->nonEmptyStringType->accepts('')); - self::assertFalse($this->nonEmptyStringType->accepts(42.1337)); - self::assertFalse($this->nonEmptyStringType->accepts(404)); - self::assertFalse($this->nonEmptyStringType->accepts(['foo' => 'bar'])); - self::assertFalse($this->nonEmptyStringType->accepts(false)); - self::assertFalse($this->nonEmptyStringType->accepts(new stdClass())); + self::assertFalse($this->nonEmptyStringType->accepts($value)); } public function test_can_cast_stringable_value(): void diff --git a/tests/Unit/Type/Types/NonNegativeIntegerTypeTest.php b/tests/Unit/Type/Types/NonNegativeIntegerTypeTest.php index eca0edb9..a99bfad2 100644 --- a/tests/Unit/Type/Types/NonNegativeIntegerTypeTest.php +++ b/tests/Unit/Type/Types/NonNegativeIntegerTypeTest.php @@ -13,6 +13,7 @@ use CuyZ\Valinor\Type\Types\PositiveIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -29,21 +30,23 @@ protected function setUp(): void $this->nonNegativeIntegerType = new NonNegativeIntegerType(); } - public function test_accepts_correct_values(): void + #[TestWith([0])] + #[TestWith([404])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->nonNegativeIntegerType->accepts(0)); - self::assertTrue($this->nonNegativeIntegerType->accepts(404)); + self::assertTrue($this->nonNegativeIntegerType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([-404])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->nonNegativeIntegerType->accepts(null)); - self::assertFalse($this->nonNegativeIntegerType->accepts('Schwifty!')); - self::assertFalse($this->nonNegativeIntegerType->accepts(-404)); - self::assertFalse($this->nonNegativeIntegerType->accepts(42.1337)); - self::assertFalse($this->nonNegativeIntegerType->accepts(['foo' => 'bar'])); - self::assertFalse($this->nonNegativeIntegerType->accepts(false)); - self::assertFalse($this->nonNegativeIntegerType->accepts(new stdClass())); + self::assertFalse($this->nonNegativeIntegerType->accepts($value)); } public function test_can_cast_integer_value(): void diff --git a/tests/Unit/Type/Types/NonPositiveIntegerTypeTest.php b/tests/Unit/Type/Types/NonPositiveIntegerTypeTest.php index 7c0b499d..fa24a02b 100644 --- a/tests/Unit/Type/Types/NonPositiveIntegerTypeTest.php +++ b/tests/Unit/Type/Types/NonPositiveIntegerTypeTest.php @@ -13,6 +13,7 @@ use CuyZ\Valinor\Type\Types\PositiveIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -29,21 +30,23 @@ protected function setUp(): void $this->nonPositiveIntegerType = new NonPositiveIntegerType(); } - public function test_accepts_correct_values(): void + #[TestWith([0])] + #[TestWith([-404])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->nonPositiveIntegerType->accepts(0)); - self::assertTrue($this->nonPositiveIntegerType->accepts(-404)); + self::assertTrue($this->nonPositiveIntegerType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([404])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->nonPositiveIntegerType->accepts(null)); - self::assertFalse($this->nonPositiveIntegerType->accepts('Schwifty!')); - self::assertFalse($this->nonPositiveIntegerType->accepts(404)); - self::assertFalse($this->nonPositiveIntegerType->accepts(42.1337)); - self::assertFalse($this->nonPositiveIntegerType->accepts(['foo' => 'bar'])); - self::assertFalse($this->nonPositiveIntegerType->accepts(false)); - self::assertFalse($this->nonPositiveIntegerType->accepts(new stdClass())); + self::assertFalse($this->nonPositiveIntegerType->accepts($value)); } public function test_can_cast_integer_value(): void diff --git a/tests/Unit/Type/Types/NullTypeTest.php b/tests/Unit/Type/Types/NullTypeTest.php index 549ba841..aa527754 100644 --- a/tests/Unit/Type/Types/NullTypeTest.php +++ b/tests/Unit/Type/Types/NullTypeTest.php @@ -9,6 +9,7 @@ use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\NullType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -30,19 +31,21 @@ public function test_string_value_is_correct(): void self::assertSame('null', $this->nullType->toString()); } - public function test_accepts_correct_values(): void + #[TestWith([null])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->nullType->accepts(null)); + self::assertTrue($this->nullType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->nullType->accepts('Schwifty!')); - self::assertFalse($this->nullType->accepts(42.1337)); - self::assertFalse($this->nullType->accepts(404)); - self::assertFalse($this->nullType->accepts(['foo' => 'bar'])); - self::assertFalse($this->nullType->accepts(false)); - self::assertFalse($this->nullType->accepts(new stdClass())); + self::assertFalse($this->nullType->accepts($value)); } public function test_matches_same_type(): void diff --git a/tests/Unit/Type/Types/NumericStringTypeTest.php b/tests/Unit/Type/Types/NumericStringTypeTest.php index 3d6fb273..72343e5e 100644 --- a/tests/Unit/Type/Types/NumericStringTypeTest.php +++ b/tests/Unit/Type/Types/NumericStringTypeTest.php @@ -14,6 +14,7 @@ use CuyZ\Valinor\Type\Types\NumericStringType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -30,21 +31,23 @@ protected function setUp(): void $this->numericStringType = new NumericStringType(); } - public function test_accepts_correct_values(): void + #[TestWith(['777.7'])] + #[TestWith(['0'])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->numericStringType->accepts('777.7')); - self::assertTrue($this->numericStringType->accepts('0')); + self::assertTrue($this->numericStringType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith([''])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->numericStringType->accepts(null)); - self::assertFalse($this->numericStringType->accepts('')); - self::assertFalse($this->numericStringType->accepts(42.1337)); - self::assertFalse($this->numericStringType->accepts(404)); - self::assertFalse($this->numericStringType->accepts(['foo' => 'bar'])); - self::assertFalse($this->numericStringType->accepts(false)); - self::assertFalse($this->numericStringType->accepts(new stdClass())); + self::assertFalse($this->numericStringType->accepts($value)); } public function test_can_cast_stringable_value(): void diff --git a/tests/Unit/Type/Types/PositiveIntegerTypeTest.php b/tests/Unit/Type/Types/PositiveIntegerTypeTest.php index 72c5368c..6d30e8bc 100644 --- a/tests/Unit/Type/Types/PositiveIntegerTypeTest.php +++ b/tests/Unit/Type/Types/PositiveIntegerTypeTest.php @@ -13,6 +13,7 @@ use CuyZ\Valinor\Type\Types\PositiveIntegerType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -29,21 +30,23 @@ protected function setUp(): void $this->positiveIntegerType = new PositiveIntegerType(); } - public function test_accepts_correct_values(): void + #[TestWith([404])] + public function test_accepts_correct_values(mixed $value): void { - self::assertTrue($this->positiveIntegerType->accepts(404)); + self::assertTrue($this->positiveIntegerType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([0])] + #[TestWith([-404])] + #[TestWith([42.1337])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->positiveIntegerType->accepts(null)); - self::assertFalse($this->positiveIntegerType->accepts('Schwifty!')); - self::assertFalse($this->positiveIntegerType->accepts(0)); - self::assertFalse($this->positiveIntegerType->accepts(-404)); - self::assertFalse($this->positiveIntegerType->accepts(42.1337)); - self::assertFalse($this->positiveIntegerType->accepts(['foo' => 'bar'])); - self::assertFalse($this->positiveIntegerType->accepts(false)); - self::assertFalse($this->positiveIntegerType->accepts(new stdClass())); + self::assertFalse($this->positiveIntegerType->accepts($value)); } public function test_can_cast_integer_value(): void diff --git a/tests/Unit/Type/Types/ShapedArrayTypeTest.php b/tests/Unit/Type/Types/ShapedArrayTypeTest.php index f0ea1293..0a3783aa 100644 --- a/tests/Unit/Type/Types/ShapedArrayTypeTest.php +++ b/tests/Unit/Type/Types/ShapedArrayTypeTest.php @@ -19,6 +19,7 @@ use CuyZ\Valinor\Type\Types\ShapedArrayType; use CuyZ\Valinor\Type\Types\StringValueType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -70,33 +71,33 @@ public function test_string_value_is_correct(): void self::assertSame("array{foo: string, 1337?: int, ...array<'unsealed-key', float>}", $this->type->toString()); } - public function test_accepts_correct_values(): void + // Without additional values + #[TestWith([['foo' => 'foo', 1337 => 42]])] + #[TestWith([['foo' => 'foo']])] + // With additional values + #[TestWith([['foo' => 'foo', 1337 => 42, 'unsealed-key' => 42.1337]])] + #[TestWith([['foo' => 'foo', 'unsealed-key' => 42.1337]])] + public function test_accepts_correct_values(mixed $value): void { - // Without additional values - self::assertTrue($this->type->accepts(['foo' => 'foo', 1337 => 42])); - self::assertTrue($this->type->accepts(['foo' => 'foo'])); - - // With additional values - self::assertTrue($this->type->accepts(['foo' => 'foo', 1337 => 42, 'unsealed-key' => 42.1337])); - self::assertTrue($this->type->accepts(['foo' => 'foo', 'unsealed-key' => 42.1337])); + self::assertTrue($this->type->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + // Without additional values + #[TestWith([['foo' => 42]])] + #[TestWith([['foo' => new stdClass()]])] + #[TestWith([['bar' => 'foo']])] + // With invalid additional values + #[TestWith([['foo' => 'foo', 42 => '']])] + // Others + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - // Without additional values - self::assertFalse($this->type->accepts(['foo' => 42])); - self::assertFalse($this->type->accepts(['foo' => new stdClass()])); - self::assertFalse($this->type->accepts(['bar' => 'foo'])); - - // With invalid additional values - self::assertFalse($this->type->accepts(['foo' => 'foo', 42 => ''])); - - self::assertFalse($this->type->accepts(null)); - self::assertFalse($this->type->accepts('Schwifty!')); - self::assertFalse($this->type->accepts(42.1337)); - self::assertFalse($this->type->accepts(404)); - self::assertFalse($this->type->accepts(false)); - self::assertFalse($this->type->accepts(new stdClass())); + self::assertFalse($this->type->accepts($value)); } public function test_matches_valid_array_shaped_type(): void diff --git a/tests/Unit/Type/Types/StringValueTypeTest.php b/tests/Unit/Type/Types/StringValueTypeTest.php index 019a884a..cf4e1de7 100644 --- a/tests/Unit/Type/Types/StringValueTypeTest.php +++ b/tests/Unit/Type/Types/StringValueTypeTest.php @@ -11,6 +11,7 @@ use CuyZ\Valinor\Type\Types\StringValueType; use CuyZ\Valinor\Type\Types\UnionType; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -30,26 +31,28 @@ public function test_value_can_be_retrieved(): void self::assertSame('Schwifty!', $this->type->value()); } - public function test_accepts_correct_values(): void + #[TestWith(['Schwifty!'])] + public function test_accepts_correct_values(mixed $value): void { $type = new StringValueType('Schwifty!'); $typeSingleQuote = StringValueType::from("'Schwifty!'"); $typeDoubleQuote = StringValueType::from('"Schwifty!"'); - self::assertTrue($type->accepts('Schwifty!')); - self::assertTrue($typeSingleQuote->accepts('Schwifty!')); - self::assertTrue($typeDoubleQuote->accepts('Schwifty!')); + self::assertTrue($type->accepts($value)); + self::assertTrue($typeSingleQuote->accepts($value)); + self::assertTrue($typeDoubleQuote->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith(['other string'])] + #[TestWith([null])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->type->accepts('other string')); - self::assertFalse($this->type->accepts(null)); - self::assertFalse($this->type->accepts(42.1337)); - self::assertFalse($this->type->accepts(404)); - self::assertFalse($this->type->accepts(['foo' => 'bar'])); - self::assertFalse($this->type->accepts(false)); - self::assertFalse($this->type->accepts(new stdClass())); + self::assertFalse($this->type->accepts($value)); } public function test_can_cast_stringable_value(): void diff --git a/tests/Unit/Type/Types/UndefinedObjectTypeTest.php b/tests/Unit/Type/Types/UndefinedObjectTypeTest.php index d908fc89..3b52fa42 100644 --- a/tests/Unit/Type/Types/UndefinedObjectTypeTest.php +++ b/tests/Unit/Type/Types/UndefinedObjectTypeTest.php @@ -11,6 +11,7 @@ use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\UndefinedObjectType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -38,14 +39,15 @@ public function test_accepts_correct_values(): void self::assertTrue($this->undefinedObjectType->accepts(new class () {})); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - self::assertFalse($this->undefinedObjectType->accepts(null)); - self::assertFalse($this->undefinedObjectType->accepts('Schwifty!')); - self::assertFalse($this->undefinedObjectType->accepts(42.1337)); - self::assertFalse($this->undefinedObjectType->accepts(404)); - self::assertFalse($this->undefinedObjectType->accepts(['foo' => 'bar'])); - self::assertFalse($this->undefinedObjectType->accepts(false)); + self::assertFalse($this->undefinedObjectType->accepts($value)); } public function test_matches_valid_types(): void diff --git a/tests/Unit/Type/Types/UnionTypeTest.php b/tests/Unit/Type/Types/UnionTypeTest.php index 2face8aa..4c015d91 100644 --- a/tests/Unit/Type/Types/UnionTypeTest.php +++ b/tests/Unit/Type/Types/UnionTypeTest.php @@ -9,6 +9,7 @@ use CuyZ\Valinor\Type\Types\Exception\ForbiddenMixedType; use CuyZ\Valinor\Type\Types\MixedType; use CuyZ\Valinor\Type\Types\UnionType; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use stdClass; @@ -62,34 +63,32 @@ public function test_to_string_returns_correct_value(): void self::assertSame("{$typeA->toString()}|{$typeB->toString()}|{$typeC->toString()}", $unionType->toString()); } - public function test_accepts_correct_values(): void + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith(['foo'])] + public function test_accepts_correct_values(mixed $value): void { $typeA = FakeType::accepting(42.1337); - $typeB = FakeType::accepting('foo'); - $typeC = FakeType::accepting($object = new stdClass()); + $typeB = FakeType::accepting(404); + $typeC = FakeType::accepting('foo'); $unionType = new UnionType($typeA, $typeB, $typeC); - self::assertTrue($unionType->accepts(42.1337)); - self::assertTrue($unionType->accepts('foo')); - self::assertTrue($unionType->accepts($object)); + self::assertTrue($unionType->accepts($value)); } - public function test_does_not_accept_incorrect_values(): void + #[TestWith([null])] + #[TestWith(['Schwifty!'])] + #[TestWith([42.1337])] + #[TestWith([404])] + #[TestWith([['foo' => 'bar']])] + #[TestWith([false])] + #[TestWith([new stdClass()])] + public function test_does_not_accept_incorrect_values(mixed $value): void { - $typeA = new FakeType(); - $typeB = new FakeType(); - $typeC = new FakeType(); - - $unionType = new UnionType($typeA, $typeB, $typeC); + $unionType = new UnionType(new FakeType(), new FakeType(), new FakeType()); - self::assertFalse($unionType->accepts(null)); - self::assertFalse($unionType->accepts('Schwifty!')); - self::assertFalse($unionType->accepts(42.1337)); - self::assertFalse($unionType->accepts(404)); - self::assertFalse($unionType->accepts(['foo' => 'bar'])); - self::assertFalse($unionType->accepts(false)); - self::assertFalse($unionType->accepts(new stdClass())); + self::assertFalse($unionType->accepts($value)); } public function test_matches_valid_type(): void