|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Tests\Functional\Types; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Schema\Table; |
| 8 | +use Doctrine\DBAL\Tests\FunctionalTestCase; |
| 9 | +use Doctrine\DBAL\Tests\TestUtil; |
| 10 | +use Doctrine\DBAL\Types\Types; |
| 11 | +use Generator; |
| 12 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 13 | +use PHPUnit\Framework\Constraint\IsIdentical; |
| 14 | +use PHPUnit\Framework\Constraint\LogicalOr; |
| 15 | + |
| 16 | +use const PHP_INT_MAX; |
| 17 | +use const PHP_INT_MIN; |
| 18 | +use const PHP_INT_SIZE; |
| 19 | + |
| 20 | +class BigIntTypeTest extends FunctionalTestCase |
| 21 | +{ |
| 22 | + #[DataProvider('provideBigIntLiterals')] |
| 23 | + public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedValue): void |
| 24 | + { |
| 25 | + $table = new Table('bigint_type_test'); |
| 26 | + $table->addColumn('id', Types::SMALLINT, ['notnull' => true]); |
| 27 | + $table->addColumn('my_integer', Types::BIGINT, ['notnull' => false]); |
| 28 | + $table->setPrimaryKey(['id']); |
| 29 | + $this->dropAndCreateTable($table); |
| 30 | + |
| 31 | + $this->connection->executeStatement(<<<SQL |
| 32 | + INSERT INTO bigint_type_test (id, my_integer) |
| 33 | + VALUES (42, $sqlLiteral) |
| 34 | + SQL); |
| 35 | + |
| 36 | + self::assertSame( |
| 37 | + $expectedValue, |
| 38 | + $this->connection->convertToPHPValue( |
| 39 | + $this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'), |
| 40 | + Types::BIGINT, |
| 41 | + ), |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** @return Generator<string, array{string, int|string|null}> */ |
| 46 | + public static function provideBigIntLiterals(): Generator |
| 47 | + { |
| 48 | + yield 'zero' => ['0', 0]; |
| 49 | + yield 'null' => ['null', null]; |
| 50 | + yield 'positive number' => ['42', 42]; |
| 51 | + yield 'negative number' => ['-42', -42]; |
| 52 | + |
| 53 | + if (PHP_INT_SIZE < 8) { |
| 54 | + // The following tests only work on 64bit systems. |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + yield 'large positive number' => ['9223372036854775806', PHP_INT_MAX - 1]; |
| 59 | + yield 'large negative number' => ['-9223372036854775807', PHP_INT_MIN + 1]; |
| 60 | + } |
| 61 | + |
| 62 | + #[DataProvider('provideBigIntEdgeLiterals')] |
| 63 | + public function testSelectBigIntEdge(int $value): void |
| 64 | + { |
| 65 | + $table = new Table('bigint_type_test'); |
| 66 | + $table->addColumn('id', Types::SMALLINT, ['notnull' => true]); |
| 67 | + $table->addColumn('my_integer', Types::BIGINT, ['notnull' => false]); |
| 68 | + $table->setPrimaryKey(['id']); |
| 69 | + $this->dropAndCreateTable($table); |
| 70 | + |
| 71 | + $this->connection->executeStatement(<<<SQL |
| 72 | + INSERT INTO bigint_type_test (id, my_integer) |
| 73 | + VALUES (42, $value) |
| 74 | + SQL); |
| 75 | + |
| 76 | + self::assertThat( |
| 77 | + $this->connection->convertToPHPValue( |
| 78 | + $this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'), |
| 79 | + Types::BIGINT, |
| 80 | + ), |
| 81 | + LogicalOr::fromConstraints(new IsIdentical($value), new IsIdentical((string) $value)), |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + /** @return Generator<string, array{int}> */ |
| 86 | + public static function provideBigIntEdgeLiterals(): Generator |
| 87 | + { |
| 88 | + yield 'max int' => [PHP_INT_MAX]; |
| 89 | + yield 'min int' => [PHP_INT_MIN]; |
| 90 | + } |
| 91 | + |
| 92 | + public function testUnsignedBigIntOnMySQL(): void |
| 93 | + { |
| 94 | + if (! TestUtil::isDriverOneOf('mysqli', 'pdo_mysql')) { |
| 95 | + self::markTestSkipped('This test only works on MySQL/MariaDB.'); |
| 96 | + } |
| 97 | + |
| 98 | + $table = new Table('bigint_type_test'); |
| 99 | + $table->addColumn('id', Types::SMALLINT, ['notnull' => true]); |
| 100 | + $table->addColumn('my_integer', Types::BIGINT, ['notnull' => false, 'unsigned' => true]); |
| 101 | + $table->setPrimaryKey(['id']); |
| 102 | + $this->dropAndCreateTable($table); |
| 103 | + |
| 104 | + // Insert (2 ** 64) - 1 |
| 105 | + $this->connection->executeStatement(<<<'SQL' |
| 106 | + INSERT INTO bigint_type_test (id, my_integer) |
| 107 | + VALUES (42, 0xFFFFFFFFFFFFFFFF) |
| 108 | + SQL); |
| 109 | + |
| 110 | + self::assertSame( |
| 111 | + '18446744073709551615', |
| 112 | + $this->connection->convertToPHPValue( |
| 113 | + $this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'), |
| 114 | + Types::BIGINT, |
| 115 | + ), |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments