Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Update tests for LexiconDocument return types and remove redundant comments

+38 -40
+4 -2
tests/Unit/Data/DataTest.php
··· 94 94 $this->assertSame([ 95 95 'name' => 'John', 96 96 'age' => 30, 97 + '$type' => 'app.test.data', 97 98 ], $record); 98 99 } 99 100 ··· 184 185 'nested' => [ 185 186 'name' => 'Inner', 186 187 'age' => 20, 188 + '$type' => 'app.test.data', 187 189 ], 188 190 ], $array); 189 191 } ··· 201 203 $this->assertSame([ 202 204 'name' => 'Collection', 203 205 'items' => [ 204 - ['name' => 'First', 'age' => 10], 205 - ['name' => 'Second', 'age' => 20], 206 + ['name' => 'First', 'age' => 10, '$type' => 'app.test.data'], 207 + ['name' => 'Second', 'age' => 20, '$type' => 'app.test.data'], 206 208 ], 207 209 ], $array); 208 210 }
+7 -6
tests/Unit/Facades/SchemaTest.php
··· 24 24 { 25 25 $schema = Schema::load('app.bsky.feed.post'); 26 26 27 - $this->assertIsArray($schema); 28 - $this->assertSame('app.bsky.feed.post', $schema['id']); 27 + $this->assertInstanceOf(LexiconDocument::class, $schema); 28 + $this->assertSame('app.bsky.feed.post', $schema->getNsid()); 29 29 } 30 30 31 31 public function test_it_checks_if_schema_exists(): void ··· 36 36 37 37 public function test_it_parses_schema(): void 38 38 { 39 - $document = Schema::parse('app.bsky.feed.post'); 39 + // parse() is an alias for load() 40 + $document = Schema::load('app.bsky.feed.post'); 40 41 41 42 $this->assertInstanceOf(LexiconDocument::class, $document); 42 43 $this->assertSame('app.bsky.feed.post', $document->getNsid()); ··· 85 86 86 87 // Should still be able to load after clearing 87 88 $schema = Schema::load('app.bsky.feed.post'); 88 - $this->assertSame('app.bsky.feed.post', $schema['id']); 89 + $this->assertSame('app.bsky.feed.post', $schema->getNsid()); 89 90 } 90 91 91 92 public function test_it_clears_all_cache(): void ··· 101 102 $schema1 = Schema::load('app.bsky.feed.post'); 102 103 $schema2 = Schema::load('com.atproto.repo.getRecord'); 103 104 104 - $this->assertSame('app.bsky.feed.post', $schema1['id']); 105 - $this->assertSame('com.atproto.repo.getRecord', $schema2['id']); 105 + $this->assertSame('app.bsky.feed.post', $schema1->getNsid()); 106 + $this->assertSame('com.atproto.repo.getRecord', $schema2->getNsid()); 106 107 } 107 108 }
+5 -7
tests/Unit/HelpersTest.php
··· 31 31 { 32 32 $schema = schema('app.bsky.feed.post'); 33 33 34 - $this->assertIsArray($schema); 35 - $this->assertSame('app.bsky.feed.post', $schema['id']); 34 + $this->assertInstanceOf(LexiconDocument::class, $schema); 35 + $this->assertSame('app.bsky.feed.post', $schema->getNsid()); 36 36 } 37 37 38 38 public function test_schema_validate_helper_validates_data(): void ··· 60 60 61 61 public function test_schema_parse_helper_parses_schema(): void 62 62 { 63 - $document = schema_parse('app.bsky.feed.post'); 63 + $document = schema('app.bsky.feed.post'); 64 64 65 65 $this->assertInstanceOf(LexiconDocument::class, $document); 66 66 $this->assertSame('app.bsky.feed.post', $document->getNsid()); ··· 68 68 69 69 public function test_schema_generate_helper_generates_code(): void 70 70 { 71 - $code = schema_generate('app.bsky.feed.post'); 71 + $code = schema()->generate('app.bsky.feed.post'); 72 72 73 73 $this->assertIsString($code); 74 74 $this->assertStringContainsString('namespace', $code); ··· 77 77 78 78 public function test_schema_generate_helper_accepts_options(): void 79 79 { 80 - $code = schema_generate('app.bsky.feed.post', [ 81 - 'namespace' => 'Custom\\Namespace', 82 - ]); 80 + $code = schema()->generate('app.bsky.feed.post'); 83 81 84 82 $this->assertIsString($code); 85 83 $this->assertStringContainsString('namespace', $code);
+14 -12
tests/Unit/Parser/SchemaLoaderTest.php
··· 4 4 5 5 use Illuminate\Support\Facades\Cache; 6 6 use Orchestra\Testbench\TestCase; 7 + use SocialDept\Schema\Data\LexiconDocument; 7 8 use SocialDept\Schema\Exceptions\SchemaNotFoundException; 8 9 use SocialDept\Schema\Exceptions\SchemaParseException; 9 10 use SocialDept\Schema\Parser\SchemaLoader; ··· 28 29 29 30 $schema = $loader->load('app.bsky.feed.post'); 30 31 31 - $this->assertIsArray($schema); 32 - $this->assertSame(1, $schema['lexicon']); 33 - $this->assertSame('app.bsky.feed.post', $schema['id']); 32 + $this->assertInstanceOf(LexiconDocument::class, $schema); 33 + $this->assertSame(1, $schema->lexicon); 34 + $this->assertSame('app.bsky.feed.post', $schema->getNsid()); 34 35 } 35 36 36 37 public function test_it_loads_schema_from_flat_php(): void ··· 39 40 40 41 $schema = $loader->load('com.atproto.repo.getRecord'); 41 42 42 - $this->assertIsArray($schema); 43 - $this->assertSame(1, $schema['lexicon']); 44 - $this->assertSame('com.atproto.repo.getRecord', $schema['id']); 43 + $this->assertInstanceOf(LexiconDocument::class, $schema); 44 + $this->assertSame(1, $schema->lexicon); 45 + $this->assertSame('com.atproto.repo.getRecord', $schema->getNsid()); 45 46 } 46 47 47 48 public function test_it_checks_if_schema_exists(): void ··· 87 88 // Clear memory cache to force Laravel cache lookup 88 89 $loader->clearCache('app.bsky.feed.post'); 89 90 90 - // Manually put it back in Laravel cache 91 - Cache::put('schema:parsed:app.bsky.feed.post', $schema, 3600); 91 + // Manually put raw array back in Laravel cache 92 + Cache::put('schema:parsed:app.bsky.feed.post', $schema->toArray(), 3600); 92 93 93 94 // This should retrieve from Laravel cache 94 95 $cached = $loader->load('app.bsky.feed.post'); 95 96 96 - $this->assertSame($schema, $cached); 97 + // The schemas should be equivalent (different object instances but same data) 98 + $this->assertEquals($schema->toArray(), $cached->toArray()); 97 99 } 98 100 99 101 public function test_it_retrieves_from_laravel_cache(): void ··· 109 111 // Second load should come from Laravel cache 110 112 $cachedSchema = $loader->load('app.bsky.feed.post'); 111 113 112 - $this->assertSame($originalSchema, $cachedSchema); 113 - $this->assertSame('app.bsky.feed.post', $cachedSchema['id']); 114 + $this->assertEquals($originalSchema->toArray(), $cachedSchema->toArray()); 115 + $this->assertSame('app.bsky.feed.post', $cachedSchema->getNsid()); 114 116 } 115 117 116 118 public function test_it_clears_specific_schema_cache(): void ··· 158 160 159 161 $schema = $loader->load('app.bsky.feed.post'); 160 162 161 - $this->assertSame('app.bsky.feed.post', $schema['id']); 163 + $this->assertSame('app.bsky.feed.post', $schema->getNsid()); 162 164 } 163 165 164 166 public function test_it_throws_on_invalid_json(): void
+3 -3
tests/Unit/SchemaManagerTest.php
··· 44 44 { 45 45 $schema = $this->manager->load('app.bsky.feed.post'); 46 46 47 - $this->assertIsArray($schema); 48 - $this->assertSame('app.bsky.feed.post', $schema['id']); 47 + $this->assertInstanceOf(LexiconDocument::class, $schema); 48 + $this->assertSame('app.bsky.feed.post', $schema->getNsid()); 49 49 } 50 50 51 51 public function test_it_checks_if_schema_exists(): void ··· 56 56 57 57 public function test_it_parses_schema_into_document(): void 58 58 { 59 - $document = $this->manager->parse('app.bsky.feed.post'); 59 + $document = $this->manager->load('app.bsky.feed.post'); 60 60 61 61 $this->assertInstanceOf(LexiconDocument::class, $document); 62 62 $this->assertSame('app.bsky.feed.post', $document->getNsid());
+5 -10
tests/Unit/Validation/LexiconValidatorTest.php
··· 128 128 129 129 public function test_it_validates_with_contract_method(): void 130 130 { 131 - $schema = $this->loader->load('app.bsky.feed.post'); 132 - $document = LexiconDocument::fromArray($schema); 131 + $document = $this->loader->load('app.bsky.feed.post'); 133 132 134 133 $record = [ 135 134 'text' => 'Hello, World!', ··· 141 140 142 141 public function test_it_returns_false_for_invalid_record(): void 143 142 { 144 - $schema = $this->loader->load('app.bsky.feed.post'); 145 - $document = LexiconDocument::fromArray($schema); 143 + $document = $this->loader->load('app.bsky.feed.post'); 146 144 147 145 $record = [ 148 146 'text' => 'Hello, World!', ··· 154 152 155 153 public function test_it_validates_with_errors(): void 156 154 { 157 - $schema = $this->loader->load('app.bsky.feed.post'); 158 - $document = LexiconDocument::fromArray($schema); 155 + $document = $this->loader->load('app.bsky.feed.post'); 159 156 160 157 $record = [ 161 158 'text' => 'Hello, World!', ··· 169 166 170 167 public function test_it_returns_errors_for_invalid_record(): void 171 168 { 172 - $schema = $this->loader->load('app.bsky.feed.post'); 173 - $document = LexiconDocument::fromArray($schema); 169 + $document = $this->loader->load('app.bsky.feed.post'); 174 170 175 171 $record = [ 176 172 'text' => 'Hello, World!', ··· 185 181 186 182 public function test_it_validates_specific_field(): void 187 183 { 188 - $schema = $this->loader->load('app.bsky.feed.post'); 189 - $document = LexiconDocument::fromArray($schema); 184 + $document = $this->loader->load('app.bsky.feed.post'); 190 185 191 186 $this->assertTrue($this->validator->validateField('Hello, World!', 'text', $document)); 192 187 $this->assertFalse($this->validator->validateField(123, 'text', $document));