You are here

function InsertTest::testSimpleInsert in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/InsertTest.php \Drupal\system\Tests\Database\InsertTest::testSimpleInsert()

Tests very basic insert functionality.

File

core/modules/system/src/Tests/Database/InsertTest.php, line 20
Contains \Drupal\system\Tests\Database\InsertTest.

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\system\Tests\Database

Code

function testSimpleInsert() {
  $num_records_before = db_query('SELECT COUNT(*) FROM {test}')
    ->fetchField();
  $query = db_insert('test');
  $query
    ->fields(array(
    'name' => 'Yoko',
    'age' => '29',
  ));
  $query
    ->execute();
  $num_records_after = db_query('SELECT COUNT(*) FROM {test}')
    ->fetchField();
  $this
    ->assertIdentical($num_records_before + 1, (int) $num_records_after, 'Record inserts correctly.');
  $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Yoko',
  ))
    ->fetchField();
  $this
    ->assertIdentical($saved_age, '29', 'Can retrieve after inserting.');
}