Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
4.3 kB · 135 lines
PHP
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136<?php
namespace SocialDept\AtpSchema\Tests\Unit\Parser;
use Orchestra\Testbench\TestCase;use SocialDept\AtpSchema\Data\Types\BooleanType;use SocialDept\AtpSchema\Data\Types\BytesType;use SocialDept\AtpSchema\Data\Types\CidLinkType;use SocialDept\AtpSchema\Data\Types\IntegerType;use SocialDept\AtpSchema\Data\Types\NullType;use SocialDept\AtpSchema\Data\Types\StringType;use SocialDept\AtpSchema\Data\Types\UnknownType;use SocialDept\AtpSchema\Exceptions\TypeResolutionException;use SocialDept\AtpSchema\Parser\PrimitiveParser;
class PrimitiveParserTest extends TestCase{ protected PrimitiveParser $parser;
protected function setUp(): void { parent::setUp();
$this->parser = new PrimitiveParser(); }
public function test_it_parses_null_type(): void { $type = $this->parser->parse(['type' => 'null']);
$this->assertInstanceOf(NullType::class, $type); }
public function test_it_parses_boolean_type(): void { $type = $this->parser->parse(['type' => 'boolean']);
$this->assertInstanceOf(BooleanType::class, $type); }
public function test_it_parses_integer_type(): void { $type = $this->parser->parse(['type' => 'integer']);
$this->assertInstanceOf(IntegerType::class, $type); }
public function test_it_parses_string_type(): void { $type = $this->parser->parse(['type' => 'string']);
$this->assertInstanceOf(StringType::class, $type); }
public function test_it_parses_bytes_type(): void { $type = $this->parser->parse(['type' => 'bytes']);
$this->assertInstanceOf(BytesType::class, $type); }
public function test_it_parses_cid_link_type(): void { $type = $this->parser->parse(['type' => 'cid-link']);
$this->assertInstanceOf(CidLinkType::class, $type); }
public function test_it_parses_unknown_type(): void { $type = $this->parser->parse(['type' => 'unknown']);
$this->assertInstanceOf(UnknownType::class, $type); }
public function test_it_throws_on_missing_type(): void { $this->expectException(TypeResolutionException::class); $this->expectExceptionMessage('Unknown Lexicon type: (missing type field)');
$this->parser->parse([]); }
public function test_it_throws_on_unknown_type(): void { $this->expectException(TypeResolutionException::class); $this->expectExceptionMessage('Unknown Lexicon type: nonexistent');
$this->parser->parse(['type' => 'nonexistent']); }
public function test_it_checks_if_type_is_primitive(): void { $this->assertTrue($this->parser->isPrimitive('null')); $this->assertTrue($this->parser->isPrimitive('boolean')); $this->assertTrue($this->parser->isPrimitive('integer')); $this->assertTrue($this->parser->isPrimitive('string')); $this->assertTrue($this->parser->isPrimitive('bytes')); $this->assertTrue($this->parser->isPrimitive('cid-link')); $this->assertTrue($this->parser->isPrimitive('unknown'));
$this->assertFalse($this->parser->isPrimitive('object')); $this->assertFalse($this->parser->isPrimitive('array')); $this->assertFalse($this->parser->isPrimitive('ref')); }
public function test_it_returns_supported_types(): void { $types = $this->parser->getSupportedTypes();
$this->assertCount(7, $types); $this->assertContains('null', $types); $this->assertContains('boolean', $types); $this->assertContains('integer', $types); $this->assertContains('string', $types); $this->assertContains('bytes', $types); $this->assertContains('cid-link', $types); $this->assertContains('unknown', $types); }
public function test_it_parses_type_with_properties(): void { $type = $this->parser->parse([ 'type' => 'string', 'description' => 'A test string', 'minLength' => 1, 'maxLength' => 100, ]);
$this->assertInstanceOf(StringType::class, $type); $this->assertSame('A test string', $type->description); $this->assertSame(1, $type->minLength); $this->assertSame(100, $type->maxLength); }}