function UpdateComplexTest::testSubSelectUpdate

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

Tests UPDATE with a subselect value.

File

core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php, line 126

Class

UpdateComplexTest
Tests the Update query builder, complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSubSelectUpdate() {
    $subselect = $this->connection
        ->select('test_task', 't');
    $subselect->addExpression('MAX([priority]) + :increment', 'max_priority', [
        ':increment' => 30,
    ]);
    // Clone this to make sure we are running a different query when
    // asserting.
    $select = clone $subselect;
    $query = $this->connection
        ->update('test')
        ->expression('age', $subselect)
        ->condition('name', 'Ringo');
    // Save the number of rows that updated for assertion later.
    $num_updated = $query->execute();
    $after_age = $this->connection
        ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
        ':name' => 'Ringo',
    ])
        ->fetchField();
    $expected_age = $select->execute()
        ->fetchField();
    $this->assertEquals($expected_age, $after_age);
    // Expect 1 row to be updated.
    $this->assertEquals(1, $num_updated);
}

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