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 */ 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 */ 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 $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}> */ 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 $schema */ yield $set . ':' . $name => [$expected, $schema, $name]; } } } } private static function fixtureRoot(): string { return __DIR__ . '/Fixtures'; } /** * @return list */ 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> */ 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> $decoded */ return $decoded; } }