Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1<?php
2
3namespace SocialDept\AtpSchema\Tests\Unit;
4
5use Orchestra\Testbench\TestCase;
6use SocialDept\AtpSchema\Data\LexiconDocument;
7use SocialDept\AtpSchema\SchemaManager;
8use SocialDept\AtpSchema\SchemaServiceProvider;
9
10class HelpersTest extends TestCase
11{
12 protected function getPackageProviders($app): array
13 {
14 return [SchemaServiceProvider::class];
15 }
16
17 protected function defineEnvironment($app): void
18 {
19 $app['config']->set('schema.sources', [__DIR__.'/../fixtures']);
20 $app['config']->set('schema.cache.enabled', false);
21 }
22
23 public function test_schema_helper_returns_manager(): void
24 {
25 $manager = schema();
26
27 $this->assertInstanceOf(SchemaManager::class, $manager);
28 }
29
30 public function test_schema_helper_loads_schema(): void
31 {
32 $schema = schema('app.bsky.feed.post');
33
34 $this->assertInstanceOf(LexiconDocument::class, $schema);
35 $this->assertSame('app.bsky.feed.post', $schema->getNsid());
36 }
37
38 public function test_schema_validate_helper_validates_data(): void
39 {
40 $validData = [
41 'text' => 'Hello, world!',
42 'createdAt' => '2024-01-01T12:00:00Z',
43 ];
44
45 $result = schema_validate('app.bsky.feed.post', $validData);
46
47 $this->assertTrue($result);
48 }
49
50 public function test_schema_validate_helper_returns_false_for_invalid_data(): void
51 {
52 $invalidData = [
53 'text' => '', // Required field
54 ];
55
56 $result = schema_validate('app.bsky.feed.post', $invalidData);
57
58 $this->assertFalse($result);
59 }
60
61 public function test_schema_parse_helper_parses_schema(): void
62 {
63 $document = schema('app.bsky.feed.post');
64
65 $this->assertInstanceOf(LexiconDocument::class, $document);
66 $this->assertSame('app.bsky.feed.post', $document->getNsid());
67 }
68
69 public function test_schema_generate_helper_generates_code(): void
70 {
71 $code = schema()->generate('app.bsky.feed.post');
72
73 $this->assertIsString($code);
74 $this->assertStringContainsString('namespace', $code);
75 $this->assertStringContainsString('class', $code);
76 }
77
78 public function test_schema_generate_helper_accepts_options(): void
79 {
80 $code = schema()->generate('app.bsky.feed.post');
81
82 $this->assertIsString($code);
83 $this->assertStringContainsString('namespace', $code);
84 }
85}