From 2ee6083150eecaf82a80f204f7bdb0b6dca8f037 Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Sun, 30 Aug 2026 07:57:15 +0700 Subject: [PATCH] [domain] add `createdAt` field to message state --- src/Domain/MessageState.php | 21 +++++++++- tests/Domain/MessageStateTest.php | 65 +++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/Domain/MessageState.php b/src/Domain/MessageState.php index 6de8d17..810a10c 100644 --- a/src/Domain/MessageState.php +++ b/src/Domain/MessageState.php @@ -49,6 +49,12 @@ class MessageState { */ private ?DataMessage $dataMessage; + /** + * Created at timestamp + * @var ?string + */ + private ?string $createdAt; + /** * @param array $recipients */ @@ -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; @@ -68,6 +75,7 @@ public function __construct( $this->isEncrypted = $isEncrypted; $this->textMessage = $textMessage; $this->dataMessage = $dataMessage; + $this->createdAt = $createdAt; } /** @@ -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; @@ -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 ); } } \ No newline at end of file diff --git a/tests/Domain/MessageStateTest.php b/tests/Domain/MessageStateTest.php index 8297b8a..e72dd80 100644 --- a/tests/Domain/MessageStateTest.php +++ b/tests/Domain/MessageStateTest.php @@ -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) [