You are here

protected function IntrospectionTestTrait::assertGraphQLFields in GraphQL 8.3

Assert certain fields in the GraphQL schema.

Parameters

array $fields: Array of [ParentType, FieldName, FieldType] triples. Field type can use the GraphQL type notation.

bool $invert: Invert the result and check for non-existence instead.

6 calls to IntrospectionTestTrait::assertGraphQLFields()
BundleLessEntityTest::testConfiguredField in modules/graphql_core/tests/src/Kernel/Entity/BundleLessEntityTest.php
Test if a field is available on the user type.
EntityFieldValueTest::testBoolean in modules/graphql_core/tests/src/Kernel/Entity/EntityFieldValueTest.php
Test boolean fields.
EntityFieldValueTest::testFieldAssignment in modules/graphql_core/tests/src/Kernel/Entity/EntityFieldValueTest.php
Verify that fields are assigned correctly among bundles.
EntityFieldValueTest::testFilteredText in modules/graphql_core/tests/src/Kernel/Entity/EntityFieldValueTest.php
Test filtered text fields.
EntityFieldValueTest::testText in modules/graphql_core/tests/src/Kernel/Entity/EntityFieldValueTest.php
Test a simple text field.

... See full list

File

tests/src/Traits/IntrospectionTestTrait.php, line 36

Class

IntrospectionTestTrait
Trait to retrieve a name-indexed schema to run assertions on it.

Namespace

Drupal\Tests\graphql\Traits

Code

protected function assertGraphQLFields(array $fields, $invert = FALSE) {
  $schema = $this
    ->introspect();
  foreach ($fields as list($parent, $name, $type)) {
    $this
      ->assertArrayHasKey($parent, $schema['types'], "Type {$parent} not found.");
    if ($invert) {
      $this
        ->assertArrayNotHasKey($name, $schema['types'][$parent]['fields'], "Field {$name} found on type {$parent}.");
      continue;
    }
    $this
      ->assertArrayHasKey($parent, $schema['types'], "Type {$parent} not found.");
    $this
      ->assertArrayHasKey($name, $schema['types'][$parent]['fields'], "Field {$name} not found on type {$parent}.");
    $field = $schema['types'][$parent]['fields'][$name];
    list($type, $decorators) = StringHelper::parseType($type);
    $decorators = array_map(function ($decorator) {
      return $decorator[1];
    }, $decorators);
    if (in_array('listOf', $decorators)) {
      $this
        ->assertEquals('LIST', $field['type']['kind'], "Expected field {$name} to be a list.");
      $this
        ->assertEquals($type, $field['type']['ofType']['name'], "Expected field {$name} to be a list of {$type}.");
    }
    else {
      $this
        ->assertEquals($type, $field['type']['name'], "Expected field {$name} to be {$type}.");
    }
  }
}