You are here

public function MergeTest::testMergeUpdateExcept in Drupal 9

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

Confirms that we can merge-update a record successfully.

This test varies from the previous test because it manually defines which fields are inserted, and which fields are updated.

File

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

Class

MergeTest
Tests the MERGE query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

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