From a9fa2ad41c821c9a9d8cdc3e7f075fad1efa25ac Mon Sep 17 00:00:00 2001 From: antonkomarev Date: Sun, 23 Feb 2020 09:18:59 +0300 Subject: [PATCH] Add DateTimeImmutable tests & make tests asserts stricter --- src/RocketChatAttachment.php | 5 ++- tests/RocketChatAttachmentTest.php | 58 ++++++++++++++++++------------ 2 files changed, 39 insertions(+), 24 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..b8e4fc2 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; @@ -14,7 +16,7 @@ public function it_can_accept_a_config_when_constructing_an_attachment(): void { $attachment = new RocketChatAttachment(['title' => 'test123']); - $this->assertEquals(['title' => 'test123'], $attachment->toArray()); + $this->assertSame(['title' => 'test123'], $attachment->toArray()); } /** @test */ @@ -22,7 +24,7 @@ public function it_can_accept_a_config_when_creating_an_attachment(): void { $attachment = RocketChatAttachment::make(['title' => 'test123']); - $this->assertEquals(['title' => 'test123'], $attachment->toArray()); + $this->assertSame(['title' => 'test123'], $attachment->toArray()); } /** @test */ @@ -30,7 +32,7 @@ public function it_returns_an_empty_array_if_not_configured(): void { $attachment = new RocketChatAttachment(); - $this->assertEquals([], $attachment->toArray()); + $this->assertSame([], $attachment->toArray()); } /** @test */ @@ -39,7 +41,7 @@ public function it_can_set_the_color(): void $attachment = new RocketChatAttachment(); $attachment->color('#FFFFFF'); - $this->assertEquals(['color' => '#FFFFFF'], $attachment->toArray()); + $this->assertSame(['color' => '#FFFFFF'], $attachment->toArray()); } /** @test */ @@ -48,7 +50,7 @@ public function it_can_set_the_text(): void $attachment = new RocketChatAttachment(); $attachment->text('test123'); - $this->assertEquals(['text' => 'test123'], $attachment->toArray()); + $this->assertSame(['text' => 'test123'], $attachment->toArray()); } /** @test */ @@ -57,17 +59,27 @@ public function it_can_set_the_timestamp(): void $attachment = new RocketChatAttachment(); $attachment->timestamp('2020-02-19T19:00:00.000Z'); - $this->assertEquals(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); + $this->assertSame(['ts' => '2020-02-19T19:00:00.000Z'], $attachment->toArray()); } /** @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()); + $this->assertSame(['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 */ @@ -76,7 +88,7 @@ public function it_can_set_the_thumb_url(): void $attachment = new RocketChatAttachment(); $attachment->thumbnailUrl('test123'); - $this->assertEquals(['thumb_url' => 'test123'], $attachment->toArray()); + $this->assertSame(['thumb_url' => 'test123'], $attachment->toArray()); } /** @test */ @@ -85,7 +97,7 @@ public function it_can_set_the_message_link(): void $attachment = new RocketChatAttachment(); $attachment->messageLink('test123'); - $this->assertEquals(['message_link' => 'test123'], $attachment->toArray()); + $this->assertSame(['message_link' => 'test123'], $attachment->toArray()); } /** @test */ @@ -94,7 +106,7 @@ public function it_can_set_the_collapsed(): void $attachment = new RocketChatAttachment(); $attachment->collapsed(true); - $this->assertEquals(['collapsed' => true], $attachment->toArray()); + $this->assertSame(['collapsed' => true], $attachment->toArray()); } /** @test */ @@ -103,7 +115,7 @@ public function it_can_set_the_author_name(): void $attachment = new RocketChatAttachment(); $attachment->authorName('author'); - $this->assertEquals(['author_name' => 'author'], $attachment->toArray()); + $this->assertSame(['author_name' => 'author'], $attachment->toArray()); } /** @test */ @@ -112,7 +124,7 @@ public function it_can_set_the_author_link(): void $attachment = new RocketChatAttachment(); $attachment->authorLink('test123'); - $this->assertEquals(['author_link' => 'test123'], $attachment->toArray()); + $this->assertSame(['author_link' => 'test123'], $attachment->toArray()); } /** @test */ @@ -121,7 +133,7 @@ public function it_can_set_the_author_icon(): void $attachment = new RocketChatAttachment(); $attachment->authorIcon('test123'); - $this->assertEquals(['author_icon' => 'test123'], $attachment->toArray()); + $this->assertSame(['author_icon' => 'test123'], $attachment->toArray()); } /** @test */ @@ -130,7 +142,7 @@ public function it_can_set_the_author(): void $attachment = new RocketChatAttachment(); $attachment->author('aname', 'alink', 'aicon'); - $this->assertEquals([ + $this->assertSame([ 'author_name' => 'aname', 'author_link' => 'alink', 'author_icon' => 'aicon', @@ -143,7 +155,7 @@ public function it_can_set_the_title(): void $attachment = new RocketChatAttachment(); $attachment->title('test123'); - $this->assertEquals(['title' => 'test123'], $attachment->toArray()); + $this->assertSame(['title' => 'test123'], $attachment->toArray()); } /** @test */ @@ -152,7 +164,7 @@ public function it_can_set_the_title_link(): void $attachment = new RocketChatAttachment(); $attachment->titleLink('test123'); - $this->assertEquals(['title_link' => 'test123'], $attachment->toArray()); + $this->assertSame(['title_link' => 'test123'], $attachment->toArray()); } /** @test */ @@ -161,7 +173,7 @@ public function it_can_set_the_title_link_download(): void $attachment = new RocketChatAttachment(); $attachment->titleLinkDownload(true); - $this->assertEquals(['title_link_download' => true], $attachment->toArray()); + $this->assertSame(['title_link_download' => true], $attachment->toArray()); } /** @test */ @@ -170,7 +182,7 @@ public function it_can_set_the_image_url(): void $attachment = new RocketChatAttachment(); $attachment->imageUrl('test123'); - $this->assertEquals(['image_url' => 'test123'], $attachment->toArray()); + $this->assertSame(['image_url' => 'test123'], $attachment->toArray()); } /** @test */ @@ -179,7 +191,7 @@ public function it_can_set_the_audio_url(): void $attachment = new RocketChatAttachment(); $attachment->audioUrl('test123'); - $this->assertEquals(['audio_url' => 'test123'], $attachment->toArray()); + $this->assertSame(['audio_url' => 'test123'], $attachment->toArray()); } /** @test */ @@ -188,7 +200,7 @@ public function it_can_set_the_video_url(): void $attachment = new RocketChatAttachment(); $attachment->videoUrl('test123'); - $this->assertEquals(['video_url' => 'test123'], $attachment->toArray()); + $this->assertSame(['video_url' => 'test123'], $attachment->toArray()); } /** @test */ @@ -209,13 +221,13 @@ public function it_can_set_the_fields(): void $attachment = new RocketChatAttachment(); $attachment->fields($fields); - $this->assertEquals(['fields' => $fields], $attachment->toArray()); + $this->assertSame(['fields' => $fields], $attachment->toArray()); } /** @test */ public function it_cannot_set_unknown_field(): void { $attachment = new RocketChatAttachment(['notExisting']); - $this->assertEquals([], $attachment->toArray()); + $this->assertSame([], $attachment->toArray()); } }