function UpsertTest::testDegeneratedMultiValueUpsert

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Database/UpsertTest.php \Drupal\KernelTests\Core\Database\UpsertTest::testDegeneratedMultiValueUpsert()

Tests a multi-value upsert that has no columns to update.

File

core/tests/Drupal/KernelTests/Core/Database/UpsertTest.php, line 242

Class

UpsertTest
Tests the Upsert query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testDegeneratedMultiValueUpsert() : void {
  $this->assertSame('1', $this->connection
    ->query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField());
  // An upsert that only adds a record.
  $this->connection
    ->upsert('test_people')
    ->key('job')
    ->fields([
    'job',
  ])
    ->values([
    'Plumber',
  ])
    ->execute();
  $this->assertSame('2', $this->connection
    ->query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField());
  // Do a multi-value upsert that should add two more records.
  $this->connection
    ->upsert('test_people')
    ->key('job')
    ->fields([
    'job',
  ])
    ->values([
    'Plumber',
  ])
    ->values([
    'Carpenter',
  ])
    ->values([
    'GalacticHitchhiker',
  ])
    ->execute();
  // Two records added, for a total of four.
  $this->assertSame('4', $this->connection
    ->query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField());
}

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