项目作者: kunicmarko20

项目描述 :
Test Cases for easier GraphQL testing
高级语言: PHP
项目地址: git://github.com/kunicmarko20/graphql-test.git
创建时间: 2018-06-28T18:59:23Z
项目社区:https://github.com/kunicmarko20/graphql-test

开源协议:MIT License

下载


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

  1. 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:

  1. use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
  2. use KunicMarko\GraphQLTest\Bridge\Lumen\TestCase;
  3. 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:

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

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

  1. use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
  2. class UserTest extends TestCase
  3. {
  4. public static $endpoint = '/';
  5. }

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

  1. use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
  2. class SettingsTest extends TestCase
  3. {
  4. protected function setUp()
  5. {
  6. $this->setDefaultHeaders([
  7. 'Content-Type' => 'application/json',
  8. ]);
  9. }
  10. }

Examples

Query

  1. use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
  2. use KunicMarko\GraphQLTest\Operation\Query;
  3. class SettingsQueryTest extends TestCase
  4. {
  5. public static $endpoint = '/';
  6. protected function setUp()
  7. {
  8. $this->setDefaultHeaders([
  9. 'Content-Type' => 'application/json',
  10. ]);
  11. }
  12. public function testSettingsQuery(): void
  13. {
  14. $query = $this->query(
  15. new Query(
  16. 'settings',
  17. [],
  18. [
  19. 'name',
  20. 'isEnabled',
  21. ],
  22. )
  23. );
  24. //Fetch response and do asserts
  25. }
  26. }

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

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

Mutation

  1. use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
  2. use KunicMarko\GraphQLTest\Operation\Mutation;
  3. class SettingsMutationTest extends TestCase
  4. {
  5. public static $endpoint = '/';
  6. protected function setUp()
  7. {
  8. $this->setDefaultHeaders([
  9. 'Content-Type' => 'application/json',
  10. ]);
  11. }
  12. public function testSettingsMutation(): void
  13. {
  14. $mutation = $this->mutation(
  15. new Mutation(
  16. 'createSettings',
  17. [
  18. 'name' => 'hide-menu-bar',
  19. 'isEnabled' => true,
  20. ],
  21. [
  22. 'name',
  23. 'isEnabled',
  24. ],
  25. )
  26. );
  27. //Fetch response and do asserts
  28. }
  29. }

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:

  1. use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase;
  2. use KunicMarko\GraphQLTest\Operation\Mutation;
  3. use KunicMarko\GraphQLTest\Type\EnumType;
  4. use KunicMarko\GraphQLTest\Type\BooleanType;
  5. use KunicMarko\GraphQLTest\Type\ArrayType;
  6. class UserMutationTest extends TestCase
  7. {
  8. //...
  9. public function testUserMutation(): void
  10. {
  11. $mutation = $this->mutation(
  12. new Mutation(
  13. 'createUser',
  14. [
  15. 'username' => 'kunicmarko20',
  16. 'salutation' => new EnumType('Mr'),
  17. 'enabled' => new BooleanType(true),
  18. 'roles' => new ArrayType(['ROLE_ADMIN', 'ROLE_TEST']),
  19. //..
  20. ],
  21. [
  22. 'username',
  23. 'salutation',
  24. ],
  25. )
  26. );
  27. //Fetch response and do asserts
  28. }
  29. }

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