File: BigIntTypeTest.php

package info (click to toggle)
php-doctrine-dbal 4.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,092 kB
  • sloc: php: 60,293; xml: 618; makefile: 23
file content (117 lines) | stat: -rw-r--r-- 4,077 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Types;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Types;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;

use const PHP_INT_MAX;
use const PHP_INT_MIN;
use const PHP_INT_SIZE;

class BigIntTypeTest extends FunctionalTestCase
{
    #[DataProvider('provideBigIntLiterals')]
    public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedValue): void
    {
        $table = Table::editor()
            ->setUnquotedName('bigint_type_test')
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('id')
                    ->setTypeName(Types::SMALLINT)
                    ->create(),
                Column::editor()
                    ->setUnquotedName('my_integer')
                    ->setTypeName(Types::BIGINT)
                    ->setNotNull(false)
                    ->create(),
            )
            ->setPrimaryKeyConstraint(
                PrimaryKeyConstraint::editor()
                    ->setUnquotedColumnNames('id')
                    ->create(),
            )
            ->create();

        $this->dropAndCreateTable($table);

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

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

    /** @return Generator<string, array{string, int|string|null}> */
    public static function provideBigIntLiterals(): Generator
    {
        yield 'zero' => ['0', 0];
        yield 'null' => ['null', null];
        yield 'positive number' => ['42', 42];
        yield 'negative number' => ['-42', -42];
        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];
    }

    public function testUnsignedBigIntOnMySQL(): void
    {
        if (! TestUtil::isDriverOneOf('mysqli', 'pdo_mysql')) {
            self::markTestSkipped('This test only works on MySQL/MariaDB.');
        }

        $table = Table::editor()
            ->setUnquotedName('bigint_type_test')
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('id')
                    ->setTypeName(Types::SMALLINT)
                    ->create(),
                Column::editor()
                    ->setUnquotedName('my_integer')
                    ->setTypeName(Types::BIGINT)
                    ->setNotNull(false)
                    ->setUnsigned(true)
                    ->create(),
            )
            ->setPrimaryKeyConstraint(
                PrimaryKeyConstraint::editor()
                    ->setUnquotedColumnNames('id')
                    ->create(),
            )
            ->create();

        $this->dropAndCreateTable($table);

        // Insert (2 ** 64) - 1
        $this->connection->executeStatement(<<<'SQL'
            INSERT INTO bigint_type_test (id, my_integer)
            VALUES (42, 0xFFFFFFFFFFFFFFFF)
            SQL);

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