-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3800 from BenMorel/finally
Refactor FK exception tests
- Loading branch information
Showing
2 changed files
with
120 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
tests/Doctrine/Tests/DBAL/Functional/ForeignKeyExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DBAL\Functional; | ||
|
||
use Doctrine\DBAL\Driver\ExceptionConverterDriver; | ||
use Doctrine\DBAL\Exception; | ||
use Doctrine\DBAL\Platforms\SqlitePlatform; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\Tests\DbalFunctionalTestCase; | ||
|
||
class ForeignKeyExceptionTest extends DbalFunctionalTestCase | ||
{ | ||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
if (! $this->connection->getDriver() instanceof ExceptionConverterDriver) { | ||
$this->markTestSkipped('Driver does not support special exception handling.'); | ||
} | ||
|
||
$schemaManager = $this->connection->getSchemaManager(); | ||
|
||
$table = new Table('constraint_error_table'); | ||
$table->addColumn('id', 'integer', []); | ||
$table->setPrimaryKey(['id']); | ||
|
||
$owningTable = new Table('owning_table'); | ||
$owningTable->addColumn('id', 'integer', []); | ||
$owningTable->addColumn('constraint_id', 'integer', []); | ||
$owningTable->setPrimaryKey(['id']); | ||
$owningTable->addForeignKeyConstraint($table, ['constraint_id'], ['id']); | ||
|
||
$schemaManager->createTable($table); | ||
$schemaManager->createTable($owningTable); | ||
} | ||
|
||
protected function tearDown() : void | ||
{ | ||
parent::tearDown(); | ||
|
||
$schemaManager = $this->connection->getSchemaManager(); | ||
|
||
$schemaManager->dropTable('owning_table'); | ||
$schemaManager->dropTable('constraint_error_table'); | ||
} | ||
|
||
public function testForeignKeyConstraintViolationExceptionOnInsert() : void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ($platform instanceof SqlitePlatform) { | ||
$this->connection->exec('PRAGMA foreign_keys = ON'); | ||
} elseif (! $platform->supportsForeignKeyConstraints()) { | ||
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); | ||
} | ||
|
||
$this->connection->insert('constraint_error_table', ['id' => 1]); | ||
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); | ||
|
||
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); | ||
|
||
$this->connection->insert('owning_table', ['id' => 2, 'constraint_id' => 2]); | ||
} | ||
|
||
public function testForeignKeyConstraintViolationExceptionOnUpdate() : void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ($platform instanceof SqlitePlatform) { | ||
$this->connection->exec('PRAGMA foreign_keys = ON'); | ||
} elseif (! $platform->supportsForeignKeyConstraints()) { | ||
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); | ||
} | ||
|
||
$this->connection->insert('constraint_error_table', ['id' => 1]); | ||
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); | ||
|
||
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); | ||
|
||
$this->connection->update('constraint_error_table', ['id' => 2], ['id' => 1]); | ||
} | ||
|
||
public function testForeignKeyConstraintViolationExceptionOnDelete() : void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ($platform instanceof SqlitePlatform) { | ||
$this->connection->exec('PRAGMA foreign_keys = ON'); | ||
} elseif (! $platform->supportsForeignKeyConstraints()) { | ||
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); | ||
} | ||
|
||
$this->connection->insert('constraint_error_table', ['id' => 1]); | ||
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); | ||
|
||
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); | ||
|
||
$this->connection->delete('constraint_error_table', ['id' => 1]); | ||
} | ||
|
||
public function testForeignKeyConstraintViolationExceptionOnTruncate() : void | ||
{ | ||
$platform = $this->connection->getDatabasePlatform(); | ||
|
||
if ($platform instanceof SqlitePlatform) { | ||
$this->connection->exec('PRAGMA foreign_keys = ON'); | ||
} elseif (! $platform->supportsForeignKeyConstraints()) { | ||
$this->markTestSkipped('Only fails on platforms with foreign key constraints.'); | ||
} | ||
|
||
$this->connection->insert('constraint_error_table', ['id' => 1]); | ||
$this->connection->insert('owning_table', ['id' => 1, 'constraint_id' => 1]); | ||
|
||
$this->expectException(Exception\ForeignKeyConstraintViolationException::class); | ||
|
||
$this->connection->executeUpdate($platform->getTruncateTableSQL('constraint_error_table')); | ||
} | ||
} |