Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117<?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;use Wotnak\Atproto\Lexicon\Schema\ObjectSchema;use Wotnak\Atproto\Lexicon\Schema\StringSchema;use Wotnak\Atproto\Lexicon\Schema\TypedObjectSchema;
final class TypedObjectSchemaTest extends TestCase {
/** @param array<string, mixed> $value */ #[DataProvider('validTypeProvider')] public function testAcceptsMissingOrMatchingType(array $value): void { static::assertTrue(self::schema()->safeValidate($value)->isValid()); }
/** * @return array<string, array{array<string, mixed>}> */ public static function validTypeProvider(): array { return [ 'missing type' => [['text' => 'Hello world']], 'matching type' => [[ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', ]], 'optional property' => [['text' => 'Hello world', 'likes' => 5]], ]; }
public function testRejectsExplicitNullType(): void { static::assertFalse( self::schema() ->safeValidate([ '$type' => NULL, 'text' => 'Hello world', ]) ->isValid(), ); }
public function testRejectsMismatchedAndNonNormalizedTypes(): void { foreach ([ 'app.bsky.feed.like', 'app.bsky.feed.post#main', 'app.bsky.feed.post#main-extra', 'APP.BSKY.FEED.POST', '', 123, ['value' => 'app.bsky.feed.post'], ] as $type) { $result = self::schema()->safeValidate(['$type' => $type, 'text' => 'Hello world']); static::assertFalse($result->isValid()); static::assertSame(['$.$type'], \array_column($result->violations, 'path')); static::assertSame(['type_mismatch'], \array_column($result->violations, 'code')); } }
public function testDelegatesPropertyValidation(): void { $result = self::schema() ->safeValidate([ '$type' => 'app.bsky.feed.post', 'text' => 123, 'likes' => 'five', ]);
static::assertFalse($result->isValid()); static::assertSame(['$.text', '$.likes'], \array_column($result->violations, 'path')); }
#[DataProvider('nonObjectProvider')] public function testRejectsNonObjects(mixed $value): void { static::assertFalse(self::schema()->safeValidate($value)->isValid()); }
/** * @return array<string, array{mixed}> */ public static function nonObjectProvider(): array { return [ 'null' => [NULL], 'string' => ['value'], 'integer' => [1], 'boolean' => [TRUE], 'list' => [['text', 5]], ]; }
public function testSafeParseAppliesObjectDefaults(): void { $schema = new TypedObjectSchema( 'app.bsky.feed.post', new ObjectSchema(['text' => new StringSchema(default: 'Hello world')]), );
$result = $schema->safeParse(new \stdClass());
static::assertTrue($result->isValid()); static::assertEquals((object) ['text' => 'Hello world'], $result->value); }
private static function schema(): TypedObjectSchema { return new TypedObjectSchema( 'app.bsky.feed.post', new ObjectSchema([ 'text' => new StringSchema(), 'likes' => new IntegerSchema(), ], required: ['text']), ); }
}