function DriverSpecificSchemaTestBase::tryInsertExpectsIntegrityConstraintViolationException

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php \Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase::tryInsertExpectsIntegrityConstraintViolationException()

Tries to insert a value that throws an IntegrityConstraintViolationException.

Parameters

string $tableName: The table to insert.

1 call to DriverSpecificSchemaTestBase::tryInsertExpectsIntegrityConstraintViolationException()
DriverSpecificSchemaTestBase::testChangePrimaryKeyToSerial in core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php
Tests converting an int to a serial when the int column has data.
1 method overrides DriverSpecificSchemaTestBase::tryInsertExpectsIntegrityConstraintViolationException()
SchemaTest::tryInsertExpectsIntegrityConstraintViolationException in core/modules/sqlite/tests/src/Kernel/sqlite/SchemaTest.php
Tries to insert a value that throws an IntegrityConstraintViolationException.

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php, line 110

Class

DriverSpecificSchemaTestBase
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function tryInsertExpectsIntegrityConstraintViolationException(string $tableName) : void {
    try {
        $this->connection
            ->insert($tableName)
            ->fields([
            'test_field_string' => 'test',
        ])
            ->execute();
        $this->fail('Expected IntegrityConstraintViolationException not thrown');
    } catch (IntegrityConstraintViolationException) {
        // Do nothing, it's the expected behavior.
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.