Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\InMemoryLexiconRepository;use Wotnak\Atproto\Lexicon\LexiconDocument;use Wotnak\Atproto\Lexicon\LexiconParser;use Wotnak\Atproto\Lexicon\Schema\RecordSchema;use Wotnak\Atproto\Lexicon\SchemaCompiler;
final class SchemaCompilerStringIntegrationTest extends TestCase {
#[DataProvider('validByteLengthProvider')] public function testAcceptsStringsWithinByteLengthConstraints(string $value): void { $result = self::stringRecord('com.example.stringLength', [ 'minLength' => 2, 'maxLength' => 4, ]) ->safeParse(self::value('com.example.stringLength', $value));
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
/** @return array<string, array{string}> */ public static function validByteLengthProvider(): array { return [ 'two ASCII bytes' => ['ab'], 'three ASCII bytes' => ['abc'], 'four ASCII bytes' => ['abcd'], 'combining acute accent' => ["\u{0301}"], 'ASCII plus combining accent' => ["a\u{0301}"], 'ASCII plus precomposed accent' => ['aé'], 'CJK character' => ['一'], 'two precomposed accents' => ['éé'], 'ASCII and precomposed accent' => ['aaé'], 'four-byte emoji' => ['👋'], ]; }
#[DataProvider('invalidByteLengthProvider')] public function testRejectsStringsOutsideByteLengthConstraints(string $value, string $code): void { self::assertViolation( self::stringRecord('com.example.stringLength', [ 'minLength' => 2, 'maxLength' => 4, ]), self::value('com.example.stringLength', $value), $code, ); }
/** @return array<string, array{string, string}> */ public static function invalidByteLengthProvider(): array { return [ 'empty string' => ['', 'min_length'], 'one ASCII byte' => ['a', 'min_length'], 'five ASCII bytes' => ['abcde', 'max_length'], 'ASCII plus two combining accents' => ["a\u{0301}\u{0301}", 'max_length'], 'three precomposed accents' => ['ééé', 'max_length'], 'emoji plus ASCII' => ['👋a', 'max_length'], 'two emoji' => ['👨👨', 'max_length'], 'family emoji' => ['👨👩👧👧', 'max_length'], ]; }
#[DataProvider('validMaximumByteLengthProvider')] public function testAcceptsStringsWithinMaximumByteLength(string $value): void { $result = self::stringRecord('com.example.stringLengthNoMinLength', [ 'maxLength' => 4, ]) ->safeParse(self::value('com.example.stringLengthNoMinLength', $value));
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
/** @return array<string, array{string}> */ public static function validMaximumByteLengthProvider(): array { return [ 'empty string' => [''], 'one ASCII byte' => ['a'], 'two ASCII bytes' => ['ab'], 'combining acute accent' => ["\u{0301}"], 'ASCII plus combining accent' => ["a\u{0301}"], 'ASCII plus precomposed accent' => ['aé'], 'three ASCII bytes' => ['abc'], 'CJK character' => ['一'], 'four ASCII bytes' => ['abcd'], 'two precomposed accents' => ['éé'], 'ASCII and precomposed accent' => ['aaé'], 'four-byte emoji' => ['👋'], ]; }
#[DataProvider('invalidMaximumByteLengthProvider')] public function testRejectsStringsAboveMaximumByteLength(string $value): void { self::assertViolation( self::stringRecord('com.example.stringLengthNoMinLength', ['maxLength' => 4]), self::value('com.example.stringLengthNoMinLength', $value), 'max_length', ); }
/** @return array<string, array{string}> */ public static function invalidMaximumByteLengthProvider(): array { return [ 'five ASCII bytes' => ['abcde'], 'ASCII plus two combining accents' => ["a\u{0301}\u{0301}"], 'three precomposed accents' => ['ééé'], 'emoji plus ASCII' => ['👋a'], 'two emoji' => ['👨👨'], 'family emoji' => ['👨👩👧👧'], ]; }
#[DataProvider('validGraphemeLengthProvider')] public function testAcceptsStringsWithinGraphemeConstraints(string $value): void { $result = self::stringRecord('com.example.stringLengthGrapheme', [ 'minGraphemes' => 2, 'maxGraphemes' => 4, ]) ->safeParse(self::value('com.example.stringLengthGrapheme', $value));
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
/** @return array<string, array{string}> */ public static function validGraphemeLengthProvider(): array { return [ 'two ASCII graphemes' => ['ab'], 'accented and ASCII graphemes' => ["a\u{0301}b"], 'two accented graphemes' => ["a\u{0301}b\u{0301}"], 'two emoji' => ['😀😀'], 'digits and family emoji' => ['12👨👩👧👧'], 'four ASCII graphemes' => ['abcd'], 'four accented graphemes' => ["a\u{0301}b\u{0301}c\u{0301}d\u{0301}"], ]; }
#[DataProvider('invalidGraphemeLengthProvider')] public function testRejectsStringsOutsideGraphemeConstraints(string $value, string $code): void { self::assertViolation( self::stringRecord('com.example.stringLengthGrapheme', [ 'minGraphemes' => 2, 'maxGraphemes' => 4, ]), self::value('com.example.stringLengthGrapheme', $value), $code, ); }
/** @return array<string, array{string, string}> */ public static function invalidGraphemeLengthProvider(): array { return [ 'empty string' => ['', 'min_graphemes'], 'three combining accents' => ["\u{0301}\u{0301}\u{0301}", 'min_graphemes'], 'one ASCII grapheme' => ['a', 'min_graphemes'], 'base with four combining accents' => ["a\u{0301}\u{0301}\u{0301}\u{0301}", 'min_graphemes'], 'emoji presentation sequence' => ["5\u{FE0F}", 'min_graphemes'], 'family emoji' => ['👨👩👧👧', 'min_graphemes'], 'five ASCII graphemes' => ['abcde', 'max_graphemes'], 'five accented graphemes' => ["a\u{0301}b\u{0301}c\u{0301}d\u{0301}e\u{0301}", 'max_graphemes'], 'five emoji' => ['😀😀😀😀😀', 'max_graphemes'], 'mixed five graphemes' => ['ab😀de', 'max_graphemes'], ]; }
public function testAppliesStringEnumConstraint(): void { $schema = self::stringRecord('com.example.stringEnum', ['enum' => ['a', 'b']]);
static::assertTrue($schema->safeParse(self::value('com.example.stringEnum', 'a'))->isValid()); self::assertViolation($schema, self::value('com.example.stringEnum', 'c'), 'invalid_enum'); }
public function testAppliesStringConstConstraint(): void { $schema = self::stringRecord('com.example.stringConst', ['const' => 'a']);
static::assertTrue($schema->safeParse(self::value('com.example.stringConst', 'a'))->isValid()); self::assertViolation($schema, self::value('com.example.stringConst', 'b'), 'invalid_const'); }
#[DataProvider('validDatetimeProvider')] public function testAcceptsValidDatetimes(string $value): void { self::assertValidFormat('com.example.datetime', 'datetime', 'datetime', $value); }
/** @return array<string, array{string}> */ public static function validDatetimeProvider(): array { return [ 'milliseconds UTC' => ['2022-12-12T00:50:36.809Z'], 'seconds UTC' => ['2022-12-12T00:50:36Z'], 'one fractional digit UTC' => ['2022-12-12T00:50:36.8Z'], 'two fractional digits UTC' => ['2022-12-12T00:50:36.80Z'], 'zero offset' => ['2022-12-12T00:50:36+00:00'], 'fractional zero offset' => ['2022-12-12T00:50:36.8+00:00'], 'negative offset' => ['2022-12-11T19:50:36-05:00'], 'fractional negative offset' => ['2022-12-11T19:50:36.8-05:00'], 'two fractional digits negative offset' => ['2022-12-11T19:50:36.80-05:00'], 'milliseconds negative offset' => ['2022-12-11T19:50:36.809-05:00'], ]; }
public function testRejectsInvalidDatetime(): void { self::assertInvalidFormat('com.example.datetime', 'datetime', 'datetime', 'bad date'); }
#[DataProvider('validUriProvider')] public function testAcceptsValidUris(string $value): void { self::assertValidFormat('com.example.uri', 'uri', 'uri', $value); }
/** @return array<string, array{string}> */ public static function validUriProvider(): array { return [ 'HTTPS origin' => ['https://example.com'], 'HTTPS path' => ['https://example.com/with/path'], 'HTTPS query' => ['https://example.com/with/path?and=query'], 'AT URI' => ['at://bsky.social'], 'DID URI' => ['did:example:test'], ]; }
public function testRejectsInvalidUri(): void { self::assertInvalidFormat('com.example.uri', 'uri', 'uri', 'not a uri'); }
public function testAppliesAtUriFormat(): void { self::assertValidFormat( 'com.example.atUri', 'atUri', 'at-uri', 'at://did:web:example.com/com.example.test/self', ); self::assertInvalidFormat('com.example.atUri', 'atUri', 'at-uri', 'http://not-atproto.com'); }
public function testAppliesDidFormat(): void { self::assertValidFormat('com.example.did', 'did', 'did', 'did:web:example.com'); self::assertValidFormat('com.example.did', 'did', 'did', 'did:plc:12345678abcdefghijklmnop'); self::assertInvalidFormat('com.example.did', 'did', 'did', 'bad did'); self::assertInvalidFormat('com.example.did', 'did', 'did', 'did:short'); }
public function testAppliesHandleFormat(): void { self::assertValidFormat('com.example.handle', 'handle', 'handle', 'test.bsky.social'); self::assertValidFormat('com.example.handle', 'handle', 'handle', 'bsky.test'); self::assertInvalidFormat('com.example.handle', 'handle', 'handle', 'bad handle'); self::assertInvalidFormat('com.example.handle', 'handle', 'handle', '-bad-.test'); }
public function testAppliesAtIdentifierFormat(): void { self::assertValidFormat('com.example.atIdentifier', 'atIdentifier', 'at-identifier', 'bsky.test'); self::assertValidFormat( 'com.example.atIdentifier', 'atIdentifier', 'at-identifier', 'did:plc:12345678abcdefghijklmnop', ); self::assertInvalidFormat('com.example.atIdentifier', 'atIdentifier', 'at-identifier', 'bad id'); self::assertInvalidFormat('com.example.atIdentifier', 'atIdentifier', 'at-identifier', '-bad-.test'); }
public function testAppliesNsidFormat(): void { self::assertValidFormat('com.example.nsid', 'nsid', 'nsid', 'com.atproto.test'); self::assertValidFormat('com.example.nsid', 'nsid', 'nsid', 'app.bsky.nested.test'); self::assertInvalidFormat('com.example.nsid', 'nsid', 'nsid', 'bad nsid'); self::assertInvalidFormat('com.example.nsid', 'nsid', 'nsid', 'com.bad-.foo'); }
public function testAppliesCidFormat(): void { self::assertValidFormat( 'com.example.cid', 'cid', 'cid', 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', ); self::assertInvalidFormat('com.example.cid', 'cid', 'cid', 'abapsdofiuwrpoiasdfuaspdfoiu'); }
public function testAppliesLanguageFormat(): void { self::assertValidFormat('com.example.language', 'language', 'language', 'en-US-boont'); self::assertInvalidFormat('com.example.language', 'language', 'language', 'not-a-language-'); }
/** @param array<string, mixed> $constraints */ private static function stringRecord(string $id, array $constraints): RecordSchema { $field = \substr($id, \strlen('com.example.')); $field = match ($field) { 'stringLength', 'stringLengthNoMinLength', 'stringLengthGrapheme', 'stringEnum', 'stringConst' => 'string', default => $field, }; return self::record($id, $field, ['type' => 'string'] + $constraints); }
/** @param array<string, mixed> $definition */ private static function record(string $id, string $field, array $definition): RecordSchema { $result = new LexiconParser()->parse([ 'lexicon' => 1, 'id' => $id, 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'any', 'record' => [ 'type' => 'object', 'properties' => [$field => $definition], ], ], ], ]); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository = new InMemoryLexiconRepository(); $repository->add($result->value); $schema = new SchemaCompiler($repository)->compileRecord($id); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; }
private static function assertValidFormat( string $id, string $field, string $format, string $value, ): void { $result = self::record($id, $field, ['type' => 'string', 'format' => $format]) ->safeParse(self::fieldValue($id, $field, $value)); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
private static function assertInvalidFormat( string $id, string $field, string $format, string $value, ): void { self::assertViolation( self::record($id, $field, ['type' => 'string', 'format' => $format]), self::fieldValue($id, $field, $value), 'invalid_format', '$.' . $field, ); }
/** @return array<string, mixed> */ private static function value(string $type, string $value): array { return ['$type' => $type, 'string' => $value]; }
/** @return array<string, mixed> */ private static function fieldValue(string $type, string $field, string $value): array { return ['$type' => $type, $field => $value]; }
/** @param array<string, mixed> $value */ private static function assertViolation( RecordSchema $schema, array $value, string $code, string $path = '$.string', ): void { $result = $schema->safeParse($value); static::assertFalse($result->isValid()); static::assertContains($path, \array_column($result->violations, 'path')); static::assertContains($code, \array_column($result->violations, 'code')); }
}