Skip to content

Commit

Permalink
Merge pull request nuwave#802 from nuwave/extract-test-helpers
Browse files Browse the repository at this point in the history
Extract test helpers into a reusable trait
  • Loading branch information
chrissm79 authored May 26, 2019
2 parents 485a4ab + 3943d07 commit 507de47
Show file tree
Hide file tree
Showing 66 changed files with 353 additions and 284 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add compatibility layer to allow `@middleware` to support Lumen https://github.com/nuwave/lighthouse/pull/786
- Add option `decode` to `@globaldId` to control the result of decoding https://github.com/nuwave/lighthouse/pull/796
- Add config option `cache.ttl` for customizing expiration time of schema cache https://github.com/nuwave/lighthouse/pull/801
- Extract test helpers into a reusable trait `\Nuwave\Lighthouse\Testing\MakesGraphQLRequests` https://github.com/nuwave/lighthouse/pull/802
- Support custom rule classes in `@rules` and `@rulesForArray` https://github.com/nuwave/lighthouse/pull/812

### Fixed
Expand Down
7 changes: 5 additions & 2 deletions src/Support/Http/routes.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php

app('router')->group(config('lighthouse.route', []), function (): void {
/** @var \Illuminate\Contracts\Routing\Registrar $router */
$router = app('router');

$router->group(config('lighthouse.route', []), function () use ($router): void {
$routeName = config('lighthouse.route_name', 'graphql');
$controller = config('lighthouse.controller');

$methods = config('lighthouse.route_enable_get', false)
? ['GET', 'POST']
: ['POST'];

app('router')->match($methods, $routeName, [
$router->match($methods, $routeName, [
'as' => 'lighthouse.graphql',
'uses' => $controller,
]);
Expand Down
117 changes: 117 additions & 0 deletions src/Testing/MakesGraphQLRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Nuwave\Lighthouse\Testing;

use Illuminate\Foundation\Testing\TestResponse;

/**
* Useful helpers for PHPUnit testing.
*
* It depends upon methods defined in
* @see \Illuminate\Foundation\Testing\Concerns\MakesHttpRequests
*/
trait MakesGraphQLRequests
{
/**
* Visit the given URI with a POST request, expecting a JSON response.
*
* @param string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Foundation\Testing\TestResponse
*/
abstract public function postJson($uri, array $data = [], array $headers = []);

/**
* Call the given URI and return the Response.
*
* @param string $method
* @param string $uri
* @param array $parameters
* @param array $cookies
* @param array $files
* @param array $server
* @param string $content
* @return \Illuminate\Foundation\Testing\TestResponse
*/
abstract public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null);

/**
* Transform headers array to array of $_SERVER vars with HTTP_* format.
*
* @param array $headers
* @return array
*/
abstract protected function transformHeadersToServerVars(array $headers);

/**
* Execute a query as if it was sent as a request to the server.
*
* @param string $query
* @return \Illuminate\Foundation\Testing\TestResponse
*/
protected function graphQL(string $query): TestResponse
{
return $this->postGraphQL(
[
'query' => $query,
]
);
}

/**
* Execute a query as if it was sent as a request to the server.
*
* @param mixed[] $data
* @param mixed[] $headers
* @return \Illuminate\Foundation\Testing\TestResponse
*/
protected function postGraphQL(array $data, array $headers = []): TestResponse
{
return $this->postJson(
$this->graphQLEndpointUrl(),
$data,
$headers
);
}

/**
* Send a multipart form request to GraphQL.
*
* This is used for file uploads conforming to the specification:
* https://github.com/jaydenseric/graphql-multipart-request-spec
*
* @param mixed[] $parameters
* @param mixed[] $files
* @return \Illuminate\Foundation\Testing\TestResponse
*/
protected function multipartGraphQL(array $parameters, array $files): TestResponse
{
return $this->call(
'POST',
$this->graphQLEndpointUrl(),
$parameters,
[],
$files,
$this->transformHeadersToServerVars([
'Content-Type' => 'multipart/form-data',
])
);
}

/**
* Return the full URL to the GraphQL endpoint.
*
* @return string
*/
private function graphQLEndpointUrl(): string
{
$path = config('lighthouse.route_name');

if ($prefix = config('lighthouse.route.prefix')) {
$path = $prefix.$path;
}

return $path;
}
}
2 changes: 1 addition & 1 deletion tests/Integration/CustomDefaultResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function itCanSpecifyACustomDefaultResolver(): void
return self::CUSTOM_RESOLVER_RESULT;
});

$this->query('
$this->graphQL('
{
foo {
bar
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Defer/DeferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public function itThrowsExceptionOnNunNullableFields(): void
}
";

$this->query('
$this->graphQL('
{
user {
name
Expand Down Expand Up @@ -471,7 +471,7 @@ public function itSkipsDeferWithIncludeAndSkipDirectives(): void
}
";

$this->query('
$this->graphQL('
{
user {
name
Expand Down Expand Up @@ -518,7 +518,7 @@ public function itRequiresDeferDirectiveOnAllFieldDeclarations(): void
}
";

$this->query('
$this->graphQL('
fragment UserWithParent on User {
name
parent {
Expand Down Expand Up @@ -574,7 +574,7 @@ public function itSkipsDeferredFieldsOnMutations(): void
}
";

$this->query('
$this->graphQL('
mutation UpdateUser {
updateUser(name: "John Doe") {
name
Expand Down Expand Up @@ -613,7 +613,7 @@ public function itDoesNotDeferFieldsIfFalse(): void
}
";

$this->query('
$this->graphQL('
{
user {
name
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Defer/SetUpDefer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function setUpDefer($app)
*/
protected function getStreamedChunks(string $query): array
{
$this->query($query)
$this->graphQL($query)
->baseResponse
->send();

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Events/BuildSchemaStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function (BuildSchemaString $buildSchemaString): string {
foo
}
';
$this->query($queryForBaseSchema)->assertJson([
$this->graphQL($queryForBaseSchema)->assertJson([
'data' => [
'foo' => 'foo',
],
Expand All @@ -62,7 +62,7 @@ function (BuildSchemaString $buildSchemaString): string {
sayHello
}
';
$this->query($queryForAdditionalSchema)->assertJson([
$this->graphQL($queryForAdditionalSchema)->assertJson([
'data' => [
'sayHello' => 'hello',
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Events/ManipulateResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function (ManipulateResult $manipulateResult): void {
}
);

$this->query('
$this->graphQL('
{
foo
}
Expand Down
24 changes: 12 additions & 12 deletions tests/Integration/Execution/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function itCanAttachEqFilterToQuery(): void
}
';

$this->query('
$this->graphQL('
{
users(id: '.$this->users->first()->getKey().') {
id
Expand All @@ -71,7 +71,7 @@ public function itCanAttachEqFilterFromInputObject(): void
}
';

$this->query('
$this->graphQL('
{
users(
input: {
Expand All @@ -95,7 +95,7 @@ public function itCanAttachNeqFilterToQuery(): void
}
';

$this->query('
$this->graphQL('
{
users(id: '.$this->users->first()->getKey().') {
id
Expand All @@ -118,7 +118,7 @@ public function itCanAttachInFilterToQuery(): void
$user1 = $this->users->first()->getKey();
$user2 = $this->users->last()->getKey();

$this->query('
$this->graphQL('
{
users(include: ['.$user1.', '.$user2.']) {
id
Expand All @@ -141,7 +141,7 @@ public function itCanAttachNotInFilterToQuery(): void
$user1 = $this->users->first()->getKey();
$user2 = $this->users->last()->getKey();

$this->query('
$this->graphQL('
{
users(exclude: ['.$user1.', '.$user2.']) {
id
Expand All @@ -163,7 +163,7 @@ public function itCanAttachWhereFilterToQuery(): void

$user1 = $this->users->first()->getKey();

$this->query('
$this->graphQL('
{
users(id: '.$user1.') {
id
Expand All @@ -189,7 +189,7 @@ public function itCanAttachTwoWhereFilterWithTheSameKeyToQuery(): void
$user1 = $this->users->first()->getKey();
$user2 = $this->users->last()->getKey();

$this->query('
$this->graphQL('
{
users(start: '.$user1.' end: '.$user2.') {
id
Expand Down Expand Up @@ -222,7 +222,7 @@ public function itCanAttachWhereBetweenFilterToQuery(): void
$start = now()->subDay()->startOfDay()->format('Y-m-d H:i:s');
$end = now()->subDay()->endOfDay()->format('Y-m-d H:i:s');

$this->query('
$this->graphQL('
{
users(
createdBetween: ["'.$start.'", "'.$end.'"]
Expand Down Expand Up @@ -262,7 +262,7 @@ public function itCanUseInputObjectsForWhereBetweenFilter(): void
$start = now()->subDay()->startOfDay()->format('Y-m-d H:i:s');
$end = now()->subDay()->endOfDay()->format('Y-m-d H:i:s');

$this->query('
$this->graphQL('
{
users(
created: {
Expand Down Expand Up @@ -300,7 +300,7 @@ public function itCanAttachWhereNotBetweenFilterToQuery(): void
$start = now()->subDay()->startOfDay()->format('Y-m-d H:i:s');
$end = now()->subDay()->endOfDay()->format('Y-m-d H:i:s');

$this->query('
$this->graphQL('
{
users(
notCreatedBetween: ["'.$start.'", "'.$end.'"]
Expand Down Expand Up @@ -334,7 +334,7 @@ public function itCanAttachWhereClauseFilterToQuery(): void

$year = now()->subYear()->format('Y');

$this->query('
$this->graphQL('
{
users(created_at: "'.$year.'") {
id
Expand All @@ -357,7 +357,7 @@ public function itOnlyProcessesFilledArguments(): void
}
';

$this->query('
$this->graphQL('
{
users(name: "'.$this->users->first()->name.'") {
id
Expand Down
Loading

0 comments on commit 507de47

Please sign in to comment.