You are here

public function UpdateComplexTest::testSubSelectUpdate in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php \Drupal\KernelTests\Core\Database\UpdateComplexTest::testSubSelectUpdate()
  2. 9 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);
}