Skip to content

Commit

Permalink
Fix bigint PHP_INT_MIN/PHP_INT_MAX string to int convert
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 28, 2024
1 parent 0b77350 commit 5b74171
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 45 deletions.
5 changes: 4 additions & 1 deletion src/Types/BigIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): int
return $value;
}

if ($value > PHP_INT_MIN && $value < PHP_INT_MAX) {
if (
($value > PHP_INT_MIN && $value < PHP_INT_MAX)
|| $value === (string) (int) $value
) {
return (int) $value;
}

Expand Down
62 changes: 18 additions & 44 deletions tests/Functional/Types/BigIntTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Doctrine\DBAL\Types\Types;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Constraint\IsIdentical;
use PHPUnit\Framework\Constraint\LogicalOr;

use const PHP_INT_MAX;
use const PHP_INT_MIN;
Expand All @@ -30,13 +28,13 @@ public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedVa

$this->connection->executeStatement(<<<SQL
INSERT INTO bigint_type_test (id, my_integer)
VALUES (42, $sqlLiteral)
VALUES (1, $sqlLiteral)
SQL);

self::assertSame(
$expectedValue,
$this->connection->convertToPHPValue(
$this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'),
$this->connection->fetchOne('SELECT my_integer from bigint_type_test'),
Types::BIGINT,
),
);
Expand All @@ -46,47 +44,23 @@ public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedVa
public static function provideBigIntLiterals(): Generator
{
yield 'zero' => ['0', 0];
yield 'minus zero' => ['-0', 0];
yield 'plus zero' => ['+0', 0];
yield 'null' => ['null', null];
yield 'positive number' => ['42', 42];
yield 'negative number' => ['-42', -42];

if (PHP_INT_SIZE < 8) {
// The following tests only work on 64bit systems.
return;
}

yield 'large positive number' => ['9223372036854775806', PHP_INT_MAX - 1];
yield 'large negative number' => ['-9223372036854775807', PHP_INT_MIN + 1];
}

#[DataProvider('provideBigIntEdgeLiterals')]
public function testSelectBigIntEdge(int $value): void
{
$table = new Table('bigint_type_test');
$table->addColumn('id', Types::SMALLINT, ['notnull' => true]);
$table->addColumn('my_integer', Types::BIGINT, ['notnull' => false]);
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);

$this->connection->executeStatement(<<<SQL
INSERT INTO bigint_type_test (id, my_integer)
VALUES (42, $value)
SQL);

self::assertThat(
$this->connection->convertToPHPValue(
$this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'),
Types::BIGINT,
),
LogicalOr::fromConstraints(new IsIdentical($value), new IsIdentical((string) $value)),
);
}

/** @return Generator<string, array{int}> */
public static function provideBigIntEdgeLiterals(): Generator
{
yield 'max int' => [PHP_INT_MAX];
yield 'min int' => [PHP_INT_MIN];
yield 'large positive number' => [PHP_INT_SIZE === 4 ? '2147483646' : '9223372036854775806', PHP_INT_MAX - 1];
yield 'large negative number' => [PHP_INT_SIZE === 4 ? '-2147483647' : '-9223372036854775807', PHP_INT_MIN + 1];
yield 'largest positive number' => [PHP_INT_SIZE === 4 ? '2147483647' : '9223372036854775807', PHP_INT_MAX];
yield 'largest negative number' => [PHP_INT_SIZE === 4 ? '-2147483648' : '-9223372036854775808', PHP_INT_MIN];
yield 'too large positive number' => [

Check failure on line 56 in tests/Functional/Types/BigIntTypeTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 line after "yield", found 0.
PHP_INT_SIZE === 4 ? '2147483648' : '9223372036854775808',
PHP_INT_SIZE === 4 ? '2147483648' : '9223372036854775808',
];
yield 'too large negative number' => [

Check failure on line 60 in tests/Functional/Types/BigIntTypeTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 line before "yield", found 0.
PHP_INT_SIZE === 4 ? '-2147483649' : '-9223372036854775809',
PHP_INT_SIZE === 4 ? '-2147483649' : '-9223372036854775809',
];
}

public function testUnsignedBigIntOnMySQL(): void
Expand All @@ -104,13 +78,13 @@ public function testUnsignedBigIntOnMySQL(): void
// Insert (2 ** 64) - 1
$this->connection->executeStatement(<<<'SQL'
INSERT INTO bigint_type_test (id, my_integer)
VALUES (42, 0xFFFFFFFFFFFFFFFF)
VALUES (1, 0xFFFFFFFFFFFFFFFF)
SQL);

self::assertSame(
'18446744073709551615',
$this->connection->convertToPHPValue(
$this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'),
$this->connection->fetchOne('SELECT my_integer from bigint_type_test'),
Types::BIGINT,
),
);
Expand Down

0 comments on commit 5b74171

Please sign in to comment.