From 58ac0d6996aaba402c7339803000280e4fd7c14f Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Sun, 23 Feb 2020 09:18:59 +0300 Subject: [PATCH] Add DateTimeImmutable tests --- src/RocketChatAttachment.php | 5 ++++- tests/RocketChatAttachmentTest.php | 14 +++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index 75db712..91f354f 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -109,7 +109,10 @@ public function text(string $text): self public function timestamp($timestamp): self { if (! ($timestamp instanceof DateTime) && ! is_string($timestamp)) { - throw new InvalidArgumentException('Timestamp must be string or DateTime, '.gettype($timestamp).' given.'); + throw new InvalidArgumentException(sprintf( + 'Timestamp must be string or DateTime, %s given.', + get_class($timestamp) + )); } if ($timestamp instanceof DateTime) { diff --git a/tests/RocketChatAttachmentTest.php b/tests/RocketChatAttachmentTest.php index 4dc9b9b..16d14ab 100644 --- a/tests/RocketChatAttachmentTest.php +++ b/tests/RocketChatAttachmentTest.php @@ -4,6 +4,8 @@ namespace NotificationChannels\RocketChat\Test; +use DateTime; +use DateTimeImmutable; use NotificationChannels\RocketChat\RocketChatAttachment; use PHPUnit\Framework\TestCase; @@ -63,13 +65,23 @@ public function it_can_set_the_timestamp(): void /** @test */ public function it_can_set_the_timestamp_as_datetime(): void { - $date = \DateTime::createFromFormat('Y-m-d H:i:s.u', '2020-02-19 19:00:00.000'); + $date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2020-02-19 19:00:00.000'); $attachment = new RocketChatAttachment(); $attachment->timestamp($date); $this->assertEquals(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); } + /** @test */ + public function it_can_set_the_timestamp_as_immutable_datetime(): void + { + $date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', '2020-02-19 19:00:00.000'); + $attachment = new RocketChatAttachment(); + $attachment->timestamp($date); + + $this->assertSame(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); + } + /** @test */ public function it_can_set_the_thumb_url(): void {