You are here

function MergeTest::testMergeInsert in Zircon Profile 8

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

Confirms that we can merge-insert a record successfully.

File

core/modules/system/src/Tests/Database/MergeTest.php, line 23
Contains \Drupal\system\Tests\Database\MergeTest.

Class

MergeTest
Tests the MERGE query builder.

Namespace

Drupal\system\Tests\Database

Code

function testMergeInsert() {
  $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $result = db_merge('test_people')
    ->key('job', 'Presenter')
    ->fields(array(
    'age' => 31,
    'name' => 'Tiffany',
  ))
    ->execute();
  $this
    ->assertEqual($result, Merge::STATUS_INSERT, 'Insert status returned.');
  $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $this
    ->assertEqual($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
  $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(
    ':job' => 'Presenter',
  ))
    ->fetch();
  $this
    ->assertEqual($person->name, 'Tiffany', 'Name set correctly.');
  $this
    ->assertEqual($person->age, 31, 'Age set correctly.');
  $this
    ->assertEqual($person->job, 'Presenter', 'Job set correctly.');
}