File: AutoIncrementColumnTest.php

package info (click to toggle)
php-doctrine-dbal 4.2.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,644 kB
  • sloc: php: 46,471; xml: 460; makefile: 22
file content (84 lines) | stat: -rw-r--r-- 2,731 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
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\RequiresPhpunit;

class AutoIncrementColumnTest extends FunctionalTestCase
{
    private bool $shouldDisableIdentityInsert = false;

    /** @throws Exception */
    protected function setUp(): void
    {
        $table = new Table('auto_increment_table');
        $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
        $table->addColumn('val', Types::INTEGER);
        $table->setPrimaryKey(['id']);

        $this->dropAndCreateTable($table);
    }

    /** @throws Exception */
    protected function tearDown(): void
    {
        if (! $this->shouldDisableIdentityInsert) {
            return;
        }

        $this->setIdentityInsert('OFF');
    }

    /** @throws Exception */
    #[RequiresPhpunit('< 11.5.18')]
    public function testInsertAutoGeneratesValue(): void
    {
        $this->connection->insert('auto_increment_table', ['val' => 0]);
        self::assertEquals(1, $this->connection->fetchOne('SELECT MAX(id) FROM auto_increment_table'));
    }

    /** @throws Exception */
    #[RequiresPhpunit('< 11.5.18')]
    public function testInsertIdentityValue(): void
    {
        $platform    = $this->connection->getDatabasePlatform();
        $isSQLServer = $platform instanceof SQLServerPlatform;

        if ($isSQLServer) {
            $this->setIdentityInsert('ON');
            $this->shouldDisableIdentityInsert = true;
        }

        $this->connection->insert('auto_increment_table', ['id' => 2, 'val' => 0]);
        self::assertEquals(2, $this->connection->fetchOne('SELECT MAX(id) FROM auto_increment_table'));

        if ($isSQLServer) {
            $this->setIdentityInsert('OFF');
            $this->shouldDisableIdentityInsert = false;
        }

        // using an explicit value for an autoincrement column does not affect the next value
        // on the following platforms
        if ($platform instanceof PostgreSqlPlatform || $platform instanceof DB2Platform) {
            return;
        }

        $this->connection->insert('auto_increment_table', ['val' => 0]);
        self::assertEquals(3, $this->connection->fetchOne('SELECT MAX(id) FROM auto_increment_table'));
    }

    /** @throws Exception */
    private function setIdentityInsert(string $value): void
    {
        $this->connection->executeStatement('SET IDENTITY_INSERT auto_increment_table ' . $value);
    }
}