Skip to content

Commit

Permalink
Reuse existing tests for SQLite FK constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Dec 30, 2019
1 parent a2b4848 commit d729f7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\PDOSqlite\Driver;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\Tests\DBAL\Functional\Driver\AbstractDriverTest;
use function extension_loaded;

Expand Down Expand Up @@ -47,31 +46,4 @@ protected static function getDatabaseNameForConnectionWithoutDatabaseNameParamet
{
return '';
}

public function testForeignKeyConstraintViolationException() : void
{
$this->connection->exec('PRAGMA foreign_keys = ON');

$this->connection->exec('
CREATE TABLE parent (
id INTEGER PRIMARY KEY
)
');

$this->connection->exec('
CREATE TABLE child (
id INTEGER PRIMARY KEY,
parent_id INTEGER,
FOREIGN KEY (parent_id) REFERENCES parent(id)
);
');

$this->connection->exec('INSERT INTO parent (id) VALUES (1)');
$this->connection->exec('INSERT INTO child (id, parent_id) VALUES (1, 1)');
$this->connection->exec('INSERT INTO child (id, parent_id) VALUES (2, 1)');

$this->expectException(ForeignKeyConstraintViolationException::class);

$this->connection->exec('INSERT INTO child (id, parent_id) VALUES (3, 2)');
}
}
22 changes: 18 additions & 4 deletions tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ public function testTableExistsException() : void

public function testForeignKeyConstraintViolationExceptionOnInsert() : void
{
if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$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.');
}

Expand Down Expand Up @@ -112,7 +116,11 @@ public function testForeignKeyConstraintViolationExceptionOnInsert() : void

public function testForeignKeyConstraintViolationExceptionOnUpdate() : void
{
if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$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.');
}

Expand Down Expand Up @@ -146,7 +154,11 @@ public function testForeignKeyConstraintViolationExceptionOnUpdate() : void

public function testForeignKeyConstraintViolationExceptionOnDelete() : void
{
if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$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.');
}

Expand Down Expand Up @@ -182,7 +194,9 @@ public function testForeignKeyConstraintViolationExceptionOnTruncate() : void
{
$platform = $this->connection->getDatabasePlatform();

if (! $platform->supportsForeignKeyConstraints()) {
if ($platform instanceof SqlitePlatform) {
$this->connection->exec('PRAGMA foreign_keys = ON');
} elseif (! $platform->supportsForeignKeyConstraints()) {
$this->markTestSkipped('Only fails on platforms with foreign key constraints.');
}

Expand Down

0 comments on commit d729f7b

Please sign in to comment.