function UpdateComplexTest::testSubSelectUpdate

Same name in other branches
  1. 9 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()

Test UPDATE with a subselect value.

File

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

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->assertEqual($after_age, $expected_age);
    $this->assertEqual(1, $num_updated, t('Expected 1 row to be updated in subselect update query.'));
}

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