You are here

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

Test mutation mocking.

File

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

Class

TestFrameworkTest
Test the test framework.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testMutationMock() {

  // Fake at least a root field, or the schema will return an error.
  $this
    ->mockField('root', [
    'name' => 'root',
    'type' => 'Boolean',
  ], TRUE);
  $this
    ->mockEnum('gender', [
    'name' => 'Gender',
  ], function () {
    return [
      'Female' => [
        'value' => 'f',
        'description' => '',
      ],
      'Male' => [
        'value' => 'm',
        'description' => '',
      ],
    ];
  });
  $this
    ->mockInputType('user', [
    'name' => 'User',
    'fields' => [
      'name' => 'String',
      'age' => 'Int',
      'gender' => 'Gender',
    ],
  ]);
  $this
    ->mockMutation('addUser', [
    'name' => 'addUser',
    'type' => 'Boolean',
    'arguments' => [
      'user' => 'User',
    ],
  ], function ($value, $args) {
    return $args['user']['age'] > 50 && $args['user']['gender'] == 'm';
  });
  $metadata = $this
    ->defaultMutationCacheMetaData();
  $this
    ->assertResults('mutation ($user: User!) { addUser(user: $user) }', [
    'user' => [
      'name' => 'John Doe',
      'age' => 52,
      'gender' => 'Male',
    ],
  ], [
    'addUser' => TRUE,
  ], $metadata);
}