Failed Conditions
Pull Request — develop (#3582)
by Jonathan
69:13 queued 04:11
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
    /** @var bool */
15
    private $tempTableCreated = false;
16
17
    protected function tearDown() : void
18
    {
19
        if ($this->connection->getSchemaManager()->tableExists('nontemporary')) {
20
            $dropTableSql = $this->connection
21
                ->getDatabasePlatform()
22
                ->getDropTableSQL('nontemporary');
23
24
            $this->connection->exec($dropTableSql);
25
        }
26
27
        $tempTable = $this->connection->getDatabasePlatform()->getTemporaryTableName('my_temporary');
28
29
        if ($this->connection->getSchemaManager()->tableExists($tempTable)) {
30
            $dropTempTableSql = $this->connection
31
                ->getDatabasePlatform()
32
                ->getDropTemporaryTableSQL($tempTable);
33
34
            $this->connection->exec($dropTempTableSql);
35
        }
36
37
        parent::tearDown();
38
    }
39
40
    /**
41
     * @group DDC-1337
42
     */
43
    public function testDropTemporaryTableNotAutoCommitTransaction() : void
44
    {
45
        if ($this->connection->getDatabasePlatform()->getName() === 'sqlanywhere' ||
46
            $this->connection->getDatabasePlatform()->getName() === 'oracle') {
47
            $this->markTestSkipped('Test does not work on Oracle and SQL Anywhere.');
48
        }
49
50
        $platform          = $this->connection->getDatabasePlatform();
51
        $columnDefinitions = ['id' => ['type' => Type::getType('integer'), 'notnull' => true]];
52
        $tempTable         = $platform->getTemporaryTableName('my_temporary');
53
54
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
55
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
56
        $this->connection->executeUpdate($createTempTableSQL);
57
58
        $table = new Table('nontemporary');
59
        $table->addColumn('id', 'integer');
60
        $table->setPrimaryKey(['id']);
61
62
        $this->connection->getSchemaManager()->createTable($table);
63
64
        $this->connection->beginTransaction();
65
        $this->connection->insert('nontemporary', ['id' => 1]);
66
        $this->connection->exec($platform->getDropTemporaryTableSQL($tempTable));
67
        $this->connection->insert('nontemporary', ['id' => 2]);
68
69
        $this->connection->rollBack();
70
71
        $rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
72
        self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
73
    }
74
75
    /**
76
     * @group DDC-1337
77
     */
78
    public function testCreateTemporaryTableNotAutoCommitTransaction() : void
79
    {
80
        if ($this->connection->getDatabasePlatform()->getName() === 'sqlanywhere' ||
81
            $this->connection->getDatabasePlatform()->getName() === 'oracle') {
82
            $this->markTestSkipped('Test does not work on Oracle and SQL Anywhere.');
83
        }
84
85
        $platform          = $this->connection->getDatabasePlatform();
86
        $columnDefinitions = ['id' => ['type' => Type::getType('integer'), 'notnull' => true]];
87
        $tempTable         = $platform->getTemporaryTableName('my_temporary');
88
89
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
90
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
91
92
        $table = new Table('nontemporary');
93
        $table->addColumn('id', 'integer');
94
        $table->setPrimaryKey(['id']);
95
96
        $this->connection->getSchemaManager()->createTable($table);
97
98
        $this->connection->beginTransaction();
99
        $this->connection->insert('nontemporary', ['id' => 1]);
100
101
        $this->connection->exec($createTempTableSQL);
102
        $this->tempTableCreated = true;
103
104
        $this->connection->insert('nontemporary', ['id' => 2]);
105
106
        $this->connection->rollBack();
107
108
        try {
109
            $this->connection->exec($platform->getDropTemporaryTableSQL($tempTable));
110
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
111
        }
112
113
        $rows = $this->connection->fetchAll('SELECT * FROM nontemporary');
114
        self::assertEquals([], $rows, 'In an event of an error this result has one row, because of an implicit commit.');
115
    }
116
}
117