GitXplorerGitXplorer
k

graphql-test

public
13 stars
7 forks
1 issues

Commits

List of commits on branch master.
Verified
95d3ea942cac403d8e3338be8fa545964307c546

Add the ability to change the HTTP method used (#1)

cchrisnharvey committed 4 years ago
Unverified
07d377ce34322511c1776e6bbf45e135273f4b5f

fix tests

kkunicmarko20 committed 6 years ago
Unverified
a29b23ee1735cd238c702f65a4fe21fa341cafdc

fix case if Array is empty in parameters

kkunicmarko20 committed 6 years ago
Verified
89b54e9b7dedf2a270c314d8b23dbe82375fa1d0

Update README.md

kkunicmarko20 committed 6 years ago
Unverified
3c6064c4fce2d70af9d7ec524f363ce58e94e9b0

allow cookies, fix tests

kkunicmarko20 committed 6 years ago
Unverified
a540fd2d19bc59a5a54ec46f62d57d4b9041f81b

improve tests

kkunicmarko20 committed 6 years ago

README

The README file for this repository.

GraphQL Test Case

Makes testing your GraphQL queries and mutations easier.

Support for Symfony, Lumen and Laravel.

PHP Version Latest Stable Version Latest Unstable Version

Build Status Coverage Status

Documentation

Installation

1. Add dependency with composer

composer require --dev kunicmarko/graphql-test

If you are using Symfony you will have to install "symfony/browser-kit".

How to use

Depending on your framework, extend the correct TestCase:

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.

In your tests you now have 2 additional helper methods:

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:

use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;

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

There is a helper method that allows you to preset headers:

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:

  • 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:

  • 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:

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.