Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1<?php
2
3namespace SocialDept\AtpSchema\Tests\Unit\Validation\Rules;
4
5use Orchestra\Testbench\TestCase;
6use SocialDept\AtpSchema\Validation\Rules\Did;
7
8class DidTest extends TestCase
9{
10 protected Did $rule;
11
12 protected function setUp(): void
13 {
14 parent::setUp();
15
16 $this->rule = new Did();
17 }
18
19 public function test_it_validates_valid_did(): void
20 {
21 $valid = 'did:plc:abcdef123456';
22
23 $failed = false;
24 $this->rule->validate('did', $valid, function () use (&$failed) {
25 $failed = true;
26 });
27
28 $this->assertFalse($failed);
29 }
30
31 public function test_it_validates_various_did_methods(): void
32 {
33 $validDids = [
34 'did:plc:z72i7hdynmk6r22z27h6tvur',
35 'did:web:example.com',
36 'did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK',
37 ];
38
39 foreach ($validDids as $did) {
40 $failed = false;
41 $this->rule->validate('did', $did, function () use (&$failed) {
42 $failed = true;
43 });
44
45 $this->assertFalse($failed, "Expected {$did} to be valid");
46 }
47 }
48
49 public function test_it_rejects_invalid_did(): void
50 {
51 $invalid = 'not-a-did';
52
53 $failed = false;
54 $this->rule->validate('did', $invalid, function () use (&$failed) {
55 $failed = true;
56 });
57
58 $this->assertTrue($failed);
59 }
60
61 public function test_it_rejects_did_without_method(): void
62 {
63 $invalid = 'did:';
64
65 $failed = false;
66 $this->rule->validate('did', $invalid, function () use (&$failed) {
67 $failed = true;
68 });
69
70 $this->assertTrue($failed);
71 }
72
73 public function test_it_rejects_non_string(): void
74 {
75 $failed = false;
76 $this->rule->validate('did', 123, function () use (&$failed) {
77 $failed = true;
78 });
79
80 $this->assertTrue($failed);
81 }
82}