Maintain local ⭤ remote in sync with automatic AT Protocol parity for Laravel (alpha & unstable)
at dev 107 lines 3.2 kB view raw
1<?php 2 3namespace SocialDept\AtpParity\Tests\Unit; 4 5use SocialDept\AtpParity\MapperRegistry; 6use SocialDept\AtpParity\Tests\Fixtures\TestMapper; 7use SocialDept\AtpParity\Tests\Fixtures\TestModel; 8use SocialDept\AtpParity\Tests\Fixtures\TestRecord; 9use SocialDept\AtpParity\Tests\TestCase; 10 11class MapperRegistryTest extends TestCase 12{ 13 private MapperRegistry $registry; 14 15 protected function setUp(): void 16 { 17 parent::setUp(); 18 $this->registry = new MapperRegistry(); 19 } 20 21 public function test_register_adds_mapper_to_all_indices(): void 22 { 23 $mapper = new TestMapper(); 24 25 $this->registry->register($mapper); 26 27 $this->assertSame($mapper, $this->registry->forRecord(TestRecord::class)); 28 $this->assertSame($mapper, $this->registry->forModel(TestModel::class)); 29 $this->assertSame($mapper, $this->registry->forLexicon('app.test.record')); 30 } 31 32 public function test_for_record_returns_null_for_unregistered_class(): void 33 { 34 $result = $this->registry->forRecord('NonExistent\\Record'); 35 36 $this->assertNull($result); 37 } 38 39 public function test_for_model_returns_null_for_unregistered_class(): void 40 { 41 $result = $this->registry->forModel('NonExistent\\Model'); 42 43 $this->assertNull($result); 44 } 45 46 public function test_for_lexicon_returns_null_for_unregistered_nsid(): void 47 { 48 $result = $this->registry->forLexicon('app.unknown.record'); 49 50 $this->assertNull($result); 51 } 52 53 public function test_has_lexicon_returns_true_when_registered(): void 54 { 55 $this->registry->register(new TestMapper()); 56 57 $this->assertTrue($this->registry->hasLexicon('app.test.record')); 58 } 59 60 public function test_has_lexicon_returns_false_when_not_registered(): void 61 { 62 $this->assertFalse($this->registry->hasLexicon('app.unknown.record')); 63 } 64 65 public function test_lexicons_returns_all_registered_nsids(): void 66 { 67 $this->registry->register(new TestMapper()); 68 69 $lexicons = $this->registry->lexicons(); 70 71 $this->assertContains('app.test.record', $lexicons); 72 $this->assertCount(1, $lexicons); 73 } 74 75 public function test_lexicons_returns_empty_array_when_no_mappers_registered(): void 76 { 77 $this->assertEmpty($this->registry->lexicons()); 78 } 79 80 public function test_all_returns_all_registered_mappers(): void 81 { 82 $mapper = new TestMapper(); 83 $this->registry->register($mapper); 84 85 $all = $this->registry->all(); 86 87 $this->assertCount(1, $all); 88 $this->assertSame($mapper, $all[0]); 89 } 90 91 public function test_all_returns_empty_array_when_no_mappers_registered(): void 92 { 93 $this->assertEmpty($this->registry->all()); 94 } 95 96 public function test_registering_same_mapper_twice_overwrites(): void 97 { 98 $mapper1 = new TestMapper(); 99 $mapper2 = new TestMapper(); 100 101 $this->registry->register($mapper1); 102 $this->registry->register($mapper2); 103 104 $this->assertSame($mapper2, $this->registry->forRecord(TestRecord::class)); 105 $this->assertCount(1, $this->registry->all()); 106 } 107}