Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172<?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\BlobSchema;use Wotnak\Atproto\Lexicon\ValidationOptions;
/** * CID values use their Lex JSON representation, {"$link": string}. BlobSchema * has no blob traversal API and admits the complete legacy shape only in * non-strict parsing mode. */final class BlobSchemaTest extends TestCase {
private const RAW_CID = 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4'; private const DAG_CBOR_CID = 'bafyreic52vzks7wdklat4evp3vimohl55i2unzqpshz2ytka5omzr7exdy';
public function testAcceptsCanonicalBlobs(): void { $schema = new BlobSchema(accept: ['*/*']);
static::assertTrue($schema->safeValidate(self::validBlob())->isValid()); static::assertTrue($schema->safeValidate(self::validBlob('image/png', 5_000))->isValid()); static::assertTrue($schema->safeValidate(self::validBlob('text/plain', 0))->isValid()); }
#[DataProvider('nonObjectProvider')] public function testRejectsNonObjects(mixed $value): void { static::assertFalse( new BlobSchema() ->safeValidate($value) ->isValid(), ); }
/** @return array<string, array{mixed}> */ public static function nonObjectProvider(): array { return [ 'null' => [NULL], 'string' => ['blob'], 'number' => [42], 'boolean' => [TRUE], 'list' => [[self::RAW_CID]], 'date object' => [new \DateTimeImmutable()], 'map object' => [new \ArrayObject()], ]; }
/** @param array<string, mixed> $value */ #[DataProvider('invalidBlobProvider')] public function testRejectsMalformedCanonicalBlobs(array $value): void { $result = new BlobSchema()->safeValidate($value);
static::assertFalse($result->isValid()); static::assertSame(['invalid_blob'], \array_column($result->violations, 'code')); }
/** @return array<string, array{array<string, mixed>}> */ public static function invalidBlobProvider(): array { $valid = self::validBlob(); return [ 'empty object' => [[]], 'only type' => [['$type' => 'blob']], 'missing type' => [[ 'ref' => $valid['ref'], 'mimeType' => 'image/jpeg', 'size' => 10_000, ]], 'wrong type' => [\array_replace($valid, ['$type' => 'not-blob'])], 'missing ref' => [\array_diff_key($valid, ['ref' => TRUE])], 'null ref' => [\array_replace($valid, ['ref' => NULL])], 'DAG-CBOR ref' => [\array_replace($valid, [ 'ref' => ['$link' => self::DAG_CBOR_CID], ])], 'invalid ref type' => [\array_replace($valid, ['ref' => self::RAW_CID])], 'missing MIME type' => [\array_diff_key($valid, ['mimeType' => TRUE])], 'null MIME type' => [\array_replace($valid, ['mimeType' => NULL])], 'array MIME type' => [\array_replace($valid, ['mimeType' => ['image/jpeg']])], 'empty MIME type' => [\array_replace($valid, ['mimeType' => ''])], 'missing size' => [\array_diff_key($valid, ['size' => TRUE])], 'null size' => [\array_replace($valid, ['size' => NULL])], 'string size' => [\array_replace($valid, ['size' => '10000'])], 'fractional size' => [\array_replace($valid, ['size' => 10_000.5])], 'negative size' => [\array_replace($valid, ['size' => -1])], 'CID v0' => [\array_replace($valid, [ 'ref' => ['$link' => 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'], ])], 'extra field' => [\array_replace($valid, ['extra' => TRUE])], ]; }
public function testStrictModeControlsLegacyAndNonRawReferences(): void { $schema = new BlobSchema(); $legacy = ['cid' => self::RAW_CID, 'mimeType' => 'image/jpeg']; $nonRaw = self::validBlob(); $nonRaw['ref'] = ['$link' => self::DAG_CBOR_CID];
static::assertFalse($schema->safeValidate($legacy)->isValid()); static::assertTrue($schema->safeValidate($legacy, ValidationOptions::validate(FALSE))->isValid()); static::assertTrue($schema->safeParse($legacy, ValidationOptions::parse(FALSE))->isValid()); static::assertFalse($schema->safeValidate($nonRaw)->isValid()); static::assertTrue($schema->safeValidate($nonRaw, ValidationOptions::validate(FALSE))->isValid()); }
#[DataProvider('invalidLegacyBlobProvider')] public function testLooseModeRejectsMalformedLegacyBlobs(mixed $value): void { static::assertFalse( new BlobSchema() ->safeValidate($value, ValidationOptions::validate(FALSE)) ->isValid(), ); }
/** @return array<string, array{mixed}> */ public static function invalidLegacyBlobProvider(): array { return [ 'missing CID' => [['mimeType' => 'image/jpeg']], 'missing MIME type' => [['cid' => self::RAW_CID]], 'invalid CID' => [['cid' => 'not-a-cid', 'mimeType' => 'image/jpeg']], 'numeric CID' => [['cid' => 42, 'mimeType' => 'image/jpeg']], 'array MIME type' => [['cid' => self::RAW_CID, 'mimeType' => ['image/jpeg']]], 'numeric MIME type' => [['cid' => self::RAW_CID, 'mimeType' => 3]], 'empty MIME type' => [['cid' => self::RAW_CID, 'mimeType' => '']], 'list' => [[]], 'string' => ['not an object'], 'date object' => [new \DateTimeImmutable()], 'map object' => [new \ArrayObject()], 'extra property' => [[ 'cid' => self::RAW_CID, 'mimeType' => 'image/jpeg', 'extra' => TRUE, ]], ]; }
public function testEnforcesAcceptAndMaximumSizeOnlyInStrictMode(): void { $schema = new BlobSchema(accept: ['image/*'], maxSize: 10_000); $wrongMime = self::validBlob('application/pdf'); $oversized = self::validBlob(size: 10_001);
static::assertTrue($schema->safeValidate(self::validBlob())->isValid()); static::assertSame( ['invalid_blob_mime'], \array_column($schema->safeValidate($wrongMime)->violations, 'code'), ); static::assertSame( ['max_size'], \array_column($schema->safeValidate($oversized)->violations, 'code'), ); static::assertTrue( $schema->safeValidate($wrongMime, ValidationOptions::validate(FALSE))->isValid(), ); static::assertTrue( $schema->safeValidate($oversized, ValidationOptions::validate(FALSE))->isValid(), ); }
/** @return array<string, mixed> */ private static function validBlob(string $mimeType = 'image/jpeg', int $size = 10_000): array { return [ '$type' => 'blob', 'ref' => ['$link' => self::RAW_CID], 'mimeType' => $mimeType, 'size' => $size, ]; }
}