You are here

public function InsertTest::testSimpleInsert in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testSimpleInsert()

Tests very basic insert functionality.

File

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

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSimpleInsert() {
  $num_records_before = $this->connection
    ->query('SELECT COUNT(*) FROM {test}')
    ->fetchField();
  $query = $this->connection
    ->insert('test');
  $query
    ->fields([
    'name' => 'Yoko',
    'age' => '29',
  ]);

  // Check how many records are queued for insertion.
  $this
    ->assertSame(1, $query
    ->count(), 'One record is queued for insertion.');
  $query
    ->execute();
  $num_records_after = $this->connection
    ->query('SELECT COUNT(*) FROM {test}')
    ->fetchField();
  $this
    ->assertSame($num_records_before + 1, (int) $num_records_after, 'Record inserts correctly.');
  $saved_age = $this->connection
    ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
    ':name' => 'Yoko',
  ])
    ->fetchField();
  $this
    ->assertSame('29', $saved_age, 'Can retrieve after inserting.');
}