You are here

public function MergeTest::testMergeUpdateExplicit 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::testMergeUpdateExplicit()

Confirms that we can merge-update a record, with alternate replacement.

File

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

Class

MergeTest
Tests the MERGE query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testMergeUpdateExplicit() {
  $num_records_before = $this->connection
    ->query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $this->connection
    ->merge('test_people')
    ->key('job', 'Speaker')
    ->insertFields([
    'age' => 31,
    'name' => 'Tiffany',
  ])
    ->updateFields([
    'name' => 'Joe',
  ])
    ->execute();
  $num_records_after = $this->connection
    ->query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $this
    ->assertEqual($num_records_before, $num_records_after, 'Merge updated properly.');
  $person = $this->connection
    ->query('SELECT * FROM {test_people} WHERE job = :job', [
    ':job' => 'Speaker',
  ])
    ->fetch();
  $this
    ->assertEqual($person->name, 'Joe', 'Name set correctly.');
  $this
    ->assertEqual($person->age, 30, 'Age skipped correctly.');
  $this
    ->assertEqual($person->job, 'Speaker', 'Job set correctly.');
}