function InsertTest::testSimpleInsert

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testSimpleInsert()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testSimpleInsert()
  3. 11.x 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 19

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSimpleInsert() : void {
    $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->assertCount(1, $query, '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.');
}

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