diff --git a/src/Behavior/MillisecondTimestamp.php b/src/Behavior/MillisecondTimestamp.php index 76b818b..f23a89c 100644 --- a/src/Behavior/MillisecondTimestamp.php +++ b/src/Behavior/MillisecondTimestamp.php @@ -12,8 +12,8 @@ class MillisecondTimestamp extends PropertyBehavior { public function fromDb($value, $key, $context) { - if ($value === null) { - return null; + if ($value === null || $value instanceof DateTime) { + return $value; } $datetime = DateTime::createFromFormat('U.u', sprintf('%F', $value / 1000.0)); diff --git a/src/Behavior/UUID.php b/src/Behavior/UUID.php index 85072e1..3bc93c9 100644 --- a/src/Behavior/UUID.php +++ b/src/Behavior/UUID.php @@ -20,8 +20,8 @@ class UUID extends PropertyBehavior implements QueryAwareBehavior public function fromDb($value, $key, $_) { - if ($value === null) { - return null; + if ($value === null || $value instanceof UuidInterface) { + return $value; } if (! is_string($value)) { diff --git a/tests/Behavior/UUIDTest.php b/tests/Behavior/UUIDTest.php index 39af436..a614499 100644 --- a/tests/Behavior/UUIDTest.php +++ b/tests/Behavior/UUIDTest.php @@ -38,6 +38,22 @@ public function testRetrievePropertyReturnsUuidInstance(): void ); } + public function testRetrievePropertyCanBeAppliedRepeatedly(): void + { + $behavior = $this->behavior(); + $uuid = $behavior->retrieveProperty( + RamseyUuid::fromString(static::TEST_UUID_VALUE)->getBytes(), + static::TEST_COLUMN + ); + + $this->assertSame($uuid, $behavior->retrieveProperty($uuid, static::TEST_COLUMN)); + + $behavior = $this->behavior(true); + $uuid = $behavior->retrieveProperty(static::TEST_UUID_VALUE, static::TEST_COLUMN); + + $this->assertSame($uuid, $behavior->retrieveProperty($uuid, static::TEST_COLUMN)); + } + public function testPersistPropertyReturnsNullWhenValueIsNull(): void { $this->assertNull($this->behavior()->persistProperty(null, static::TEST_COLUMN)); diff --git a/tests/MillisecondTimestampBehaviorTest.php b/tests/MillisecondTimestampBehaviorTest.php index a1045e4..07d71dd 100644 --- a/tests/MillisecondTimestampBehaviorTest.php +++ b/tests/MillisecondTimestampBehaviorTest.php @@ -15,6 +15,14 @@ public function testFromDbReturnsNullWhenNullIsPassed() $this->assertNull((new MillisecondTimestamp([]))->fromDb(null, 'key', null)); } + public function testFromDbCanBeAppliedRepeatedly() + { + $behavior = new MillisecondTimestamp([]); + $sometime = $behavior->fromDb(1668002400123, 'key', null); + + $this->assertSame($sometime, $behavior->fromDb($sometime, 'key', null)); + } + public function testToDbReturnsNullWhenNullIsPassed() { $this->assertNull((new MillisecondTimestamp([]))->toDb(null, 'key', null));