Download the PHP package newman/laravel-graphql-test-utils without Composer
On this page you can find all versions of the php package newman/laravel-graphql-test-utils. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-graphql-test-utils
Laravel GraphQL testing utility
This package helps you to test your GraphQL queries & mutations in your TestCases. It works with any laravel GraphQL server.
Requirements
- Laravel 8.12+, 9.0+, 10.0+, 11.0
- PHP 8.0+
Installation
Require the package via Composer:
:book: Documentation & Usage
To start using it, import our trait in your TestCase
Now you can explore our available builder methods.
Default assertions
By default, we don't assert anything for you after response is retrieved, but we expose function to register your handler to customize this behaviour.
Builder methods
setQuery
Set your GraphQL query.
setVariables
Set multiple GraphQL variables as key-value pair.
Note: Calling setVariables
, will replace all previously set variables. You may want to use mergeVariables
in that case instead.
setVariable
Set variables one-by-one.
You can also mix it with setVariables
and mergeVariables
.
Note: Calling setVariable
with the same key will override previous value.
mergeVariables
Instead of resetting all variables like setVariables
does, this method merges previously set variables with a pair of new variables.
schema
Specifies which GraphQL schema (name) to use.
Note: Depending on the driver used, it may automatically resolve HTTP method to use based on schema. More.
httpMethod
Forces specific HTTP method to use. By default, it will resolve from schema (if driver provides that), otherwise POST
will be used.
driver
Optionally you can switch this query to other driver than default. Probably you won't need this.
withToken
Adds an Bearer Authorization token to request.
withoutToken
Remove the authorization token from the request.
withHeader
Add single header to request.
withHeader
Add multiple headers to request.
modifyRequest
Since in base we use Laravel`s MakesHttpRequests
Trait, you can access those functions as well.
withDefaultAssertions
and withoutDefaultAssertions
You may want to disable or re-enable default assertions individually.
Disable default assertions for this query:
call
Makes the call to GraphQL and returns our TestResponse
class. Function has 2 arguments
Note: Variables passed to this function will merge with existing ones.
Response
We extend default Laravel`s Illuminate\Testing\TestResponse
with a few GraphQL related functions/assertions.
assertNoGraphQLErrors
Asserts no GraphQL errors were returned.
assertGraphQLUnauthorized
Asserts there is an Unauthorized error in corresponding GraphQL error format.
getQuery
Access used query in the request.
getVariables
Access used variables in the request.
getGraphQLErrors
Returns array
of GraphQL errors or null
when not present.
hasGraphQLErrors
Determines if there are any GraphQL errors.
getGraphQLValidationMessages
Get list of Laravel validation messages or empty array when none.
getValidationFieldMessages
Get Laravel validation messages on specific field or empty array when none or field is not present.
getValidationFieldFirstMessage
Get first Laravel validation message on specific field or null when none or field is not present.
:information_source: Remember you can access all Illuminate\Testing\TestResponse
functions as well.
:question: Missing frequent response helper? Open up a new issue.
Custom Response class
This example shows you how to create your custom TestResponse class with your custom functions.
Then in your ServiceProvider (e.g. AppServiceProvider
) or TestCase:
Custom Builder class
This example shows you how to create your custom GraphQLBuilder class with your custom functions.
Then in your ServiceProvider (e.g. AppServiceProvider
):
Drivers
Drivers help you to construct request by pulling information from GraphQL server config, so you can discard some builder calls. You can write your custom driver if none of ours fits your needs.
RebingDriver (Default)
https://github.com/rebing/graphql-laravel
It reads URL prefix from config graphql.route.prefix
and takes first HTTP method from config by schema name.
This is the default driver.
NullDriver
It uses /graphql
URL prefix and POST
HTTP Method.
To use, call GraphQLTesting::useNullDriver()
in your AppServiceProvider.php
boot()
method.
Custom driver
Example on how to implement custom driver with name myCustom
.
Then in your ServiceProvider (e.g. AppServiceProvider
):
:handshake: Contributing
We'll appreciate your collaboration to this package.
When making pull requests, make sure:
- All tests are passing:
composer test
- Test coverage is not reduced:
composer test-coverage
- There are no PHPStan errors:
composer phpstan
- Coding standard is followed:
composer lint
orcomposer fix-style
to automatically fix it.
All versions of laravel-graphql-test-utils with dependencies
illuminate/contracts Version ^8.12|^9.0|^10.0|^11.0
illuminate/support Version ^8.12|^9.0|^10.0|^11.0
illuminate/testing Version ^8.12|^9.0|^10.0|^11.0