You are here

public function SelectSubqueryTest::testNotExistsSubquerySelect in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testNotExistsSubquerySelect()

Tests NOT EXISTS subquery conditionals on SELECT statements.

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

File

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

Class

SelectSubqueryTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testNotExistsSubquerySelect() {

  // 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].[name] = [t].[name]');
  $query
    ->notExists($subquery);

  // Ensure that we got the right number of records.
  $people = $query
    ->execute()
    ->fetchCol();
  $this
    ->assertCount(3, $people, 'NOT EXISTS query returned the correct results.');
}