Maintain local ⭤ remote in sync with automatic AT Protocol parity for Laravel (alpha & unstable)
at main 128 lines 4.2 kB view raw
1<?php 2 3namespace SocialDept\AtpParity\Tests\Unit\Concerns; 4 5use Carbon\Carbon; 6use SocialDept\AtpParity\MapperRegistry; 7use SocialDept\AtpParity\Tests\Fixtures\SyncableMapper; 8use SocialDept\AtpParity\Tests\Fixtures\SyncableModel; 9use SocialDept\AtpParity\Tests\Fixtures\TestRecord; 10use SocialDept\AtpParity\Tests\TestCase; 11 12class SyncsWithAtpTest extends TestCase 13{ 14 public function test_get_atp_synced_at_column_returns_default(): void 15 { 16 $model = new SyncableModel(); 17 18 $this->assertSame('atp_synced_at', $model->getAtpSyncedAtColumn()); 19 } 20 21 public function test_get_atp_synced_at_returns_timestamp(): void 22 { 23 $now = Carbon::now(); 24 $model = new SyncableModel(['atp_synced_at' => $now]); 25 26 $this->assertEquals($now->toDateTimeString(), $model->getAtpSyncedAt()->toDateTimeString()); 27 } 28 29 public function test_get_atp_synced_at_returns_null_when_not_set(): void 30 { 31 $model = new SyncableModel(); 32 33 $this->assertNull($model->getAtpSyncedAt()); 34 } 35 36 public function test_mark_as_synced_sets_all_attributes(): void 37 { 38 Carbon::setTestNow('2024-01-15 12:00:00'); 39 40 $model = new SyncableModel(); 41 $model->markAsSynced('at://did/col/rkey', 'cid123'); 42 43 $this->assertSame('at://did/col/rkey', $model->atp_uri); 44 $this->assertSame('cid123', $model->atp_cid); 45 $this->assertSame('2024-01-15 12:00:00', $model->atp_synced_at->toDateTimeString()); 46 47 Carbon::setTestNow(); 48 } 49 50 public function test_has_local_changes_returns_true_when_never_synced(): void 51 { 52 $model = new SyncableModel(); 53 54 $this->assertTrue($model->hasLocalChanges()); 55 } 56 57 public function test_has_local_changes_returns_false_when_no_updated_at(): void 58 { 59 $model = new SyncableModel([ 60 'atp_synced_at' => Carbon::now(), 61 ]); 62 63 $this->assertFalse($model->hasLocalChanges()); 64 } 65 66 public function test_has_local_changes_returns_true_when_updated_after_sync(): void 67 { 68 $model = new SyncableModel([ 69 'atp_synced_at' => Carbon::parse('2024-01-15 12:00:00'), 70 'updated_at' => Carbon::parse('2024-01-15 13:00:00'), 71 ]); 72 73 $this->assertTrue($model->hasLocalChanges()); 74 } 75 76 public function test_has_local_changes_returns_false_when_synced_after_update(): void 77 { 78 $model = new SyncableModel([ 79 'updated_at' => Carbon::parse('2024-01-15 12:00:00'), 80 'atp_synced_at' => Carbon::parse('2024-01-15 13:00:00'), 81 ]); 82 83 $this->assertFalse($model->hasLocalChanges()); 84 } 85 86 public function test_update_from_record_updates_model_and_sync_timestamp(): void 87 { 88 Carbon::setTestNow('2024-01-15 14:00:00'); 89 90 $registry = app(MapperRegistry::class); 91 $registry->register(new SyncableMapper()); 92 93 $model = new SyncableModel(['content' => 'Original']); 94 $record = new TestRecord(text: 'From remote'); 95 96 $model->updateFromRecord($record, 'at://did/col/rkey', 'newcid'); 97 98 $this->assertSame('From remote', $model->content); 99 $this->assertSame('at://did/col/rkey', $model->atp_uri); 100 $this->assertSame('newcid', $model->atp_cid); 101 $this->assertSame('2024-01-15 14:00:00', $model->atp_synced_at->toDateTimeString()); 102 103 Carbon::setTestNow(); 104 } 105 106 public function test_update_from_record_does_nothing_without_mapper(): void 107 { 108 $this->app->forgetInstance(MapperRegistry::class); 109 $this->app->singleton(MapperRegistry::class); 110 111 $model = new SyncableModel(['content' => 'Original']); 112 $record = new TestRecord(text: 'From remote'); 113 114 $model->updateFromRecord($record, 'at://did/col/rkey', 'cid'); 115 116 $this->assertSame('Original', $model->content); 117 } 118 119 public function test_inherits_has_atp_record_methods(): void 120 { 121 $model = new SyncableModel(['atp_uri' => 'at://did:plc:test/app.test.record/rkey']); 122 123 $this->assertTrue($model->hasAtpRecord()); 124 $this->assertSame('did:plc:test', $model->getAtpDid()); 125 $this->assertSame('app.test.record', $model->getAtpCollection()); 126 $this->assertSame('rkey', $model->getAtpRkey()); 127 } 128}