You are here

public function TestFrameworkTest::testInterfaceMock in GraphQL 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/Framework/TestFrameworkTest.php \Drupal\Tests\graphql\Kernel\Framework\TestFrameworkTest::testInterfaceMock()

Test interface mocking.

File

tests/src/Kernel/Framework/TestFrameworkTest.php, line 139

Class

TestFrameworkTest
Test the test framework.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testInterfaceMock() : void {
  $schema = <<<GQL
      schema {
        query: Query
      }

      type Query {
        root: [Token]
      }

      interface Token {
        id: Int
      }

      type Number implements Token {
        id: Int
        value: Int
      }

      type Word implements Token {
        id: Int
        value: String
      }
GQL;
  $this
    ->setUpSchema($schema);
  $this
    ->mockTypeResolver('Token', function ($value) {
    return is_int($value['value']) ? 'Number' : 'Word';
  });
  $this
    ->mockResolver('Query', 'root', [
    [
      'value' => 42,
    ],
    [
      'value' => 'GraphQL',
    ],
  ]);
  $query = <<<GQL
      query {
        root {
          ... on Number {
            number: value
          }

          ... on Word {
            word:value
          }
        }
      }
GQL;
  $this
    ->assertResults($query, [], [
    'root' => [
      0 => [
        'number' => 42,
      ],
      1 => [
        'word' => 'GraphQL',
      ],
    ],
  ]);
}