Skip to content

vladdnepr/graphql-test

This branch is 8 commits ahead of kunicmarko20/graphql-test:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

868927e · Sep 12, 2022

History

21 Commits
Jun 28, 2018
Sep 12, 2022
Sep 12, 2022
Sep 12, 2022
Jun 28, 2018
Jun 28, 2018
Sep 12, 2022
Sep 12, 2022
Jun 28, 2018
Jun 28, 2018
Jun 28, 2018
Sep 12, 2022
Jun 28, 2018
Sep 12, 2022
Jun 28, 2018
Sep 12, 2022

Repository files navigation

GraphQL Test Case

Makes testing your GraphQL queries and mutations easier.

Support for Symfony.

PHP Version Latest Stable Version Latest Unstable Version

Build Status Coverage Status

Documentation

Installation

1. Add dependency with composer

composer require --dev vladdnepr/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 VladDnepr\GraphQLTest\Bridge\Symfony\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 VladDnepr\GraphQLTest\Bridge\Symfony\TestCase;

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

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

use VladDnepr\GraphQLTest\Bridge\Symfony\TestCase;

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

Examples

Query

use VladDnepr\GraphQLTest\Bridge\Symfony\TestCase;
use VladDnepr\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
    }
}

VladDnepr\GraphQLTest\Operation\Query construct accepts 3 arguments:

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

Mutation

use VladDnepr\GraphQLTest\Bridge\Symfony\TestCase;
use VladDnepr\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
    }
}

VladDnepr\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 VladDnepr\GraphQLTest\Bridge\Symfony\TestCase;
use VladDnepr\GraphQLTest\Operation\Mutation;
use VladDnepr\GraphQLTest\Type\EnumType;
use VladDnepr\GraphQLTest\Type\BooleanType;
use VladDnepr\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 VladDnepr\GraphQLTest\Type\TypeInterface and use your own Type instead.

About

Test Cases for easier GraphQL testing

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 92.5%
  • Makefile 4.9%
  • Shell 2.6%