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 */ 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 $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}> */ 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 */ 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 */ 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, ]; } }