lastdragon-ru/phpunit-graphql
PHPUnit helpers for testing GraphQL in PHP apps. Provides assertions and utilities to execute queries/mutations, validate responses, and compare errors/data structures, aimed at simplifying reliable GraphQL API test coverage.
Install the Package
composer require --dev lastdragon-ru/phpunit-graphql
Ensure lastdragon-ru/graphql-printer is also installed (required dependency):
composer require lastdragon-ru/graphql-printer
Basic Assertion Example
Use the GraphQLAssertions trait in your test class to validate GraphQL queries or schemas:
use LastDragonRu\PhpUnitGraphQL\GraphQLAssertions;
class GraphQLTest extends TestCase
{
use GraphQLAssertions;
public function testQueryMatchesExpected()
{
$query = <<<'GRAPHQL'
query {
user(id: 1) {
id
name
}
}
GRAPHQL;
$this->assertGraphQLQueryMatches($query, [
'user' => [
'id' => 1,
'name' => 'John Doe',
],
]);
}
}
Key Methods to Explore First
assertGraphQLQueryMatches(): Validate a query against expected output.assertGraphQLSchemaMatches(): Validate a schema against a reference.assertGraphQLDocumentMatches(): Compare two GraphQL documents (queries/fragments).Mock or Execute Queries
Use Laravel’s HTTP testing or a GraphQL client (e.g., webonyx/graphql-php) to fetch responses.
$response = $this->graphQL('query { user(id: 1) { name } }');
$actual = $response->getData();
Assert Structure
Combine with GraphQLAssertions to validate structure:
$this->assertGraphQLQueryMatches($query, $expectedData);
Schema Validation Test schema compliance during development:
$schema = new \GraphQL\Type\Schema([...]);
$this->assertGraphQLSchemaMatches($schema, $referenceSchema);
expect($query)->graphQL()->toMatch($expected);
$this->livewire(UpdateProfile::class)
->call('updateName', 'Alice')
->assertGraphQLMutationMatches('updateProfile', ['name' => 'Alice']);
Reuse assertions for fragments:
$fragment = <<<'GRAPHQL'
fragment UserFields on User {
id
name
}
GRAPHQL;
$this->assertGraphQLDocumentMatches($fragment, $referenceFragment);
Schema Mismatches
assertGraphQLSchemaMatches() fails if the schema lacks optional fields.strict: false or mock partial schemas:
$this->assertGraphQLSchemaMatches($schema, $reference, strict: false);
Whitespace Sensitivity
trim() or GraphQLPrinter::print() for consistency:
$this->assertGraphQLDocumentMatches(
trim($query),
trim($reference)
);
Dynamic Variables
$id: ID!) may fail if not mocked.GraphQLAssertions::withVariables():
$this->assertGraphQLQueryMatches($query, $expected, ['id' => 1]);
$this->assertGraphQLQueryMatches($query, $expected, debug: true);
assertGraphQLQueryContains() for loose validation:
$this->assertGraphQLQueryContains($query, 'user { id }');
Custom Printers
Override the default printer in config/graphql.php:
'printer' => \App\CustomGraphQLPrinter::class,
Custom Assertions Extend the trait for domain-specific rules:
use LastDragonRu\PhpUnitGraphQL\GraphQLAssertions;
trait CustomGraphQLAssertions {
protected function assertGraphQLHasPagination($query) {
$this->assertGraphQLQueryContains($query, 'pageInfo { ... }');
}
}
CI Optimization Cache schema references in CI to speed up tests:
static::boot(function () {
$schema = require __DIR__.'/schema.cache.php';
GraphQLAssertions::setReferenceSchema($schema);
});
How can I help you explore Laravel packages today?