You are here

public function TestFrameworkTest::testUnionMock 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::testUnionMock()

Test union mocks.

File

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

Class

TestFrameworkTest
Test the test framework.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

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

      type Query {
        root: [Token]
      }

      union Token = Number | Word

      type Number implements Token {
        value: Int
      }

      type Word implements Token {
        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',
    ],
  ]);
  $this
    ->assertResults('{ root { ... on Number { number:value } ... on Word { word:value }  } }', [], [
    'root' => [
      0 => [
        'number' => 42,
      ],
      1 => [
        'word' => 'GraphQL',
      ],
    ],
  ]);
}