Skip to content

Commit

Permalink
Add DateTimeImmutable tests & make tests asserts stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Feb 23, 2020
1 parent 27da688 commit a9fa2ad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/RocketChatAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
58 changes: 35 additions & 23 deletions tests/RocketChatAttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace NotificationChannels\RocketChat\Test;

use DateTime;
use DateTimeImmutable;
use NotificationChannels\RocketChat\RocketChatAttachment;
use PHPUnit\Framework\TestCase;

Expand All @@ -14,23 +16,23 @@ 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 */
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 */
public function it_returns_an_empty_array_if_not_configured(): void
{
$attachment = new RocketChatAttachment();

$this->assertEquals([], $attachment->toArray());
$this->assertSame([], $attachment->toArray());
}

/** @test */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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',
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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());
}
}

0 comments on commit a9fa2ad

Please sign in to comment.