Failed Conditions
Pull Request — develop (#3582)
by Jonathan
62:35
created

TemporaryTableTest::tearDown()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
nc 4
nop 0
cc 3
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 = [
49
            [
50
                'name' => 'id',
51
                'type' => Type::getType('integer'),
52
                'notnull' => true,
53
            ],
54
        ];
55
56
        $tempTable = $platform->getTemporaryTableName('my_temporary');
57
58
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
59
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
60
        $this->connection->executeUpdate($createTempTableSQL);
61
62
        $table = new Table('nontemporary');
63
        $table->addColumn('id', 'integer');
64
        $table->setPrimaryKey(['id']);
65
66
        $this->connection->getSchemaManager()->createTable($table);
67
68
        $this->connection->beginTransaction();
69
        $this->connection->insert('nontemporary', ['id' => 1]);
70
        $this->connection->exec($platform->getDropTemporaryTableSQL($tempTable));
71
        $this->connection->insert('nontemporary', ['id' => 2]);
72
73
        $this->connection->rollBack();
74
75
        $rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
76
        self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
77
    }
78
79
    /**
80
     * @group DDC-1337
81
     */
82
    public function testCreateTemporaryTableNotAutoCommitTransaction() : void
83
    {
84
        if ($this->connection->getDatabasePlatform()->getName() === 'sqlanywhere' ||
85
            $this->connection->getDatabasePlatform()->getName() === 'oracle') {
86
            $this->markTestSkipped('Test does not work on Oracle and SQL Anywhere.');
87
        }
88
89
        $platform          = $this->connection->getDatabasePlatform();
90
        $columnDefinitions = [
91
            [
92
                'name' => 'id',
93
                'type' => Type::getType('integer'),
94
                'notnull' => true,
95
            ],
96
        ];
97
98
        $tempTable = $platform->getTemporaryTableName('my_temporary');
99
100
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
101
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
102
103
        $table = new Table('nontemporary');
104
        $table->addColumn('id', 'integer');
105
        $table->setPrimaryKey(['id']);
106
107
        $this->connection->getSchemaManager()->createTable($table);
108
109
        $this->connection->beginTransaction();
110
        $this->connection->insert('nontemporary', ['id' => 1]);
111
112
        $this->connection->exec($createTempTableSQL);
113
114
        $this->connection->insert('nontemporary', ['id' => 2]);
115
116
        $this->connection->rollBack();
117
118
        try {
119
            $this->connection->exec($platform->getDropTemporaryTableSQL($tempTable));
120
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
121
        }
122
123
        $rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
124
        self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
125
    }
126
}
127