1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
<?php
declare(strict_types=1);
namespace Zhineng\Snowflake\Tests;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Zhineng\Snowflake\Timestamp;
#[CoversClass(Timestamp::class)]
final class TimestampTest extends TestCase
{
public function testTimestampCanBeInitialized(): void
{
$field = new Timestamp('timestamp', 41);
$this->assertSame('timestamp', $field->name);
$this->assertSame(41, $field->bits);
}
public function testTimestampHasDefaultParameterValues(): void
{
$field = new Timestamp;
$this->assertSame('timestamp', $field->name);
$this->assertSame(41, $field->bits);
}
public function testTimestampHasMakeFactoryMethod(): void
{
$field = Timestamp::make('timestamp', 41);
$this->assertSame('timestamp', $field->name);
$this->assertSame(41, $field->bits);
}
public function testTimestampHasDynamicValue(): void
{
$field = new Timestamp;
$value1 = $field->value();
usleep(1000); // Sleep for 1 millisecond
$value2 = $field->value();
$this->assertGreaterThan($value1, $value2);
}
public function testEpochCanBeCustomized(): void
{
$epoch = new \DateTime('2026-01-01 00:00:00');
$field = new Timestamp('timestamp', 41, $epoch);
$now = (int) floor(microtime(as_float: true) * 1000);
$this->assertSame($now - $epoch->getTimestamp() * 1000, $field->value());
}
public function testEpochCanBeSetAsInteger(): void
{
$epoch = new \DateTime('2026-01-01 00:00:00');
$timestampInMillis = $epoch->getTimestamp() * 1000;
$field = new Timestamp('timestamp', 41, $timestampInMillis);
$now = (int) floor(microtime(as_float: true) * 1000);
$this->assertSame($now - $timestampInMillis, $field->value());
}
public function testEpochMustBeNonNegative(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Epoch must be non-negative.');
new Timestamp('timestamp', 41, -1);
}
}
|