kunicmarko / graphql-test
GraphQL 测试用例
0.2.0
2020-06-30 09:59 UTC
Requires
- php: ^7.1
Requires (Dev)
- laravel/laravel: ^5.6
- laravel/lumen-framework: ^5.6
- symfony/browser-kit: ^3.4 || ^4.0 || ^4.1
- symfony/framework-bundle: ^3.4 || ^4.0 || ^4.1
- symfony/phpunit-bridge: ^4.0
This package is auto-updated.
Last update: 2024-08-29 03:26:11 UTC
README
使测试您的 GraphQL 查询和变异变得简单。
支持 Symfony、Lumen 和 Laravel。
文档
安装
1. 使用 composer 添加依赖
composer require --dev kunicmarko/graphql-test
如果您使用的是 Symfony,您必须安装 "symfony/browser-kit"。
如何使用
根据您的框架,扩展正确的 TestCase
use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase; use KunicMarko\GraphQLTest\Bridge\Lumen\TestCase; use KunicMarko\GraphQLTest\Bridge\Laravel\TestCase;
接下来片段中显示的内容对所有测试用例都相同。
在您的测试中,现在有 2 个额外的辅助方法
public function query(QueryInterface $query, array $files = [], array $headers = []); public function mutation(MutationInterface $mutation, array $files = [], array $headers = [])
默认情况下,端点是 /graphql
,您可以在测试中通过更改变量来覆盖此设置
use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase; class UserTest extends TestCase { public static $endpoint = '/'; }
有一个辅助方法允许您预先设置头信息
use KunicMarko\GraphQLTest\Bridge\Symfony\TestCase; class SettingsTest extends TestCase { protected function setUp() { $this->setDefaultHeaders([ 'Content-Type' => 'application/json', ]); } }
示例
查询
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
构造函数接受 3 个参数
- 查询名称(必需)
- 参数(可选)
- 字段(可选)
变异
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
构造函数接受 3 个参数
- 变异名称(必需)
- 参数(可选)
- 字段(可选)
如果您有 Enum、Boolean 或 Array 作为参数,可以按照以下方式传递
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 } }
此外,如果您需要自定义类型,您始终可以扩展 KunicMarko\GraphQLTest\Type\TypeInterface
并使用您自己的类型。