|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace unit\src; |
| 4 | + |
| 5 | +use G4\ValueObject\Exception\MissingTimestampValueException; |
| 6 | +use G4\ValueObject\Exception\InvalidTimestampValueException; |
| 7 | +use G4\ValueObject\TimestampValue; |
| 8 | +use G4\ValueObject\TimestampValueMilliSeconds; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class TimestampValueMillisecondsTest extends TestCase |
| 12 | +{ |
| 13 | + public function testMissingValueException() |
| 14 | + { |
| 15 | + $this->expectException(MissingTimestampValueException::class); |
| 16 | + new TimestampValueMilliSeconds(''); |
| 17 | + } |
| 18 | + |
| 19 | + public function testInvalidValueFloatException() |
| 20 | + { |
| 21 | + $this->expectException(InvalidTimestampValueException::class); |
| 22 | + new TimestampValueMilliSeconds('1.0'); |
| 23 | + } |
| 24 | + |
| 25 | + public function testInvalidValueHexaNumberException() |
| 26 | + { |
| 27 | + $this->expectException(InvalidTimestampValueException::class); |
| 28 | + new TimestampValueMilliSeconds('0xFF'); |
| 29 | + } |
| 30 | + |
| 31 | + public function testValidValues() |
| 32 | + { |
| 33 | + $now = (int) (microtime(true) * 1000); |
| 34 | + $timestampInt = new TimestampValueMilliSeconds($now); |
| 35 | + $this->assertEquals($now, $timestampInt->getValue()); |
| 36 | + |
| 37 | + $timestampString = new TimestampValueMilliSeconds('1748436857881'); |
| 38 | + $this->assertEquals('1748436857881', (string) $timestampString); |
| 39 | + } |
| 40 | + |
| 41 | + public function testGetFormatted() |
| 42 | + { |
| 43 | + $timestamp = new TimestampValueMilliSeconds('1748436857881'); |
| 44 | + $this->assertEquals('2025-05-28 14:54:17.881', $timestamp->getFormatted()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testGetSeconds() |
| 48 | + { |
| 49 | + $timestamp = new TimestampValueMilliSeconds(1748436857881); |
| 50 | + $this->assertEquals(1748436857, $timestamp->getSeconds()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testNow() |
| 54 | + { |
| 55 | + $time = time(); |
| 56 | + $timestamp = TimestampValueMilliSeconds::now(); |
| 57 | + $this->assertEquals($time, $timestamp->getSeconds()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testInvalidValueLengthLower() |
| 61 | + { |
| 62 | + $this->expectException(InvalidTimestampValueException::class); |
| 63 | + new TimestampValueMilliSeconds(123456789); |
| 64 | + } |
| 65 | + |
| 66 | + public function testInvalidValueLengthHigher() |
| 67 | + { |
| 68 | + $this->expectException(InvalidTimestampValueException::class); |
| 69 | + new TimestampValueMilliSeconds(12345678912345); |
| 70 | + } |
| 71 | + |
| 72 | + public function testFromTimestampValue() |
| 73 | + { |
| 74 | + $timestamp = TimestampValueMilliSeconds::fromTimestampValue(new TimestampValue(1748436857)); |
| 75 | + $this->assertEquals(1748436857000, $timestamp->getValue()); |
| 76 | + } |
| 77 | +} |
0 commit comments