From 58ac0d6996aaba402c7339803000280e4fd7c14f Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Sun, 23 Feb 2020 09:18:59 +0300 Subject: [PATCH 1/8] 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 { From bbcaffce42c091df21dd38447b317459dba2ec70 Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Sun, 23 Feb 2020 09:31:08 +0300 Subject: [PATCH 2/8] Add negative test for timestamp method --- src/RocketChatAttachment.php | 6 +++++- tests/RocketChatAttachmentTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index 91f354f..2e21fee 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -109,9 +109,13 @@ public function text(string $text): self public function timestamp($timestamp): self { if (! ($timestamp instanceof DateTime) && ! is_string($timestamp)) { + $invalidType = gettype($timestamp); + if ($invalidType === 'object') { + $invalidType = get_class($timestamp); + } throw new InvalidArgumentException(sprintf( 'Timestamp must be string or DateTime, %s given.', - get_class($timestamp) + $invalidType )); } diff --git a/tests/RocketChatAttachmentTest.php b/tests/RocketChatAttachmentTest.php index 16d14ab..1fa199c 100644 --- a/tests/RocketChatAttachmentTest.php +++ b/tests/RocketChatAttachmentTest.php @@ -6,6 +6,7 @@ use DateTime; use DateTimeImmutable; +use InvalidArgumentException; use NotificationChannels\RocketChat\RocketChatAttachment; use PHPUnit\Framework\TestCase; @@ -82,6 +83,16 @@ public function it_can_set_the_timestamp_as_immutable_datetime(): void $this->assertSame(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); } + /** @test */ + public function it_cannot_set_the_timestamp_as_integer(): void + { + $this->expectException(InvalidArgumentException::class); + + $date = 1234567890; + $attachment = new RocketChatAttachment(); + $attachment->timestamp($date); + } + /** @test */ public function it_can_set_the_thumb_url(): void { From 9610434fa8896afeeb0f336eef7b11f7933edd3e Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Mon, 24 Feb 2020 19:02:01 +0300 Subject: [PATCH 3/8] Use DateTimeInterface instead of concrete DateTime class --- src/RocketChatAttachment.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index 2e21fee..69d327e 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -4,7 +4,7 @@ namespace NotificationChannels\RocketChat; -use DateTime; +use DateTimeInterface; use DateTimeZone; use Illuminate\Support\Str; use InvalidArgumentException; @@ -103,23 +103,23 @@ public function text(string $text): self } /** - * @param string|\DateTime $timestamp + * @param string|\DateTimeInterface $timestamp * @return \NotificationChannels\RocketChat\RocketChatAttachment */ public function timestamp($timestamp): self { - if (! ($timestamp instanceof DateTime) && ! is_string($timestamp)) { - $invalidType = gettype($timestamp); - if ($invalidType === 'object') { - $invalidType = get_class($timestamp); - } + if (! ($timestamp instanceof DateTimeInterface) && ! is_string($timestamp)) { + $invalidType = is_object($timestamp) + ? get_class($timestamp) + : gettype($timestamp); + throw new InvalidArgumentException(sprintf( 'Timestamp must be string or DateTime, %s given.', $invalidType )); } - if ($timestamp instanceof DateTime) { + if ($timestamp instanceof DateTimeInterface) { $date = clone $timestamp; $timestamp = $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z'); } From f2b9e7dfded5f4d8a1ff8227f77944dfa63b9a38 Mon Sep 17 00:00:00 2001 From: Nicholas Sterk Date: Tue, 25 Feb 2020 15:57:40 +0100 Subject: [PATCH 4/8] switching to DATE_ATOM --- src/RocketChatAttachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index 69d327e..a687266 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -121,7 +121,7 @@ public function timestamp($timestamp): self if ($timestamp instanceof DateTimeInterface) { $date = clone $timestamp; - $timestamp = $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z'); + $timestamp = $date->format(DateTimeInterface::ATOM); } $this->timestamp = $timestamp; From c803bf03fb808cf5bf51bf45d04cc2fd8cc3b173 Mon Sep 17 00:00:00 2001 From: Nicholas Sterk Date: Tue, 25 Feb 2020 16:00:07 +0100 Subject: [PATCH 5/8] fixing tests --- tests/RocketChatAttachmentTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/RocketChatAttachmentTest.php b/tests/RocketChatAttachmentTest.php index 1fa199c..38271f0 100644 --- a/tests/RocketChatAttachmentTest.php +++ b/tests/RocketChatAttachmentTest.php @@ -9,6 +9,7 @@ use InvalidArgumentException; use NotificationChannels\RocketChat\RocketChatAttachment; use PHPUnit\Framework\TestCase; +use DateTimeInterface; final class RocketChatAttachmentTest extends TestCase { @@ -70,7 +71,7 @@ public function it_can_set_the_timestamp_as_datetime(): void $attachment = new RocketChatAttachment(); $attachment->timestamp($date); - $this->assertEquals(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); + $this->assertEquals(['ts' => $date->format(DateTimeInterface::ATOM)], $attachment->toArray()); } /** @test */ @@ -80,7 +81,7 @@ public function it_can_set_the_timestamp_as_immutable_datetime(): void $attachment = new RocketChatAttachment(); $attachment->timestamp($date); - $this->assertSame(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); + $this->assertSame(['ts' => $date->format(DateTimeInterface::ATOM)], $attachment->toArray()); } /** @test */ From 91f95a02d90557ed86f8133e9a7a690d8434e2be Mon Sep 17 00:00:00 2001 From: Nicholas Sterk Date: Tue, 25 Feb 2020 16:04:17 +0100 Subject: [PATCH 6/8] style fixes --- src/RocketChatAttachment.php | 1 - tests/RocketChatAttachmentTest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index a687266..b2dfb0f 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -5,7 +5,6 @@ namespace NotificationChannels\RocketChat; use DateTimeInterface; -use DateTimeZone; use Illuminate\Support\Str; use InvalidArgumentException; diff --git a/tests/RocketChatAttachmentTest.php b/tests/RocketChatAttachmentTest.php index 38271f0..b767fe4 100644 --- a/tests/RocketChatAttachmentTest.php +++ b/tests/RocketChatAttachmentTest.php @@ -6,10 +6,10 @@ use DateTime; use DateTimeImmutable; +use DateTimeInterface; use InvalidArgumentException; use NotificationChannels\RocketChat\RocketChatAttachment; use PHPUnit\Framework\TestCase; -use DateTimeInterface; final class RocketChatAttachmentTest extends TestCase { From 7b6d79e701b9028646805ac1a7ec4783c854f38b Mon Sep 17 00:00:00 2001 From: Nicholas Sterk Date: Tue, 25 Feb 2020 17:24:11 +0100 Subject: [PATCH 7/8] removing clone --- src/RocketChatAttachment.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index b2dfb0f..4e87b10 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -119,8 +119,7 @@ public function timestamp($timestamp): self } if ($timestamp instanceof DateTimeInterface) { - $date = clone $timestamp; - $timestamp = $date->format(DateTimeInterface::ATOM); + $timestamp = $timestamp->format(DateTimeInterface::ATOM); } $this->timestamp = $timestamp; From d61deb9eb7e77e783ed060297119259fadb13b1d Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Tue, 25 Feb 2020 22:27:50 +0300 Subject: [PATCH 8/8] Replace ATOM with RFC3339 --- src/RocketChatAttachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RocketChatAttachment.php b/src/RocketChatAttachment.php index 4e87b10..6db1281 100644 --- a/src/RocketChatAttachment.php +++ b/src/RocketChatAttachment.php @@ -119,7 +119,7 @@ public function timestamp($timestamp): self } if ($timestamp instanceof DateTimeInterface) { - $timestamp = $timestamp->format(DateTimeInterface::ATOM); + $timestamp = $timestamp->format(DateTimeInterface::RFC3339); } $this->timestamp = $timestamp;