2017 © Pedro Peláez
 

library graphql-test

GraphQL Test Cases

image

kunicmarko/graphql-test

GraphQL Test Cases

  • Saturday, July 7, 2018
  • by kunicmarko20
  • Repository
  • 2 Watchers
  • 1 Stars
  • 30 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

GraphQL Test Case

Makes testing your GraphQL queries and mutations easier., (*1)

Support for Symfony, Lumen and Laravel., (*2)

PHP Version Latest Stable Version Latest Unstable Version, (*3)

Build Status Coverage Status, (*4)

Documentation

Installation

1. Add dependency with composer, (*5)

composer require --dev kunicmarko/graphql-test

If you are using Symfony you will have to install "symfony/browser-kit"., (*6)

How to use

Depending on your framework, extend the correct TestCase:, (*7)

use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
use KunicMarko\GraphQLTest\Bridge\Lumen\TestCase;
use KunicMarko\GraphQLTest\Bridge\Laravel\TestCase;

Everything you see in the next snippets is the same for all Test Cases., (*8)

In your tests you now have 2 additional helper methods:, (*9)

public function query(QueryInterface $query, array $files = [], array $headers = []);
public function mutation(MutationInterface $mutation, array $files = [], array $headers = [])

By default, endpoint is /graphql, you can overwrite this by changing variable in your tests:, (*10)

use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;

class UserTest extends TestCase
{
    public static $endpoint = '/';
}

There is a helper method that allows you to preset headers:, (*11)

use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;

class SettingsTest extends TestCase
{
    protected function setUp()
    {
        $this->setDefaultHeaders([
            'Content-Type' => 'application/json',
        ]);
    }
}

Examples

Query

use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
use KunicMarko\GraphQLTest\Operation\Query;

class SettingsQueryTest extends TestCase
{
    public static $endpoint = '/';

    protected function setUp()
    {
        $this->setDefaultHeaders([
            'Content-Type' => 'application/json',
        ]);
    }

    public function testSettingsQuery(): void
    {
        $query = $this->query(
            new Query(
                'settings',
                [],
                [
                    'name',
                    'isEnabled',
                ],
            )
        );

        //Fetch response and do asserts
    }
}

KunicMarko\GraphQLTest\Operation\Query construct accepts 3 arguments:, (*12)

  • name of query (mandatory)
  • parameters (optional)
  • fields (optional)

Mutation

use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
use KunicMarko\GraphQLTest\Operation\Mutation;

class SettingsMutationTest extends TestCase
{
    public static $endpoint = '/';

    protected function setUp()
    {
        $this->setDefaultHeaders([
            'Content-Type' => 'application/json',
        ]);
    }

    public function testSettingsMutation(): void
    {
        $mutation = $this->mutation(
            new Mutation(
                'createSettings',
                [
                    'name' => 'hide-menu-bar',
                    'isEnabled' => true,
                ],
                [
                    'name',
                    'isEnabled',
                ],
            )
        );

        //Fetch response and do asserts
    }
}

KunicMarko\GraphQLTest\Operation\Mutation construct accepts 3 arguments:, (*13)

  • name of mutation (mandatory)
  • parameters (optional)
  • fields (optional)

If you have a Enum, Boolean or Array as an argument you can pass it as following:, (*14)

use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
use KunicMarko\GraphQLTest\Operation\Mutation;
use KunicMarko\GraphQLTest\Type\EnumType;
use KunicMarko\GraphQLTest\Type\BooleanType;
use KunicMarko\GraphQLTest\Type\ArrayType;

class UserMutationTest extends TestCase
{
    //...
    public function testUserMutation(): void
    {
        $mutation = $this->mutation(
            new Mutation(
                'createUser',
                [

                    'username' => 'kunicmarko20',
                    'salutation' => new EnumType('Mr'),
                    'enabled' => new BooleanType(true),
                    'roles' => new ArrayType(['ROLE_ADMIN', 'ROLE_TEST']),
                    //..
                ],
                [
                    'username',
                    'salutation',
                ],
            )
        );

        //Fetch response and do asserts
    }
}

Also, if you need a custom type you can always extend KunicMarko\GraphQLTest\Type\TypeInterface and use your own Type instead., (*15)

The Versions