File: AutoIncrementColumnTest.php

package info (click to toggle)
php-doctrine-dbal 4.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,088 kB
  • sloc: php: 60,318; xml: 618; makefile: 23
file content (98 lines) | stat: -rw-r--r-- 3,170 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
<?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\Column;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;

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

    /** @throws Exception */
    protected function setUp(): void
    {
        $table = Table::editor()
            ->setUnquotedName('auto_increment_table')
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('id')
                    ->setTypeName(Types::INTEGER)
                    ->setAutoincrement(true)
                    ->create(),
                Column::editor()
                    ->setUnquotedName('val')
                    ->setTypeName(Types::INTEGER)
                    ->create(),
            )
            ->setPrimaryKeyConstraint(
                PrimaryKeyConstraint::editor()
                    ->setUnquotedColumnNames('id')
                    ->create(),
            )
            ->create();

        $this->dropAndCreateTable($table);
    }

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

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

    /** @throws Exception */
    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 */
    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);
    }
}