You are here

public function TestFrameworkTest::testUnionMock in GraphQL 8.3

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

Test union mocks.

@todo Unions are identical to interfaces right now, but they should not be.

File

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

Class

TestFrameworkTest
Test the test framework.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testUnionMock() {
  $this
    ->mockUnion('token', [
    'name' => 'Token',
    'types' => [
      'Word',
    ],
  ]);
  $this
    ->mockType('number', [
    'name' => 'Number',
    'unions' => [
      'Token',
    ],
  ], function ($value) {
    return is_int($value['value']);
  });
  $this
    ->mockType('word', [
    'name' => 'Word',
  ], function ($value) {
    return is_string($value['value']);
  });
  $this
    ->mockField('int_value', [
    'name' => 'value',
    'type' => 'Int',
    'parents' => [
      'Number',
    ],
  ], function ($value) {
    (yield $value['value']);
  });
  $this
    ->mockField('string_value', [
    'name' => 'value',
    'type' => 'String',
    'parents' => [
      'Word',
    ],
  ], function ($value) {
    (yield $value['value']);
  });
  $this
    ->mockField('root', [
    'name' => 'root',
    'type' => '[Token]',
  ], function () {
    (yield [
      'value' => 42,
    ]);
    (yield [
      'value' => 'GraphQL',
    ]);
  });
  $this
    ->assertResults('{ root { ... on Number { number:value } ... on Word { word:value }  } }', [], [
    'root' => [
      0 => [
        'number' => 42,
      ],
      1 => [
        'word' => 'GraphQL',
      ],
    ],
  ], $this
    ->defaultCacheMetaData());
}