function MergeTest::testMergeInsertWithoutUpdate

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

Tests that we can merge-insert without any update fields.

File

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

Class

MergeTest
Tests the MERGE query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testMergeInsertWithoutUpdate() : void {
    $num_records_before = $this->connection
        ->query('SELECT COUNT(*) FROM {test_people}')
        ->fetchField();
    $this->connection
        ->merge('test_people')
        ->key('job', 'Presenter')
        ->execute();
    $num_records_after = $this->connection
        ->query('SELECT COUNT(*) FROM {test_people}')
        ->fetchField();
    $this->assertEquals($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->assertEquals('', $person->name, 'Name set correctly.');
    $this->assertEquals(0, $person->age, 'Age set correctly.');
    $this->assertEquals('Presenter', $person->job, 'Job set correctly.');
}

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