miniaturebase/pest-plugin-graphql

使用 Pest 以优雅的方式测试您的 PHP GraphQL 服务器!

v1.0.1 2021-08-08 20:44 UTC

This package is auto-updated.

Last update: 2024-09-09 03:54:50 UTC


README

使用 Pest 以优雅的方式测试您的 GraphQL API!

安装

通过 Composer 简单安装!

composer require --dev miniaturebase/pest-plugin-graphql

新增内容是什么?

  • 将您的模式作为代码进行测试;
  • 断言 PSR-7 GraphQL 响应数据和错误;
  • 测试解析器(即将推出!);

期望

  • schema(string|Schema $document)
  • isValidSdl()
  • toHaveDirective(string $directive)
  • toHaveEnum(string $enum)
  • toHaveInput(string $input)
  • toHaveInterface(string $interface)
  • toHaveScalar(string $scalar)
  • toHaveType(string $type)
  • toHaveUnion(string $union)
  • toBeGraphQlResponse()
  • toHavePath(string $path, $value = null)
  • toHaveData(array $data)
  • toHaveErrors(array $errors)

更多功能即将推出!

schema(string|Schema $document)

使用 GraphQL\Type\Schema 实例作为底层值创建一个新的期望。

test('my schema')->schema();
it('is my schema')->schema();
Pest\GraphQl\schema();

您还可以提供替代的模式路径或文档内容,如下所示。

it('uses any schema you want')->schema(sprintf('%s/../app/schema.graphql', __DIR__));
test('inline content')->schema(<<<'GRAPHQL'
type Query {
    foo: Int
    bar: Int
}
GRAPHQL);
it('even uses your instance')->schema(AcmeSchemaBuilder::build());

isValidSdl()

断言模式有效且符合 GraphQL 规范。

it('validates your schema')->schema()->isValidSdl();

toHaveDirective(string $directive)

断言给定的指令定义存在于模式文档中。

it('has a directive')->schema()->toHaveDirective('auth')
it('will also use base classnames')->schema()->toHaveDirective(Auth::class);

toHaveEnum(string $enum)

断言给定的枚举定义存在于模式文档中。

it('has an enum')->schema()->toHaveEnum('Status')
it('will also use base classnames')->schema()->toHaveEnum(Status::class);

toHaveInput(string $input)

断言给定的输入定义存在于模式文档中。

it('has a input')->schema()->toHaveInput('Message')
it('will also use base classnames')->schema()->toHaveInput(Message::class);

toHaveInterface(string $interface)

断言给定的接口定义存在于模式文档中。

it('has a interface')->schema()->toHaveInterface('Notification')
it('will also use base classnames')->schema()->toHaveInterface(Notification::class);

toHaveScalar(string $scalar)

断言给定的标量定义存在于模式文档中。

it('has a scalar')->schema()->toHaveScalar('Date')
it('will also use base classnames')->schema()->toHaveScalar(Date::class);

toHaveType(string $type)

断言给定的(对象)类型已在模式文档中定义。

it('has mutations')->schema()->toHaveType('Mutation');
test('user defined types too')->schema()->toHaveType('User');
it('will also use your base classnames')->schema()->toHaveType(User::class);

toHaveUnion(string $union)

断言给定的联合定义存在于模式文档中。

it('has a union')->schema()->toHaveUnion('Character')
it('will also use your base classnames')->schema()->toHaveUnion(Character::class);

toBeGraphQlResponse()

断言底层(PSR-7)响应值符合 GraphQL 规范。

test('response validity')
    ->expect($response) // a Psr\Http\Message\ResponseInterface instance
    ->toBeGraphQlResponse();

toHavePath(string $path, $value = null)

断言底层 GraphQL 响应包含给定路径上的数据。可选地提供要检查的值!

it('reads paths')
    ->expect($response) // a Psr\Http\Message\ResponseInterface instance
    ->toHavePath('foo')
    ->toHavePath('foo', 1)
    ->not()
    ->toHavePath('foo.bar')
    ->not()
    ->toHavePath('foo', 0);

toHaveData(array $data)

断言底层响应 GraphQL 数据与预期数据规范上相等。

it('contains response data')
    ->expect($response) // a Psr\Http\Message\ResponseInterface instance
    ->toHaveData([
        'foo' => 1,
    ]);

toHaveErrors(array $errors)

断言底层响应 GraphQL 错误与预期错误集合规范上相等。

it('has errors')
    ->expect($response) // a Psr\Http\Message\ResponseInterface instance
    ->toHaveErrors([
        [
            'message'   => 'Oops, I did it again',
            'locations' => [['line' => 1, 'column' => 5]],
            'path'      => ['foo'],
        ],
    ]);

此存储库基于 Pest 插件模板。

Pest 由 Nuno Maduro 创建,并遵循 Sponsorware 许可协议。它已被开源,现在采用 MIT 许可协议