|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CarAndClassic\TalkJS\Tests\Unit; |
| 6 | + |
| 7 | +use CarAndClassic\TalkJS\Models\Conversation; |
| 8 | +use CarAndClassic\TalkJS\Tests\TestCase; |
| 9 | + |
| 10 | +final class ConversationTest extends TestCase |
| 11 | +{ |
| 12 | + private array $conversations; |
| 13 | + |
| 14 | + public function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + $createdAt = time() * 1000; |
| 18 | + $userIds = [ |
| 19 | + 'TestConversationUserId1', |
| 20 | + 'TestConversationUserId2' |
| 21 | + ]; |
| 22 | + $this->conversations = [ |
| 23 | + [ |
| 24 | + 'id' => 'testConversationId1', |
| 25 | + 'subject' => 'Test Conversation 1', |
| 26 | + 'topicId' => 'Test Topic 1', |
| 27 | + 'photoUrl' => null, |
| 28 | + 'welcomeMessages' => ['Test Welcome Message'], |
| 29 | + 'custom' => ['test' => 'test'], |
| 30 | + 'lastMessage' => [ |
| 31 | + 'id' => "test", |
| 32 | + 'type' => "UserMessage", |
| 33 | + 'conversationId' => "dev_test", |
| 34 | + 'senderId' => $userIds[1], |
| 35 | + 'text' => "This is the message copy", |
| 36 | + 'readBy' => [ |
| 37 | + $userIds[0], |
| 38 | + ], |
| 39 | + 'origin' => "rest", |
| 40 | + 'location' => null, |
| 41 | + 'custom' => [], |
| 42 | + 'attachment' => null, |
| 43 | + 'createdAt' => $createdAt, |
| 44 | + ], |
| 45 | + 'participants' => [ |
| 46 | + $userIds[0] => [ |
| 47 | + 'access' => 'ReadWrite', |
| 48 | + 'notify' => true |
| 49 | + ], |
| 50 | + $userIds[1] => [ |
| 51 | + 'access' => 'Read', |
| 52 | + 'notify' => false |
| 53 | + ] |
| 54 | + ], |
| 55 | + 'createdAt' => $createdAt |
| 56 | + ], |
| 57 | + [ |
| 58 | + 'id' => 'testConversationId2', |
| 59 | + 'subject' => 'Test Conversation 2', |
| 60 | + 'topicId' => 'Test Topic 2', |
| 61 | + 'photoUrl' => null, |
| 62 | + 'welcomeMessages' => ['Test Welcome Message'], |
| 63 | + 'custom' => ['test' => 'test'], |
| 64 | + 'lastMessage' => null, |
| 65 | + 'participants' => [ |
| 66 | + $userIds[0] => [ |
| 67 | + 'access' => 'ReadWrite', |
| 68 | + 'notify' => true |
| 69 | + ], |
| 70 | + $userIds[1] => [ |
| 71 | + 'access' => 'Read', |
| 72 | + 'notify' => false |
| 73 | + ] |
| 74 | + ], |
| 75 | + 'createdAt' => $createdAt |
| 76 | + ], |
| 77 | + [ |
| 78 | + 'id' => 'testConversationId3', |
| 79 | + 'subject' => 'Test Conversation 3', |
| 80 | + 'topicId' => 'Test Topic 3', |
| 81 | + 'photoUrl' => null, |
| 82 | + 'welcomeMessages' => ['Test Welcome Message'], |
| 83 | + 'custom' => ['test' => 'test'], |
| 84 | + 'lastMessage' => [ |
| 85 | + 'id' => "test", |
| 86 | + 'type' => "UserMessage", |
| 87 | + 'conversationId' => "dev_test", |
| 88 | + 'senderId' => $userIds[1], |
| 89 | + 'text' => "This is the message copy", |
| 90 | + 'readBy' => [], |
| 91 | + 'origin' => "rest", |
| 92 | + 'location' => null, |
| 93 | + 'custom' => [], |
| 94 | + 'attachment' => null, |
| 95 | + 'createdAt' => $createdAt, |
| 96 | + ], |
| 97 | + 'participants' => [ |
| 98 | + $userIds[0] => [ |
| 99 | + 'access' => 'ReadWrite', |
| 100 | + 'notify' => true |
| 101 | + ], |
| 102 | + $userIds[1] => [ |
| 103 | + 'access' => 'Read', |
| 104 | + 'notify' => false |
| 105 | + ] |
| 106 | + ], |
| 107 | + 'createdAt' => $createdAt |
| 108 | + ] |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + public function testCreateManyFromArray(): void |
| 113 | + { |
| 114 | + $conversations = Conversation::createManyFromArray($this->conversations); |
| 115 | + |
| 116 | + $this->assertIsArray($conversations); |
| 117 | + |
| 118 | + foreach ($conversations as $conversation) { |
| 119 | + $this->assertInstanceOf(Conversation::class, $conversation); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public function testUnreadBy(): void |
| 124 | + { |
| 125 | + $conversation1 = new Conversation($this->conversations[0]); |
| 126 | + $conversation2 = new Conversation($this->conversations[1]); |
| 127 | + $conversation3 = new Conversation($this->conversations[2]); |
| 128 | + |
| 129 | + $this->assertEmpty($conversation1->unreadBy()); |
| 130 | + $this->assertNull($conversation2->unreadBy()); |
| 131 | + $this->assertEquals($conversation3->unreadBy(), ['TestConversationUserId1']); |
| 132 | + } |
| 133 | +} |
0 commit comments