forked from shapintv/talkjs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from carandclassic/add-isreadby-isread-methods-…
…to-message-model Add isreadby isread methods to message model
- Loading branch information
Showing
8 changed files
with
297 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CarAndClassic\TalkJS\Tests\Unit; | ||
|
||
use CarAndClassic\TalkJS\Models\Conversation; | ||
use CarAndClassic\TalkJS\Tests\TestCase; | ||
|
||
final class ConversationTest extends TestCase | ||
{ | ||
private array $conversations; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$createdAt = time() * 1000; | ||
$userIds = [ | ||
'TestConversationUserId1', | ||
'TestConversationUserId2' | ||
]; | ||
$this->conversations = [ | ||
[ | ||
'id' => 'testConversationId1', | ||
'subject' => 'Test Conversation 1', | ||
'topicId' => 'Test Topic 1', | ||
'photoUrl' => null, | ||
'welcomeMessages' => ['Test Welcome Message'], | ||
'custom' => ['test' => 'test'], | ||
'lastMessage' => [ | ||
'id' => "test", | ||
'type' => "UserMessage", | ||
'conversationId' => "dev_test", | ||
'senderId' => $userIds[1], | ||
'text' => "This is the message copy", | ||
'readBy' => [ | ||
$userIds[0], | ||
], | ||
'origin' => "rest", | ||
'location' => null, | ||
'custom' => [], | ||
'attachment' => null, | ||
'createdAt' => $createdAt, | ||
], | ||
'participants' => [ | ||
$userIds[0] => [ | ||
'access' => 'ReadWrite', | ||
'notify' => true | ||
], | ||
$userIds[1] => [ | ||
'access' => 'Read', | ||
'notify' => false | ||
] | ||
], | ||
'createdAt' => $createdAt | ||
], | ||
[ | ||
'id' => 'testConversationId2', | ||
'subject' => 'Test Conversation 2', | ||
'topicId' => 'Test Topic 2', | ||
'photoUrl' => null, | ||
'welcomeMessages' => ['Test Welcome Message'], | ||
'custom' => ['test' => 'test'], | ||
'lastMessage' => null, | ||
'participants' => [ | ||
$userIds[0] => [ | ||
'access' => 'ReadWrite', | ||
'notify' => true | ||
], | ||
$userIds[1] => [ | ||
'access' => 'Read', | ||
'notify' => false | ||
] | ||
], | ||
'createdAt' => $createdAt | ||
], | ||
[ | ||
'id' => 'testConversationId3', | ||
'subject' => 'Test Conversation 3', | ||
'topicId' => 'Test Topic 3', | ||
'photoUrl' => null, | ||
'welcomeMessages' => ['Test Welcome Message'], | ||
'custom' => ['test' => 'test'], | ||
'lastMessage' => [ | ||
'id' => "test", | ||
'type' => "UserMessage", | ||
'conversationId' => "dev_test", | ||
'senderId' => $userIds[1], | ||
'text' => "This is the message copy", | ||
'readBy' => [], | ||
'origin' => "rest", | ||
'location' => null, | ||
'custom' => [], | ||
'attachment' => null, | ||
'createdAt' => $createdAt, | ||
], | ||
'participants' => [ | ||
$userIds[0] => [ | ||
'access' => 'ReadWrite', | ||
'notify' => true | ||
], | ||
$userIds[1] => [ | ||
'access' => 'Read', | ||
'notify' => false | ||
] | ||
], | ||
'createdAt' => $createdAt | ||
] | ||
]; | ||
} | ||
|
||
public function testCreateManyFromArray(): void | ||
{ | ||
$conversations = Conversation::createManyFromArray($this->conversations); | ||
|
||
$this->assertIsArray($conversations); | ||
|
||
foreach ($conversations as $conversation) { | ||
$this->assertInstanceOf(Conversation::class, $conversation); | ||
} | ||
} | ||
|
||
public function testUnreadBy(): void | ||
{ | ||
$conversation1 = new Conversation($this->conversations[0]); | ||
$conversation2 = new Conversation($this->conversations[1]); | ||
$conversation3 = new Conversation($this->conversations[2]); | ||
|
||
$this->assertEmpty($conversation1->unreadBy()); | ||
$this->assertNull($conversation2->unreadBy()); | ||
$this->assertEquals($conversation3->unreadBy(), ['TestConversationUserId1']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CarAndClassic\TalkJS\Tests\Unit; | ||
|
||
use CarAndClassic\TalkJS\Enumerations\MessageType; | ||
use CarAndClassic\TalkJS\Models\Message; | ||
use CarAndClassic\TalkJS\Tests\TestCase; | ||
|
||
final class MessageTest extends TestCase | ||
{ | ||
private string $senderId; | ||
private array $messages; | ||
private Message $message1; | ||
private Message $message2; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$conversationId = 'testConversationId'; | ||
$this->senderId = 'testSenderId'; | ||
$this->userIds = [ | ||
'TestConversationUserId1', | ||
'TestConversationUserId2' | ||
]; | ||
$this->messages = [ | ||
[ | ||
'id' => '1', // At time of writing results are returned descending | ||
'type' => MessageType::USER, | ||
'conversationId' => $conversationId, | ||
'senderId' => $this->senderId, | ||
'text' => 'Test User Message', | ||
'readBy' => [ | ||
$this->userIds[0] | ||
], | ||
'origin' => 'rest', | ||
'location' => null, | ||
'custom' => ['test' => 'test'], | ||
'createdAt' => (time() + 1) * 1000, // At time of writing TalkJS returns timestamp in milliseconds | ||
'attachment' => null | ||
], | ||
[ | ||
'id' => '2', | ||
'type' => MessageType::SYSTEM, | ||
'conversationId' => $conversationId, | ||
'senderId' => null, | ||
'text' => 'Test System Message', | ||
'readBy' => [], | ||
'origin' => 'rest', | ||
'location' => null, | ||
'custom' => ['test' => 'test'], | ||
'createdAt' => time() * 1000, | ||
'attachment' => null | ||
] | ||
]; | ||
$this->message1 = new Message($this->messages[0]); | ||
$this->message2 = new Message($this->messages[1]); | ||
} | ||
|
||
public function testCreateManyFromArray(): void | ||
{ | ||
$messages = Message::createManyFromArray($this->messages); | ||
|
||
$this->assertIsArray($messages); | ||
|
||
foreach ($messages as $message) { | ||
$this->assertInstanceOf(Message::class, $message); | ||
} | ||
} | ||
|
||
public function testIsUserMessage(): void | ||
{ | ||
$this->assertSame($this->message1->isUserMessage(), true); | ||
$this->assertSame($this->message2->isUserMessage(), false); | ||
} | ||
|
||
public function testIsSystemMessage(): void | ||
{ | ||
$this->assertSame($this->message1->isSystemMessage(), false); | ||
$this->assertSame($this->message2->isSystemMessage(), true); | ||
} | ||
|
||
public function testIsReadBy(): void | ||
{ | ||
$this->assertSame($this->message1->isReadBy($this->userIds[0]), true); | ||
$this->assertSame($this->message1->isReadBy($this->userIds[1]), false); | ||
$this->assertSame($this->message1->isReadBy($this->senderId), true); | ||
$this->assertSame($this->message2->isReadBy($this->userIds[0]), false); | ||
} | ||
|
||
public function testIsRead(): void | ||
{ | ||
$this->assertSame($this->message1->isRead(), true); | ||
$this->assertSame($this->message2->isRead(), false); | ||
} | ||
} |