Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use data provider for types tests #581

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions tests/Unit/Type/Types/ArrayKeyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
71 changes: 48 additions & 23 deletions tests/Unit/Type/Types/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
38 changes: 20 additions & 18 deletions tests/Unit/Type/Types/BooleanValueTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
18 changes: 10 additions & 8 deletions tests/Unit/Type/Types/CallableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
68 changes: 29 additions & 39 deletions tests/Unit/Type/Types/ClassStringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use stdClass;

Expand Down Expand Up @@ -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
Expand Down
Loading
Loading