Skip to content

Commit

Permalink
Allow RocketChatAttachment class accept DateTimeImmutable as timestam…
Browse files Browse the repository at this point in the history
…p argument (#9)

* Use DateTimeInterface instead of concrete DateTime class

* Add DateTimeImmutable tests

* Add negative test for timestamp method

Co-authored-by: Nicholas <[email protected]>
  • Loading branch information
antonkomarev and Funfare authored Mar 1, 2020
1 parent 0556a13 commit 689fee0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
21 changes: 13 additions & 8 deletions src/RocketChatAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace NotificationChannels\RocketChat;

use DateTime;
use DateTimeZone;
use DateTimeInterface;
use Illuminate\Support\Str;
use InvalidArgumentException;

Expand Down Expand Up @@ -103,18 +102,24 @@ 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)) {
throw new InvalidArgumentException('Timestamp must be string or DateTime, '.gettype($timestamp).' given.');
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) {
$date = clone $timestamp;
$timestamp = $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z');
if ($timestamp instanceof DateTimeInterface) {
$timestamp = $timestamp->format(DateTimeInterface::RFC3339);
}

$this->timestamp = $timestamp;
Expand Down
28 changes: 26 additions & 2 deletions tests/RocketChatAttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace NotificationChannels\RocketChat\Test;

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

Expand Down Expand Up @@ -63,11 +67,31 @@ 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());
$this->assertEquals(['ts' => $date->format(DateTimeInterface::ATOM)], $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' => $date->format(DateTimeInterface::ATOM)], $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 */
Expand Down

0 comments on commit 689fee0

Please sign in to comment.