Skip to content

Commit

Permalink
Support int64 in appendFieldValue
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed Feb 18, 2025
1 parent a168f09 commit 6162894
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Protocol/ProtocolWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,13 @@ public function appendFieldValue(mixed $value, Buffer $buffer): void
$buffer->append($value);

} elseif (is_int($value)) {
$buffer->appendUint8(Constants::FIELD_LONG_INT);
$buffer->appendInt32($value);
if ($value >= -2_147_483_648 && $value <= 2_147_483_647) {
$buffer->appendUint8(Constants::FIELD_LONG_INT);
$buffer->appendInt32($value);
} else {
$buffer->appendUint8(Constants::FIELD_LONG_LONG_INT);
$buffer->appendInt64($value);
}

} elseif (is_bool($value)) {
$buffer->appendUint8(Constants::FIELD_BOOLEAN);
Expand Down
34 changes: 34 additions & 0 deletions test/Protocol/ProtocolWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,38 @@ public function test_appendFieldValue_canHandleDateTimeImmutable()

$protocolWriter->appendFieldValue($date, $buffer);
}

public function test_appendFieldValue_canHandleInt32()
{
$buffer = $this->createMock(Buffer::class);
$protocolWriter = new ProtocolWriter();

$int = 42;

$buffer->expects($this->once())
->method('appendUint8')
->with(Constants::FIELD_LONG_INT);
$buffer->expects($this->once())
->method('appendInt32')
->with($int);

$protocolWriter->appendFieldValue($int, $buffer);
}

public function test_appendFieldValue_canHandleInt64()
{
$buffer = $this->createMock(Buffer::class);
$protocolWriter = new ProtocolWriter();

$int = 2_157_483_647;

$buffer->expects($this->once())
->method('appendUint8')
->with(Constants::FIELD_LONG_LONG_INT);
$buffer->expects($this->once())
->method('appendInt64')
->with($int);

$protocolWriter->appendFieldValue($int, $buffer);
}
}

0 comments on commit 6162894

Please sign in to comment.