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. 10 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 15

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->assertIdentical($query->count(), 1, '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->assertIdentical($saved_age, '29', 'Can retrieve after inserting.');
}

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