You are here

public function SelectSubqueryTest::testFromSubquerySelectWithLimit in Drupal 10

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

Tests that we can use a subquery in a FROM clause with a LIMIT.

File

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

Class

SelectSubqueryTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testFromSubquerySelectWithLimit() {

  // Create a subquery, which is just a normal query object.
  $subquery = $this->connection
    ->select('test_task', 'tt');
  $subquery
    ->addField('tt', 'pid', 'pid');
  $subquery
    ->addField('tt', 'task', 'task');
  $subquery
    ->orderBy('priority', 'DESC');
  $subquery
    ->range(0, 1);

  // Create another query that joins against the virtual table resulting
  // from the subquery.
  $select = $this->connection
    ->select($subquery, 'tt2');
  $select
    ->join('test', 't', '[t].[id] = [tt2].[pid]');
  $select
    ->addField('t', 'name');

  // The resulting query should be equivalent to:
  // @code
  // SELECT t.name
  // FROM (SELECT tt.pid AS pid, tt.task AS task FROM test_task tt ORDER BY priority DESC LIMIT 1 OFFSET 0) tt
  //   INNER JOIN test t ON t.id=tt.pid
  // @endcode
  $people = $select
    ->execute()
    ->fetchCol();
  $this
    ->assertCount(1, $people, 'Returned the correct number of rows.');
}