You are here

public function UpdateComplexTest::testSubSelectUpdate in Drupal 8

Same name and namespace in other branches
  1. 9 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.'));
}