Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Interop;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Identifier\AtUriParser;use Wotnak\Atproto\Identifier\DidParser;use Wotnak\Atproto\Identifier\HandleParser;use Wotnak\Atproto\Identifier\NsidParser;use Wotnak\Atproto\Identifier\RecordKeyParser;use Wotnak\Atproto\Lexicon\DataModelValidator;use Wotnak\Atproto\Lexicon\LexiconParser;use Wotnak\Atproto\Lexicon\Schema\StringSchema;
/** * Runs a bundled snapshot of the official language-neutral ATProto fixtures. * * Snapshot provenance is recorded in Fixtures/manifest.json. Refresh it with * the fixtures:update:interop Composer script. */final class InteropFixtureTest extends TestCase {
public function testFixtureSnapshotRecordsOfficialSourceCommit(): void { $path = self::fixtureRoot() . '/manifest.json'; $contents = \file_get_contents($path); if ($contents === FALSE) { throw new \RuntimeException('Unable to read fixture manifest: ' . $path); } $manifest = \json_decode($contents, TRUE, flags: JSON_THROW_ON_ERROR);
static::assertIsArray($manifest); static::assertSame('https://github.com/bluesky-social/atproto', $manifest['repository'] ?? NULL); static::assertSame('MIT', $manifest['license'] ?? NULL); static::assertSame('LICENSE-MIT.txt', $manifest['licenseFile'] ?? NULL); static::assertSame( [ 'interop-test-files/syntax/*_syntax_*.txt', 'packages/lex/lex-json/tests/data-model-valid.json', 'packages/lex/lex-json/tests/data-model-invalid.json', 'packages/lex/lex-document/tests/lexicon-valid.json', 'packages/lex/lex-document/tests/lexicon-invalid.json', ], $manifest['sources'] ?? NULL, );
$commit = $manifest['commit'] ?? NULL; static::assertIsString($commit); static::assertMatchesRegularExpression('/^[0-9a-f]{40}$/D', $commit); }
#[DataProvider('syntaxVectorProvider')] public function testSyntaxVector(string $kind, bool $expected, string $value, string $source): void { $actual = match ($kind) { 'aturi' => new AtUriParser()->isValid($value), 'did' => DidParser::isValid($value), 'handle' => new HandleParser()->isValid($value), 'nsid' => new NsidParser()->isValid($value), 'recordkey' => new RecordKeyParser()->isValid($value), 'atidentifier' => DidParser::isValid($value) || new HandleParser()->isValid($value), 'datetime', 'language', 'tid' => new StringSchema(format: $kind) ->safeValidate($value) ->isValid(), default => throw new \LogicException('Unsupported upstream syntax vector kind.'), };
static::assertSame($expected, $actual, $source . ': ' . $value); }
/** * @return iterable<string, array{string, bool, string, string}> */ public static function syntaxVectorProvider(): iterable { $directory = self::fixtureRoot() . '/syntax'; $files = \glob($directory . '/*_syntax_*.txt'); if ($files === FALSE || $files === []) { throw new \RuntimeException('No bundled syntax fixtures found in: ' . $directory); } foreach ($files as $file) { $name = \basename($file, '.txt'); $matches = []; if (\preg_match('/^([a-z]+)_syntax_(valid|invalid)$/D', $name, $matches) !== 1) { continue; } foreach (self::vectorLines($file) as $index => $value) { yield $name . ':' . $index => [$matches[1], $matches[2] === 'valid', $value, $name]; } } }
#[DataProvider('dataModelVectorProvider')] public function testDataModelVector(bool $expected, mixed $value, string $note): void { $result = new DataModelValidator()->validateJsonDocument($value); static::assertSame($expected, $result->isValid(), $note . ': ' . \implode('; ', $result->messages())); }
/** * @return iterable<string, array{bool, mixed, string}> */ public static function dataModelVectorProvider(): iterable { foreach (['valid' => TRUE, 'invalid' => FALSE] as $set => $expected) { $path = self::fixtureRoot() . '/lex-json/data-model-' . $set . '.json'; foreach (self::jsonList($path) as $index => $fixture) { $note = \is_string($fixture['note'] ?? NULL) ? $fixture['note'] : $set . ':' . $index; yield $set . ':' . $note => [$expected, $fixture['json'] ?? NULL, $note]; } } }
/** * @param array<string, mixed> $schema */ #[DataProvider('lexiconVectorProvider')] public function testLexiconVector(bool $expected, array $schema, string $name): void { $result = new LexiconParser()->parse($schema); static::assertSame($expected, $result->isValid(), $name . ': ' . \implode('; ', $result->messages())); }
/** * @return iterable<string, array{bool, array<string, mixed>, string}> */ public static function lexiconVectorProvider(): iterable { foreach (['valid' => TRUE, 'invalid' => FALSE] as $set => $expected) { $path = self::fixtureRoot() . '/lex-document/lexicon-' . $set . '.json'; foreach (self::jsonList($path) as $index => $fixture) { $name = \is_string($fixture['name'] ?? NULL) ? $fixture['name'] : $set . ':' . $index; $schema = $fixture['lexicon'] ?? NULL; if (\is_array($schema)) { /** @var array<string, mixed> $schema */ yield $set . ':' . $name => [$expected, $schema, $name]; } } } }
private static function fixtureRoot(): string { return __DIR__ . '/Fixtures'; }
/** * @return list<string> */ private static function vectorLines(string $path): array { $lines = \file($path, FILE_IGNORE_NEW_LINES); if ($lines === FALSE) { throw new \RuntimeException('Unable to read upstream vector file: ' . $path); } return \array_values(\array_filter( $lines, static fn (string $line): bool => $line !== '' && !\str_starts_with($line, '#'), )); }
/** * @return list<array<string, mixed>> */ private static function jsonList(string $path): array { $contents = \file_get_contents($path); if ($contents === FALSE) { throw new \RuntimeException('Unable to read upstream fixture file: ' . $path); } $decoded = \json_decode($contents, TRUE, flags: JSON_THROW_ON_ERROR); if (!\is_array($decoded) || !\array_is_list($decoded)) { throw new \RuntimeException('Upstream fixture must contain a JSON list: ' . $path); } /** @var list<array<string, mixed>> $decoded */ return $decoded; }
}