Skip to content

Commit

Permalink
fix tests for deferred fields
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissm79 committed Mar 20, 2018
1 parent eef99ab commit 8e2e819
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 86 deletions.
47 changes: 25 additions & 22 deletions tests/Integration/Schema/Directives/Fields/BelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\DBTestCase;
use Tests\Utils\Models\Task;
use Tests\Utils\Models\Company;
use Tests\Utils\Models\User;

class BelongsToTest extends DBTestCase
Expand All @@ -19,11 +19,11 @@ class BelongsToTest extends DBTestCase
protected $user;

/**
* User's tasks.
* User's company.
*
* @var Task
* @var Company
*/
protected $task;
protected $company;

/**
* Setup test environment.
Expand All @@ -32,9 +32,9 @@ protected function setUp()
{
parent::setUp();

$this->user = factory(User::class)->create();
$this->task = factory(Task::class)->create([
'user_id' => $this->user->getKey(),
$this->company = factory(Company::class)->create();
$this->user = factory(User::class)->create([
'company_id' => $this->company->getKey(),
]);
}

Expand All @@ -44,19 +44,21 @@ protected function setUp()
public function itCanResolveBelongsToRelationship()
{
$schema = '
type Task {
user: User! @belongsTo
type Company {
name: String!
}
type User {
foo: String!
company: Company @belongsTo
}
type Query {
user: User @auth
}
';

$type = schema()->register($schema)->first();
$resolver = array_get($type->config['fields'](), 'user.resolve');
$user = $resolver($this->task, []);
$this->be($this->user);

$this->assertInstanceOf(User::class, $user);
$result = $this->execute($schema, '{ user { company { name } } }');
$this->assertEquals($this->company->name, array_get($result->data, 'user.company.name'));
}

/**
Expand All @@ -65,18 +67,19 @@ public function itCanResolveBelongsToRelationship()
public function itCanResolveBelongsToWithCustomName()
{
$schema = '
type Task {
bar: User! @belongsTo(relation:"user")
type Company {
name: String!
}
type User {
foo: String!
account: Company @belongsTo(relation: "company")
}
type Query {
user: User @auth
}
';

$type = schema()->register($schema)->first();
$resolver = array_get($type->config['fields'](), 'bar.resolve');
$user = $resolver($this->task, []);

$this->assertInstanceOf(User::class, $user);
$this->be($this->user);
$result = $this->execute($schema, '{ user { account { name } } }');
$this->assertEquals($this->company->name, array_get($result->data, 'user.account.name'));
}
}
92 changes: 29 additions & 63 deletions tests/Integration/Schema/Directives/Fields/HasManyDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ public function itCanQueryHasManyRelationship()
type Task {
foo: String
}
type Query {
user: User @auth
}
';

$type = schema()->register($schema)->first();
$resolver = array_get($type->config['fields'](), 'tasks.resolve');
$tasks = $resolver($this->user, []);
$this->be($this->user);

$this->assertCount(3, $tasks);
$result = $this->execute($schema, '{ user { tasks { id } } }');

$this->assertCount(3, array_get($result->data, 'user.tasks'));
}

/**
Expand All @@ -74,38 +77,21 @@ public function itCanQueryHasManyPaginator()
tasks: [Task!]! @hasMany(type:"paginator")
}
type Task {
foo: String
id: Int!
}
type Query {
user: User @auth
}
';

// Need lighthouse schema to resolve PaginatorInfo type
$types = schema()->register((new SchemaStitcher())->lighthouseSchema()."\n".$schema);
$root = $types->first(function ($root) {
return 'User' === $root->name;
});
$root->config['fields']();

$paginator = collect(schema()->types())->first(function ($type) {
return 'UserTaskPaginator' === $type->name;
});

$resolver = array_get($root->config['fields'](), 'tasks.resolve');
$tasks = $resolver($this->user, ['count' => 2]);

$this->assertInstanceOf(LengthAwarePaginator::class, $tasks);
$this->assertEquals(2, $tasks->count());
$this->assertEquals(3, $tasks->total());
$this->assertTrue($tasks->hasMorePages());

$resolver = array_get($paginator->config['fields'](), 'data.resolve');
$data = $resolver($tasks, []);
$this->assertCount(2, $data);

$resolver = array_get($paginator->config['fields'](), 'paginatorInfo.resolve');
$pageInfo = $resolver($tasks, []);
$this->assertTrue($pageInfo['hasMorePages']);
$this->assertEquals(1, $pageInfo['currentPage']);
$this->assertEquals(2, $pageInfo['perPage']);
$result = $this->execute($schema, '
{ user { tasks(count: 2) { paginatorInfo { total count hasMorePages } data { id } } } }
', true);

$this->assertEquals(2, array_get($result->data, 'user.tasks.paginatorInfo.count'));
$this->assertEquals(3, array_get($result->data, 'user.tasks.paginatorInfo.total'));
$this->assertTrue(array_get($result->data, 'user.tasks.paginatorInfo.hasMorePages'));
$this->assertCount(2, array_get($result->data, 'user.tasks.data'));
}

/**
Expand All @@ -118,39 +104,19 @@ public function itCanQueryHasManyRelayConnection()
tasks: [Task!]! @hasMany(type:"relay")
}
type Task {
foo: String
id: Int!
}
type Query {
user: User @auth
}
';

// Need lighthouse schema to resolve PageInfo type
$types = schema()->register((new SchemaStitcher())->lighthouseSchema()."\n".$schema);
$root = $types->first(function ($root) {
return 'User' === $root->name;
});
$root->config['fields']();

$connection = collect(schema()->types())->first(function ($type) {
return 'UserTaskConnection' === $type->name;
});

$resolver = array_get($root->config['fields'](), 'tasks.resolve');
$tasks = $resolver($this->user, ['first' => 2]);

$this->assertInstanceOf(LengthAwarePaginator::class, $tasks);
$this->assertEquals(2, $tasks->count());
$this->assertEquals(3, $tasks->total());
$this->assertTrue($tasks->hasMorePages());

$resolver = array_get($connection->config['fields'](), 'edges.resolve');
$edges = $resolver($tasks, []);
$this->assertCount(2, $edges);

$resolver = array_get($connection->config['fields'](), 'pageInfo.resolve');
$pageInfo = $resolver($tasks, []);
$this->assertEquals($edges->first()['cursor'], $pageInfo['startCursor']);
$this->assertEquals($edges->last()['cursor'], $pageInfo['endCursor']);
$this->assertTrue($pageInfo['hasNextPage']);
$this->assertFalse($pageInfo['hasPreviousPage']);
$result = $this->execute($schema, '
{ user { tasks(first: 2) { pageInfo { hasNextPage } edges { node { id } } } } }
', true);

$this->assertTrue(array_get($result->data, 'user.tasks.pageInfo.hasNextPage'));
$this->assertCount(2, array_get($result->data, 'user.tasks.edges'));
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use GraphQL\Executor\Executor;
use GraphQL\Language\Parser;
use Orchestra\Testbench\TestCase as BaseTestCase;

Expand Down Expand Up @@ -95,6 +96,24 @@ protected function parse(string $schema)
return Parser::parse($schema);
}

/**
* Execute query/mutation.
*
* @param string $schema
* @param string $query
* @param string $lighthouse
*
* @return \GraphQL\Executor\ExecutionResult
*/
protected function execute($schema, $query, $lighthouse = false)
{
$schema = $lighthouse
? file_get_contents(realpath(__DIR__.'/../assets/schema.graphql'))."\n".$schema
: $schema;

return Executor::execute(schema()->build($schema), $this->parse($query));
}

/**
* Store file contents.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Utils/Models/Company.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tests\Utils\Models;

use Illuminate\Database\Eloquent\Model;
use Nuwave\Lighthouse\Support\Traits\IsRelayConnection;

class Company extends Model
{
use IsRelayConnection;
}
5 changes: 5 additions & 0 deletions tests/Utils/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class User extends Authenticatable
{
use IsRelayConnection;

public function company()
{
return $this->belongsTo(Company::class);
}

public function tasks()
{
return $this->hasMany(Task::class);
Expand Down
11 changes: 11 additions & 0 deletions tests/database/factories/CompanyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Faker\Generator as Faker;

$factory->define(Tests\Utils\Models\Company::class, function (Faker $faker) {
return [
'name' => $faker->sentence,
'created_at' => now(),
'updated_at' => now(),
];
});
4 changes: 4 additions & 0 deletions tests/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

use Faker\Generator as Faker;
use Tests\Utils\Models\Company;

$factory->define(Tests\Utils\Models\User::class, function (Faker $faker) {
return [
'company_id' => function () {
return factory(Company::class)->create()->getKey();
},
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestbenchUsersTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::drop('companies');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestbenchUsersTable extends Migration
class CreateTestbenchCompaniesTable extends Migration
{
/**
* Run the migrations.
Expand All @@ -13,6 +13,7 @@ public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('company_id');
$table->string('name');
$table->string('email');
$table->string('password');
Expand Down

0 comments on commit 8e2e819

Please sign in to comment.