Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit\Schema;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\Schema\IntegerSchema;
final class IntegerSchemaTest extends TestCase {
#[DataProvider('integerProvider')] public function testAcceptsIntegers(int|float $value, int $expected): void { $result = new IntegerSchema()->safeValidate($value);
static::assertTrue($result->isValid()); static::assertSame($expected, $result->value); }
/** * @return array<string, array{int|float, int}> */ public static function integerProvider(): array { return [ 'positive' => [42, 42], 'zero' => [0, 0], 'negative' => [-42, -42], 'integer-like float' => [42.0, 42], ]; }
#[DataProvider('nonIntegerProvider')] public function testRejectsNonIntegers(mixed $value): void { $result = new IntegerSchema()->safeValidate($value);
static::assertFalse($result->isValid()); static::assertSame(['invalid_type'], \array_column($result->violations, 'code')); }
/** * @return array<string, array{mixed}> */ public static function nonIntegerProvider(): array { return [ 'null' => [NULL], 'fraction' => [1.5], 'infinity' => [INF], 'negative infinity' => [-INF], 'NaN' => [NAN], 'string' => ['1'], 'boolean' => [TRUE], 'array' => [[]], 'object-like array' => [['value' => 1]], ]; }
/** @param list<string> $codes */ #[DataProvider('rangeProvider')] public function testEnforcesInclusiveRange(int $value, bool $valid, array $codes): void { $result = new IntegerSchema(minimum: 10, maximum: 100)->safeValidate($value);
static::assertSame($valid, $result->isValid()); static::assertSame($codes, \array_column($result->violations, 'code')); }
/** * @return array<string, array{int, bool, list<string>}> */ public static function rangeProvider(): array { return [ 'below minimum' => [5, FALSE, ['minimum']], 'at minimum' => [10, TRUE, []], 'within range' => [50, TRUE, []], 'at maximum' => [100, TRUE, []], 'above maximum' => [150, FALSE, ['maximum']], ]; }
public function testEnforcesEnumAndConst(): void { $enum = new IntegerSchema(enum: [1, 2]); static::assertTrue($enum->safeValidate(1)->isValid()); static::assertSame(['invalid_enum'], \array_column($enum->safeValidate(3)->violations, 'code'));
$const = new IntegerSchema(const: 4); static::assertTrue($const->safeValidate(4)->isValid()); static::assertSame(['invalid_const'], \array_column($const->safeValidate(5)->violations, 'code')); }
public function testDefaultAppliesOnlyWhileParsingNull(): void { $schema = new IntegerSchema(default: 10);
$parsed = $schema->safeParse(NULL); static::assertTrue($parsed->isValid()); static::assertSame(10, $parsed->value); static::assertFalse($schema->safeValidate(NULL)->isValid()); static::assertSame(20, $schema->safeParse(20)->value); static::assertSame(0, $schema->safeParse(0)->value); }
public function testDefaultMustSatisfyConstraints(): void { $result = new IntegerSchema(minimum: 10, default: 5)->safeParse(NULL);
static::assertFalse($result->isValid()); static::assertSame(5, $result->value); static::assertSame(['minimum'], \array_column($result->violations, 'code')); }
}