Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1<?php
2
3namespace SocialDept\AtpSchema\Tests\Unit\Parser;
4
5use Orchestra\Testbench\TestCase;
6use SocialDept\AtpSchema\Data\Types\BooleanType;
7use SocialDept\AtpSchema\Data\Types\BytesType;
8use SocialDept\AtpSchema\Data\Types\CidLinkType;
9use SocialDept\AtpSchema\Data\Types\IntegerType;
10use SocialDept\AtpSchema\Data\Types\NullType;
11use SocialDept\AtpSchema\Data\Types\StringType;
12use SocialDept\AtpSchema\Data\Types\UnknownType;
13use SocialDept\AtpSchema\Exceptions\TypeResolutionException;
14use SocialDept\AtpSchema\Parser\PrimitiveParser;
15
16class PrimitiveParserTest extends TestCase
17{
18 protected PrimitiveParser $parser;
19
20 protected function setUp(): void
21 {
22 parent::setUp();
23
24 $this->parser = new PrimitiveParser();
25 }
26
27 public function test_it_parses_null_type(): void
28 {
29 $type = $this->parser->parse(['type' => 'null']);
30
31 $this->assertInstanceOf(NullType::class, $type);
32 }
33
34 public function test_it_parses_boolean_type(): void
35 {
36 $type = $this->parser->parse(['type' => 'boolean']);
37
38 $this->assertInstanceOf(BooleanType::class, $type);
39 }
40
41 public function test_it_parses_integer_type(): void
42 {
43 $type = $this->parser->parse(['type' => 'integer']);
44
45 $this->assertInstanceOf(IntegerType::class, $type);
46 }
47
48 public function test_it_parses_string_type(): void
49 {
50 $type = $this->parser->parse(['type' => 'string']);
51
52 $this->assertInstanceOf(StringType::class, $type);
53 }
54
55 public function test_it_parses_bytes_type(): void
56 {
57 $type = $this->parser->parse(['type' => 'bytes']);
58
59 $this->assertInstanceOf(BytesType::class, $type);
60 }
61
62 public function test_it_parses_cid_link_type(): void
63 {
64 $type = $this->parser->parse(['type' => 'cid-link']);
65
66 $this->assertInstanceOf(CidLinkType::class, $type);
67 }
68
69 public function test_it_parses_unknown_type(): void
70 {
71 $type = $this->parser->parse(['type' => 'unknown']);
72
73 $this->assertInstanceOf(UnknownType::class, $type);
74 }
75
76 public function test_it_throws_on_missing_type(): void
77 {
78 $this->expectException(TypeResolutionException::class);
79 $this->expectExceptionMessage('Unknown Lexicon type: (missing type field)');
80
81 $this->parser->parse([]);
82 }
83
84 public function test_it_throws_on_unknown_type(): void
85 {
86 $this->expectException(TypeResolutionException::class);
87 $this->expectExceptionMessage('Unknown Lexicon type: nonexistent');
88
89 $this->parser->parse(['type' => 'nonexistent']);
90 }
91
92 public function test_it_checks_if_type_is_primitive(): void
93 {
94 $this->assertTrue($this->parser->isPrimitive('null'));
95 $this->assertTrue($this->parser->isPrimitive('boolean'));
96 $this->assertTrue($this->parser->isPrimitive('integer'));
97 $this->assertTrue($this->parser->isPrimitive('string'));
98 $this->assertTrue($this->parser->isPrimitive('bytes'));
99 $this->assertTrue($this->parser->isPrimitive('cid-link'));
100 $this->assertTrue($this->parser->isPrimitive('unknown'));
101
102 $this->assertFalse($this->parser->isPrimitive('object'));
103 $this->assertFalse($this->parser->isPrimitive('array'));
104 $this->assertFalse($this->parser->isPrimitive('ref'));
105 }
106
107 public function test_it_returns_supported_types(): void
108 {
109 $types = $this->parser->getSupportedTypes();
110
111 $this->assertCount(7, $types);
112 $this->assertContains('null', $types);
113 $this->assertContains('boolean', $types);
114 $this->assertContains('integer', $types);
115 $this->assertContains('string', $types);
116 $this->assertContains('bytes', $types);
117 $this->assertContains('cid-link', $types);
118 $this->assertContains('unknown', $types);
119 }
120
121 public function test_it_parses_type_with_properties(): void
122 {
123 $type = $this->parser->parse([
124 'type' => 'string',
125 'description' => 'A test string',
126 'minLength' => 1,
127 'maxLength' => 100,
128 ]);
129
130 $this->assertInstanceOf(StringType::class, $type);
131 $this->assertSame('A test string', $type->description);
132 $this->assertSame(1, $type->minLength);
133 $this->assertSame(100, $type->maxLength);
134 }
135}