function MergeTest::testMergeUpdate

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/MergeTest.php \Drupal\KernelTests\Core\Database\MergeTest::testMergeUpdate()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/MergeTest.php \Drupal\KernelTests\Core\Database\MergeTest::testMergeUpdate()
  3. 10 core/tests/Drupal/KernelTests/Core/Database/MergeTest.php \Drupal\KernelTests\Core\Database\MergeTest::testMergeUpdate()

Confirms that we can merge-update a record successfully.

File

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

Class

MergeTest
Tests the MERGE query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testMergeUpdate() : void {
    $num_records_before = $this->connection
        ->query('SELECT COUNT(*) FROM {test_people}')
        ->fetchField();
    $result = $this->connection
        ->merge('test_people')
        ->key('job', 'Speaker')
        ->fields([
        'age' => 31,
        'name' => 'Tiffany',
    ])
        ->execute();
    $this->assertEquals(Merge::STATUS_UPDATE, $result, 'Update status returned.');
    $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(31, $person->age, 'Age set correctly.');
    $this->assertEquals('Speaker', $person->job, 'Job set correctly.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.