You are here

public function MergeTest::testMergeInsert in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/MergeTest.php \Drupal\KernelTests\Core\Database\MergeTest::testMergeInsert()

Confirms that we can merge-insert a record successfully.

File

core/tests/Drupal/KernelTests/Core/Database/MergeTest.php, line 18

Class

MergeTest
Tests the MERGE query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testMergeInsert() {
  $num_records_before = $this->connection
    ->query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $result = $this->connection
    ->merge('test_people')
    ->key('job', 'Presenter')
    ->fields([
    'age' => 31,
    'name' => 'Tiffany',
  ])
    ->execute();
  $this
    ->assertEqual($result, Merge::STATUS_INSERT, 'Insert status returned.');
  $num_records_after = $this->connection
    ->query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $this
    ->assertEqual($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
  $person = $this->connection
    ->query('SELECT * FROM {test_people} WHERE job = :job', [
    ':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.');
}