Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/Domain/MessageState.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class MessageState {
*/
private ?DataMessage $dataMessage;

/**
* Created at timestamp
* @var ?string
*/
private ?string $createdAt;

/**
* @param array<RecipientState> $recipients
*/
Expand All @@ -59,7 +65,8 @@ public function __construct(
bool $isHashed = false,
bool $isEncrypted = false,
?TextMessage $textMessage = null,
?DataMessage $dataMessage = null
?DataMessage $dataMessage = null,
?string $createdAt = null
) {
$this->id = $id;
$this->state = $state;
Expand All @@ -68,6 +75,7 @@ public function __construct(
$this->isEncrypted = $isEncrypted;
$this->textMessage = $textMessage;
$this->dataMessage = $dataMessage;
$this->createdAt = $createdAt;
}

/**
Expand Down Expand Up @@ -118,6 +126,14 @@ public function DataMessage(): ?DataMessage {
return $this->dataMessage;
}

/**
* Get created at timestamp
* @return ?string
*/
public function CreatedAt(): ?string {
return $this->createdAt;
}

public function Decrypt(Encryptor $encryptor): self {
if ($this->isHashed) {
return $this;
Expand Down Expand Up @@ -161,7 +177,8 @@ public static function FromObject(object $obj): self {
$obj->isHashed ?? false,
$obj->isEncrypted ?? false,
isset($obj->textMessage) ? TextMessage::FromObject($obj->textMessage) : null,
isset($obj->dataMessage) ? DataMessage::FromObject($obj->dataMessage) : null
isset($obj->dataMessage) ? DataMessage::FromObject($obj->dataMessage) : null,
$obj->createdAt ?? null
);
}
}
65 changes: 65 additions & 0 deletions tests/Domain/MessageStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,71 @@ public function testCanGetRecipients(): void {
$this->assertSame($recipients, $messageState->Recipients());
}

public function testCanGetCreatedAt(): void {
$createdAt = '2024-01-02T03:04:05+02:00';
$messageState = new MessageState('msg_123', $this->processStateMock, [$this->recipientStateMock], false, false, null, null, $createdAt);
$this->assertEquals($createdAt, $messageState->CreatedAt());
}

public function testCreatedAtDefaultsToNull(): void {
$messageState = new MessageState('msg_123', $this->processStateMock, [$this->recipientStateMock]);
$this->assertNull($messageState->CreatedAt());
}

public function testCanCreateFromObjectWithCreatedAt(): void {
$createdAt = '2024-01-02T03:04:05+02:00';
$obj = (object) [
'id' => 'msg_123',
'state' => ProcessState::PENDING,
'createdAt' => $createdAt,
'recipients' => [
(object) [
'phoneNumber' => '+1234567890',
'state' => ProcessState::PENDING,
],
]
];

$messageState = MessageState::FromObject($obj);

$this->assertEquals($createdAt, $messageState->CreatedAt());
}

public function testCanCreateFromObjectWithoutCreatedAt(): void {
$obj = (object) [
'id' => 'msg_123',
'state' => ProcessState::PENDING,
'recipients' => [
(object) [
'phoneNumber' => '+1234567890',
'state' => ProcessState::PENDING,
],
]
];

$messageState = MessageState::FromObject($obj);

$this->assertNull($messageState->CreatedAt());
}

public function testCanCreateFromObjectWithMalformedCreatedAt(): void {
$obj = (object) [
'id' => 'msg_123',
'state' => ProcessState::PENDING,
'createdAt' => 'not-a-valid-timestamp',
'recipients' => [
(object) [
'phoneNumber' => '+1234567890',
'state' => ProcessState::PENDING,
],
]
];

$messageState = MessageState::FromObject($obj);

$this->assertEquals('not-a-valid-timestamp', $messageState->CreatedAt());
}

public function testCanCreateFromObject(): void {
$id = 'msg_123';
$obj = (object) [
Expand Down
Loading