Failed Conditions
Pull Request — develop (#3582)
by Jonathan
63:41
created

TemporaryTableTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional;
6
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\Tests\DbalFunctionalTestCase;
10
use Throwable;
11
12
class TemporaryTableTest extends DbalFunctionalTestCase
13
{
14
    protected function tearDown() : void
15
    {
16
        if ($this->connection->getSchemaManager()->tableExists('nontemporary')) {
17
            $dropTableSql = $this->connection
18
                ->getDatabasePlatform()
19
                ->getDropTableSQL('nontemporary');
20
21
            $this->connection->exec($dropTableSql);
22
        }
23
24
        $tempTable = $this->connection->getDatabasePlatform()->getTemporaryTableName('my_temporary');
25
26
        if ($this->connection->getSchemaManager()->tableExists($tempTable)) {
27
            $dropTempTableSql = $this->connection
28
                ->getDatabasePlatform()
29
                ->getDropTemporaryTableSQL($tempTable);
30
31
            $this->connection->exec($dropTempTableSql);
32
        }
33
34
        parent::tearDown();
35
    }
36
37
    /**
38
     * @group DDC-1337
39
     */
40
    public function testDropTemporaryTableNotAutoCommitTransaction() : void
41
    {
42
        if ($this->connection->getDatabasePlatform()->getName() === 'sqlanywhere' ||
43
            $this->connection->getDatabasePlatform()->getName() === 'oracle') {
44
            $this->markTestSkipped('Test does not work on Oracle and SQL Anywhere.');
45
        }
46
47
        $platform          = $this->connection->getDatabasePlatform();
48
        $columnDefinitions = ['id' => ['type' => Type::getType('integer'), 'notnull' => true]];
49
        $tempTable         = $platform->getTemporaryTableName('my_temporary');
50
51
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
52
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
53
        $this->connection->executeUpdate($createTempTableSQL);
54
55
        $table = new Table('nontemporary');
56
        $table->addColumn('id', 'integer');
57
        $table->setPrimaryKey(['id']);
58
59
        $this->connection->getSchemaManager()->createTable($table);
60
61
        $this->connection->beginTransaction();
62
        $this->connection->insert('nontemporary', ['id' => 1]);
63
        $this->connection->exec($platform->getDropTemporaryTableSQL($tempTable));
64
        $this->connection->insert('nontemporary', ['id' => 2]);
65
66
        $this->connection->rollBack();
67
68
        $rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
69
        self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
70
    }
71
72
    /**
73
     * @group DDC-1337
74
     */
75
    public function testCreateTemporaryTableNotAutoCommitTransaction() : void
76
    {
77
        if ($this->connection->getDatabasePlatform()->getName() === 'sqlanywhere' ||
78
            $this->connection->getDatabasePlatform()->getName() === 'oracle') {
79
            $this->markTestSkipped('Test does not work on Oracle and SQL Anywhere.');
80
        }
81
82
        $platform          = $this->connection->getDatabasePlatform();
83
        $columnDefinitions = ['id' => ['type' => Type::getType('integer'), 'notnull' => true]];
84
        $tempTable         = $platform->getTemporaryTableName('my_temporary');
85
86
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
87
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
88
89
        $table = new Table('nontemporary');
90
        $table->addColumn('id', 'integer');
91
        $table->setPrimaryKey(['id']);
92
93
        $this->connection->getSchemaManager()->createTable($table);
94
95
        $this->connection->beginTransaction();
96
        $this->connection->insert('nontemporary', ['id' => 1]);
97
98
        $this->connection->exec($createTempTableSQL);
99
100
        $this->connection->insert('nontemporary', ['id' => 2]);
101
102
        $this->connection->rollBack();
103
104
        try {
105
            $this->connection->exec($platform->getDropTemporaryTableSQL($tempTable));
106
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
107
        }
108
109
        $rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
110
        self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
111
    }
112
}
113