Completed
Push — master ( f37a88...f7f156 )
by Sergei
92:58 queued 31:02
created

testForeignKeyConstraintViolationExceptionOnTruncate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional;
6
7
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
8
use Doctrine\DBAL\Exception;
9
use Doctrine\DBAL\Platforms\SqlitePlatform;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\Tests\DbalFunctionalTestCase;
12
13
class ForeignKeyExceptionTest extends DbalFunctionalTestCase
14
{
15
    protected function setUp() : void
16
    {
17
        parent::setUp();
18
19
        if (! $this->connection->getDriver() instanceof ExceptionConverterDriver) {
20
            $this->markTestSkipped('Driver does not support special exception handling.');
21
        }
22
23
        $schemaManager = $this->connection->getSchemaManager();
24
25
        $table = new Table('constraint_error_table');
26
        $table->addColumn('id', 'integer', []);
27
        $table->setPrimaryKey(['id']);
28
29
        $owningTable = new Table('owning_table');
30
        $owningTable->addColumn('id', 'integer', []);
31
        $owningTable->addColumn('constraint_id', 'integer', []);
32
        $owningTable->setPrimaryKey(['id']);
33
        $owningTable->addForeignKeyConstraint($table, ['constraint_id'], ['id']);
34
35
        $schemaManager->createTable($table);
36
        $schemaManager->createTable($owningTable);
37
    }
38
39
    protected function tearDown() : void
40
    {
41
        parent::tearDown();
42
43
        $schemaManager = $this->connection->getSchemaManager();
44
45
        $schemaManager->dropTable('owning_table');
46
        $schemaManager->dropTable('constraint_error_table');
47
    }
48
49
    public function testForeignKeyConstraintViolationExceptionOnInsert() : void
50
    {
51
        $platform = $this->connection->getDatabasePlatform();
52
53
        if ($platform instanceof SqlitePlatform) {
54
            $this->connection->exec('PRAGMA foreign_keys = ON');
55
        } elseif (! $platform->supportsForeignKeyConstraints()) {
56
            $this->markTestSkipped('Only fails on platforms with foreign key constraints.');
57
        }
58
59
        $this->connection->insert('constraint_error_table', ['id' => 1]);
60
        $this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]);
61
62
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
63
64
        $this->connection->insert('owning_table', ['id' => 2, 'constraint_id' => 2]);
65
    }
66
67
    public function testForeignKeyConstraintViolationExceptionOnUpdate() : void
68
    {
69
        $platform = $this->connection->getDatabasePlatform();
70
71
        if ($platform instanceof SqlitePlatform) {
72
            $this->connection->exec('PRAGMA foreign_keys = ON');
73
        } elseif (! $platform->supportsForeignKeyConstraints()) {
74
            $this->markTestSkipped('Only fails on platforms with foreign key constraints.');
75
        }
76
77
        $this->connection->insert('constraint_error_table', ['id' => 1]);
78
        $this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]);
79
80
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
81
82
        $this->connection->update('constraint_error_table', ['id' => 2], ['id' => 1]);
83
    }
84
85
    public function testForeignKeyConstraintViolationExceptionOnDelete() : void
86
    {
87
        $platform = $this->connection->getDatabasePlatform();
88
89
        if ($platform instanceof SqlitePlatform) {
90
            $this->connection->exec('PRAGMA foreign_keys = ON');
91
        } elseif (! $platform->supportsForeignKeyConstraints()) {
92
            $this->markTestSkipped('Only fails on platforms with foreign key constraints.');
93
        }
94
95
        $this->connection->insert('constraint_error_table', ['id' => 1]);
96
        $this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]);
97
98
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
99
100
        $this->connection->delete('constraint_error_table', ['id' => 1]);
101
    }
102
103
    public function testForeignKeyConstraintViolationExceptionOnTruncate() : void
104
    {
105
        $platform = $this->connection->getDatabasePlatform();
106
107
        if ($platform instanceof SqlitePlatform) {
108
            $this->connection->exec('PRAGMA foreign_keys = ON');
109
        } elseif (! $platform->supportsForeignKeyConstraints()) {
110
            $this->markTestSkipped('Only fails on platforms with foreign key constraints.');
111
        }
112
113
        $this->connection->insert('constraint_error_table', ['id' => 1]);
114
        $this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]);
115
116
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
117
118
        $this->connection->executeUpdate($platform->getTruncateTableSQL('constraint_error_table'));
119
    }
120
}
121