|
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\Schema\Table; |
|
10
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
|
11
|
|
|
|
|
12
|
|
|
class ForeignKeyExceptionTest extends DbalFunctionalTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
protected function setUp() : void |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
|
|
18
|
|
|
if (! $this->connection->getDriver() instanceof ExceptionConverterDriver) { |
|
19
|
|
|
$this->markTestSkipped('Driver does not support special exception handling.'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$schemaManager = $this->connection->getSchemaManager(); |
|
23
|
|
|
|
|
24
|
|
|
$table = new Table('constraint_error_table'); |
|
25
|
|
|
$table->addColumn('id', 'integer', []); |
|
26
|
|
|
$table->setPrimaryKey(['id']); |
|
27
|
|
|
|
|
28
|
|
|
$owningTable = new Table('owning_table'); |
|
29
|
|
|
$owningTable->addColumn('id', 'integer', []); |
|
30
|
|
|
$owningTable->addColumn('constraint_id', 'integer', []); |
|
31
|
|
|
$owningTable->setPrimaryKey(['id']); |
|
32
|
|
|
$owningTable->addForeignKeyConstraint($table, ['constraint_id'], ['id']); |
|
33
|
|
|
|
|
34
|
|
|
$schemaManager->createTable($table); |
|
35
|
|
|
$schemaManager->createTable($owningTable); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function tearDown() : void |
|
39
|
|
|
{ |
|
40
|
|
|
parent::tearDown(); |
|
41
|
|
|
|
|
42
|
|
|
$schemaManager = $this->connection->getSchemaManager(); |
|
43
|
|
|
|
|
44
|
|
|
$schemaManager->dropTable('owning_table'); |
|
45
|
|
|
$schemaManager->dropTable('constraint_error_table'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testForeignKeyConstraintViolationExceptionOnInsert() : void |
|
49
|
|
|
{ |
|
50
|
|
|
if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
|
51
|
|
|
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->connection->insert('constraint_error_table', ['id' => 1]); |
|
55
|
|
|
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); |
|
56
|
|
|
|
|
57
|
|
|
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); |
|
58
|
|
|
|
|
59
|
|
|
$this->connection->insert('owning_table', ['id' => 2, 'constraint_id' => 2]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testForeignKeyConstraintViolationExceptionOnUpdate() : void |
|
63
|
|
|
{ |
|
64
|
|
|
if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
|
65
|
|
|
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->connection->insert('constraint_error_table', ['id' => 1]); |
|
69
|
|
|
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); |
|
70
|
|
|
|
|
71
|
|
|
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); |
|
72
|
|
|
|
|
73
|
|
|
$this->connection->update('constraint_error_table', ['id' => 2], ['id' => 1]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testForeignKeyConstraintViolationExceptionOnDelete() : void |
|
77
|
|
|
{ |
|
78
|
|
|
if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
|
79
|
|
|
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->connection->insert('constraint_error_table', ['id' => 1]); |
|
83
|
|
|
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); |
|
84
|
|
|
|
|
85
|
|
|
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); |
|
86
|
|
|
|
|
87
|
|
|
$this->connection->delete('constraint_error_table', ['id' => 1]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testForeignKeyConstraintViolationExceptionOnTruncate() : void |
|
91
|
|
|
{ |
|
92
|
|
|
$platform = $this->connection->getDatabasePlatform(); |
|
93
|
|
|
|
|
94
|
|
|
if (! $platform->supportsForeignKeyConstraints()) { |
|
95
|
|
|
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->connection->insert('constraint_error_table', ['id' => 1]); |
|
99
|
|
|
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); |
|
100
|
|
|
|
|
101
|
|
|
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); |
|
102
|
|
|
|
|
103
|
|
|
$this->connection->executeUpdate($platform->getTruncateTableSQL('constraint_error_table')); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|