|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is a part of the DiscordPHP project. |
| 7 | + * |
| 8 | + * Copyright (c) 2015-2022 David Cole <david.cole1340@gmail.com> |
| 9 | + * Copyright (c) 2020-present Valithor Obsidion <valithor@discordphp.org> |
| 10 | + * |
| 11 | + * This file is subject to the MIT license that is bundled |
| 12 | + * with this source code in the LICENSE.md file. |
| 13 | + */ |
| 14 | + |
| 15 | +use Discord\Helpers\Timestamp; |
| 16 | + |
| 17 | +final class TimestampTest extends DiscordTestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @covers \Discord\Helpers\Timestamp::__construct |
| 21 | + * @covers \Discord\Helpers\Timestamp::__toString |
| 22 | + */ |
| 23 | + public function testCreatesTimestampWithDefaultFormat(): void |
| 24 | + { |
| 25 | + $timestamp = new Timestamp(1700000000); |
| 26 | + |
| 27 | + $this->assertSame(1700000000, $timestamp->timestamp); |
| 28 | + $this->assertSame(Timestamp::STYLE_SHORT_DATE_TIME, $timestamp->format); |
| 29 | + $this->assertSame('<t:1700000000:f>', (string) $timestamp); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @covers \Discord\Helpers\Timestamp::new |
| 34 | + */ |
| 35 | + public function testCreatesTimestampWithFactory(): void |
| 36 | + { |
| 37 | + $timestamp = Timestamp::new('1700000000', Timestamp::STYLE_RELATIVE_TIME); |
| 38 | + |
| 39 | + $this->assertSame(1700000000, $timestamp->timestamp); |
| 40 | + $this->assertSame(Timestamp::STYLE_RELATIVE_TIME, $timestamp->format); |
| 41 | + $this->assertSame('<t:1700000000:R>', (string) $timestamp); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @covers \Discord\Helpers\Timestamp::setTimestamp |
| 46 | + */ |
| 47 | + public function testNormalizesDateTimeTimestamp(): void |
| 48 | + { |
| 49 | + $timestamp = new Timestamp(new DateTimeImmutable('@1700000000')); |
| 50 | + |
| 51 | + $this->assertSame(1700000000, $timestamp->timestamp); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @covers \Discord\Helpers\Timestamp::__set |
| 56 | + * @covers \Discord\Helpers\Timestamp::setTimestamp |
| 57 | + * @covers \Discord\Helpers\Timestamp::setFormat |
| 58 | + */ |
| 59 | + public function testMagicPropertiesCanBeUpdated(): void |
| 60 | + { |
| 61 | + $timestamp = new Timestamp(1700000000); |
| 62 | + |
| 63 | + $timestamp->timestamp = '1700000001'; |
| 64 | + $timestamp->format = Timestamp::STYLE_LONG_DATE_TIME; |
| 65 | + |
| 66 | + $this->assertSame(1700000001, $timestamp->timestamp); |
| 67 | + $this->assertSame(Timestamp::STYLE_LONG_DATE_TIME, $timestamp->format); |
| 68 | + $this->assertSame('<t:1700000001:F>', (string) $timestamp); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @covers \Discord\Helpers\Timestamp::setTimestamp |
| 73 | + */ |
| 74 | + public function testRejectsInvalidTimestamp(): void |
| 75 | + { |
| 76 | + $this->expectException(InvalidArgumentException::class); |
| 77 | + |
| 78 | + new Timestamp('not-a-timestamp'); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @covers \Discord\Helpers\Timestamp::setFormat |
| 83 | + */ |
| 84 | + public function testRejectsInvalidFormat(): void |
| 85 | + { |
| 86 | + $this->expectException(InvalidArgumentException::class); |
| 87 | + |
| 88 | + new Timestamp(1700000000, 'invalid'); |
| 89 | + } |
| 90 | +} |
0 commit comments