zestic / pest-plugin-graphql
使用 Pest 以优雅的方式测试您的 PHP GraphQL 服务器!
v0.1.0
2024-01-07 21:16 UTC
Requires
- php: ^8.1
- pestphp/pest: ^2.0
- pestphp/pest-plugin: ^2.0.1
- psr/http-message: ^1.0
- webonyx/graphql-php: >=15
- zestic/graphql-simple-client: ^0.1.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.6
- pestphp/pest-dev-tools: ^2.16.0
README
使用 Pest 以优雅的方式测试您的 GraphQL API!
安装
通过 Composer 简单安装!
composer require --dev zestic/pest-plugin-graphql
在您的 .env
文件中,设置测试 URL
TEST_GRAPHQL_URL=http://localhost
为了组织您的测试,在您的 tests
目录中创建一个 Api
目录。
确保在 composer.json
文件中正确设置了命名空间。
"autoload-dev": { "psr-4": { "Tests\\": "tests/" } }
最后,在您的 Pest.php 文件中添加以下行
uses(Pest\GraphQl\ApiTestCase::class)->in('Api');
新增功能
- 将测试模式作为代码进行测试;
- 断言 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 许可证 授予许可。