cjweber/laravel-test-graphql-client

用于在您的GraphQL API请求上运行集成测试的客户端和TestCase扩展

v1.0.4 2022-09-03 21:09 UTC

This package is auto-updated.

Last update: 2024-09-30 01:16:56 UTC


README

这是一个允许您轻松快速地对您的GraphQL API创建集成测试的包。

要求

1. Laravel 6.0 + / PHP 7.0 +
2. PHPUnit 8.0 +
3. GraphQL setup on your Laravel server, via https://github.com/rebing/graphql-laravel or other

包含内容

1. Basic GraphQL Client
2. Extension of TestCase which allows for access to the GraphQL client within your tests

安装

composer require cjweber/laravel-test-graphql-client --dev

使用方法

1. Extend the package TestCase for your feature test in order to leverage the included GraphQL Client
2. Make queries against your GraphQL by constructing a Query object and then passing it to the `graphql` property on your TestCase class
3. Use the `assertGraphQlFields` function in the package to test that the server returned the fields you expect. You can also do any other assertions that come with PHPUnit against the response data returned by the client.

示例

<?php

namespace Tests\Feature\GraphQL;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use GraphQLTestClient\TestCase;
use GraphQLTestClient\Field;
use GraphQLTestClient\Query;

use App\Project;
use App\User;

class ProjectTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function setUp() :void
    {
        parent::setUp();
        $this->user = factory(User::class)->create();
    }

    /**
     * Test that a User can create a Project
     *
     * @return void
     */
    public function testCreateProject()
    {
        $this->actingAs($this->user, 'api');

        $description = $this->faker->paragraph;
        $name = $this->faker->sentence;

        $query = new Query(
            'project',
            [
                'input' => [
                    'name' => $name,
                    'description' => $description,
                ],
            ],
            [
                new Field('uuid'),
                new Field('description'),
                new Field('name'),
            ]
        );

        $result = $this->graphql->mutate($query)->getData();

        $this->graphql->assertGraphQlFields($result, $query);
        $createdProject = Project::where('uuid', $result['uuid'])->first();
        $this->assertEquals($createdProject->name, $name);
        $this->assertEquals($createdProject->description, $description);
        $this->assertEquals($result['name'], $name);
        $this->assertEquals($result['description'], $description);
    }
}

感谢

Christian Goltz 为在 https://github.com/christiangoltz/graphql-client-php 提供了大量的基础GraphQL客户端代码表示感谢。

反馈/变更

如果您想提出改进或变更,请随时通过 christopher.j.weber@protonmail.com 发送邮件,创建问题报告,或提交PR。