You are here

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

Test mutation mocking.

File

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

Class

TestFrameworkTest
Test the test framework.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testMutationMock() : void {
  $schema = <<<GQL
      schema {
        query: Query
        mutation: Mutation
      }

      type Query {
        root: Boolean
      }

      type Mutation {
        addUser(user: User!): Boolean
      }

      enum Gender {
        Female
        Male
      }

      input User {
        name: String
        age: Int
        gender: Gender
      }
GQL;
  $this
    ->setUpSchema($schema);
  $this
    ->mockResolver('Query', 'root', TRUE);
  $this
    ->mockResolver('Mutation', 'addUser', function ($parent, $args) {
    return $args['user']['age'] > 50 && $args['user']['gender'] === 'Male';
  });
  $metadata = $this
    ->defaultMutationCacheMetaData();
  $this
    ->assertResults('mutation ($user: User!) { addUser(user: $user) }', [
    'user' => [
      'name' => 'John Doe',
      'age' => 52,
      'gender' => 'Male',
    ],
  ], [
    'addUser' => TRUE,
  ], $metadata);
}