Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testExistsSubquerySelect()
  2. 9 core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testExistsSubquerySelect()

Tests EXISTS subquery conditionals on SELECT statements.

We essentially select all rows from the {test} table that have matching rows in the {test_people} table based on the shared name column.

File

core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php, line 220

Class

SelectSubqueryTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testExistsSubquerySelect() {

  // Put George into {test_people}.
  $this->connection
    ->insert('test_people')
    ->fields([
    'name' => 'George',
    'age' => 27,
    'job' => 'Singer',
  ])
    ->execute();

  // Base query to {test}.
  $query = $this->connection
    ->select('test', 't')
    ->fields('t', [
    'name',
  ]);

  // Subquery to {test_people}.
  $subquery = $this->connection
    ->select('test_people', 'tp')
    ->fields('tp', [
    'name',
  ])
    ->where('[tp].[age] = [t].[age]');
  $query
    ->exists($subquery);
  $result = $query
    ->execute();

  // Ensure that we got the right record.
  $record = $result
    ->fetch();
  $this
    ->assertEquals('George', $record->name, 'Fetched name is correct using EXISTS query.');
}