Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1<?php
2
3namespace SocialDept\AtpSchema\Tests\Unit\Data;
4
5use Orchestra\Testbench\TestCase;
6use SocialDept\AtpSchema\Data\BlobReference;
7use SocialDept\AtpSchema\Exceptions\SchemaValidationException;
8
9class BlobReferenceTest extends TestCase
10{
11 public function test_it_creates_from_constructor(): void
12 {
13 $blob = new BlobReference(
14 'bafyreig...',
15 'image/png',
16 12345
17 );
18
19 $this->assertEquals('bafyreig...', $blob->ref);
20 $this->assertEquals('image/png', $blob->mimeType);
21 $this->assertEquals(12345, $blob->size);
22 }
23
24 public function test_it_creates_from_array_with_link_object(): void
25 {
26 $data = [
27 '$type' => 'blob',
28 'ref' => [
29 '$link' => 'bafyreig...',
30 ],
31 'mimeType' => 'image/jpeg',
32 'size' => 54321,
33 ];
34
35 $blob = BlobReference::fromArray($data);
36
37 $this->assertEquals('bafyreig...', $blob->ref);
38 $this->assertEquals('image/jpeg', $blob->mimeType);
39 $this->assertEquals(54321, $blob->size);
40 }
41
42 public function test_it_creates_from_array_with_string_ref(): void
43 {
44 $data = [
45 'ref' => 'bafyreig...',
46 'mimeType' => 'video/mp4',
47 'size' => 999,
48 ];
49
50 $blob = BlobReference::fromArray($data);
51
52 $this->assertEquals('bafyreig...', $blob->ref);
53 $this->assertEquals('video/mp4', $blob->mimeType);
54 $this->assertEquals(999, $blob->size);
55 }
56
57 public function test_it_throws_exception_when_ref_is_missing(): void
58 {
59 $this->expectException(SchemaValidationException::class);
60
61 BlobReference::fromArray([
62 'mimeType' => 'image/png',
63 'size' => 123,
64 ]);
65 }
66
67 public function test_it_throws_exception_when_mime_type_is_missing(): void
68 {
69 $this->expectException(SchemaValidationException::class);
70
71 BlobReference::fromArray([
72 'ref' => 'bafyreig...',
73 'size' => 123,
74 ]);
75 }
76
77 public function test_it_throws_exception_when_size_is_missing(): void
78 {
79 $this->expectException(SchemaValidationException::class);
80
81 BlobReference::fromArray([
82 'ref' => 'bafyreig...',
83 'mimeType' => 'image/png',
84 ]);
85 }
86
87 public function test_it_converts_to_array(): void
88 {
89 $blob = new BlobReference(
90 'bafyreig...',
91 'image/png',
92 12345
93 );
94
95 $array = $blob->toArray();
96
97 $this->assertEquals([
98 '$type' => 'blob',
99 'ref' => [
100 '$link' => 'bafyreig...',
101 ],
102 'mimeType' => 'image/png',
103 'size' => 12345,
104 ], $array);
105 }
106
107 public function test_it_gets_cid(): void
108 {
109 $blob = new BlobReference('bafyreig123', 'image/png', 100);
110
111 $this->assertEquals('bafyreig123', $blob->getCid());
112 }
113
114 public function test_it_gets_mime_type(): void
115 {
116 $blob = new BlobReference('bafyreig...', 'video/webm', 100);
117
118 $this->assertEquals('video/webm', $blob->getMimeType());
119 }
120
121 public function test_it_gets_size(): void
122 {
123 $blob = new BlobReference('bafyreig...', 'image/png', 54321);
124
125 $this->assertEquals(54321, $blob->getSize());
126 }
127
128 public function test_it_checks_if_is_image(): void
129 {
130 $imageBlob = new BlobReference('cid1', 'image/png', 100);
131 $videoBlob = new BlobReference('cid2', 'video/mp4', 200);
132
133 $this->assertTrue($imageBlob->isImage());
134 $this->assertFalse($videoBlob->isImage());
135 }
136
137 public function test_it_checks_if_is_video(): void
138 {
139 $imageBlob = new BlobReference('cid1', 'image/png', 100);
140 $videoBlob = new BlobReference('cid2', 'video/mp4', 200);
141
142 $this->assertFalse($imageBlob->isVideo());
143 $this->assertTrue($videoBlob->isVideo());
144 }
145
146 public function test_it_matches_exact_mime_type(): void
147 {
148 $blob = new BlobReference('cid', 'image/png', 100);
149
150 $this->assertTrue($blob->matchesMimeType('image/png'));
151 $this->assertFalse($blob->matchesMimeType('image/jpeg'));
152 }
153
154 public function test_it_matches_wildcard_mime_type(): void
155 {
156 $imageBlob = new BlobReference('cid1', 'image/png', 100);
157 $videoBlob = new BlobReference('cid2', 'video/mp4', 200);
158
159 $this->assertTrue($imageBlob->matchesMimeType('image/*'));
160 $this->assertFalse($imageBlob->matchesMimeType('video/*'));
161 $this->assertTrue($videoBlob->matchesMimeType('video/*'));
162 $this->assertFalse($videoBlob->matchesMimeType('image/*'));
163 }
164
165 public function test_it_matches_universal_wildcard(): void
166 {
167 $blob = new BlobReference('cid', 'application/json', 100);
168
169 $this->assertTrue($blob->matchesMimeType('*/*'));
170 }
171
172 public function test_it_handles_different_image_types(): void
173 {
174 $pngBlob = new BlobReference('cid1', 'image/png', 100);
175 $jpegBlob = new BlobReference('cid2', 'image/jpeg', 200);
176 $webpBlob = new BlobReference('cid3', 'image/webp', 300);
177
178 $this->assertTrue($pngBlob->isImage());
179 $this->assertTrue($jpegBlob->isImage());
180 $this->assertTrue($webpBlob->isImage());
181 }
182
183 public function test_it_handles_different_video_types(): void
184 {
185 $mp4Blob = new BlobReference('cid1', 'video/mp4', 100);
186 $webmBlob = new BlobReference('cid2', 'video/webm', 200);
187
188 $this->assertTrue($mp4Blob->isVideo());
189 $this->assertTrue($webmBlob->isVideo());
190 }
191
192 public function test_it_converts_size_to_integer(): void
193 {
194 $data = [
195 'ref' => 'cid',
196 'mimeType' => 'image/png',
197 'size' => '12345',
198 ];
199
200 $blob = BlobReference::fromArray($data);
201
202 $this->assertIsInt($blob->size);
203 $this->assertEquals(12345, $blob->size);
204 }
205}