You are here

public function FilterTest::testQueryCondition in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/Query/FilterTest.php \Drupal\Tests\jsonapi\Kernel\Query\FilterTest::testQueryCondition()

@covers ::queryCondition

File

tests/src/Kernel/Query/FilterTest.php, line 131

Class

FilterTest
@coversDefaultClass \Drupal\jsonapi\Query\Filter @group jsonapi @group jsonapi_query @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Query

Code

public function testQueryCondition() {

  // Can't use a data provider because we need access to the container.
  $data = $this
    ->queryConditionData();
  $get_sql_query_for_entity_query = function ($entity_query) {

    // Expose parts of \Drupal\Core\Entity\Query\Sql\Query::execute().
    $o = new \ReflectionObject($entity_query);
    $m1 = $o
      ->getMethod('prepare');
    $m1
      ->setAccessible(TRUE);
    $m2 = $o
      ->getMethod('compile');
    $m2
      ->setAccessible(TRUE);

    // The private property computed by the two previous private calls, whose
    // value we need to inspect.
    $p = $o
      ->getProperty('sqlQuery');
    $p
      ->setAccessible(TRUE);
    $m1
      ->invoke($entity_query);
    $m2
      ->invoke($entity_query);
    return (string) $p
      ->getValue($entity_query);
  };
  $resource_type = new ResourceType('node', 'painting', NULL);
  foreach ($data as $case) {
    $parameter = $case[0];
    $expected_query = $case[1];
    $filter = Filter::createFromQueryParameter($parameter, $resource_type, $this->fieldResolver);
    $query = $this->nodeStorage
      ->getQuery();

    // Get the query condition parsed from the input.
    $condition = $filter
      ->queryCondition($query);

    // Apply it to the query.
    $query
      ->condition($condition);

    // Verify the SQL query is exactly the same.
    $expected_sql_query = $get_sql_query_for_entity_query($expected_query);
    $actual_sql_query = $get_sql_query_for_entity_query($query);
    $this
      ->assertSame($expected_sql_query, $actual_sql_query);

    // Compare the results.
    $this
      ->assertEquals($expected_query
      ->execute(), $query
      ->execute());
  }
}