You are here

public function InsertTest::testInsertIntegrityViolation in Drupal 9

Tests insertion integrity violation with no default value for a column.

File

core/tests/Drupal/KernelTests/Core/Database/InsertTest.php, line 216

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testInsertIntegrityViolation() {

  // Remove the default from the 'age' column, so that inserting a record
  // without its value specified will lead to integrity failure.
  $this->connection
    ->schema()
    ->changeField('test', 'age', 'age', [
    'description' => "The person's age",
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ]);

  // Try inserting a record that misses the value for the 'age' column,
  // should raise an IntegrityConstraintViolationException.
  $this
    ->expectException(IntegrityConstraintViolationException::class);
  $this->connection
    ->insert('test')
    ->fields([
    'name',
  ])
    ->values([
    'name' => 'Elvis',
  ])
    ->execute();
}