From 8e2e819f6583a5bbca156f68cf67267885bc8f95 Mon Sep 17 00:00:00 2001 From: Christopher Moore Date: Mon, 19 Mar 2018 21:48:49 -0700 Subject: [PATCH] fix tests for deferred fields --- .../Directives/Fields/BelongsToTest.php | 47 +++++----- .../Fields/HasManyDirectiveTest.php | 92 ++++++------------- tests/TestCase.php | 19 ++++ tests/Utils/Models/Company.php | 11 +++ tests/Utils/Models/User.php | 5 + tests/database/factories/CompanyFactory.php | 11 +++ tests/database/factories/UserFactory.php | 4 + ...00000_create_testbench_companies_table.php | 28 ++++++ ...28_000001_create_testbench_users_table.php | 3 +- 9 files changed, 134 insertions(+), 86 deletions(-) create mode 100644 tests/Utils/Models/Company.php create mode 100644 tests/database/factories/CompanyFactory.php create mode 100644 tests/database/migrations/2018_02_28_000000_create_testbench_companies_table.php diff --git a/tests/Integration/Schema/Directives/Fields/BelongsToTest.php b/tests/Integration/Schema/Directives/Fields/BelongsToTest.php index 543c74a7dd..cad23a8fd8 100644 --- a/tests/Integration/Schema/Directives/Fields/BelongsToTest.php +++ b/tests/Integration/Schema/Directives/Fields/BelongsToTest.php @@ -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 @@ -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. @@ -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(), ]); } @@ -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')); } /** @@ -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')); } } diff --git a/tests/Integration/Schema/Directives/Fields/HasManyDirectiveTest.php b/tests/Integration/Schema/Directives/Fields/HasManyDirectiveTest.php index b67fcf4dec..4e8ad24b6c 100644 --- a/tests/Integration/Schema/Directives/Fields/HasManyDirectiveTest.php +++ b/tests/Integration/Schema/Directives/Fields/HasManyDirectiveTest.php @@ -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')); } /** @@ -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')); } /** @@ -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')); } /** diff --git a/tests/TestCase.php b/tests/TestCase.php index 1a82bd120c..f9a940fb64 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Tests; +use GraphQL\Executor\Executor; use GraphQL\Language\Parser; use Orchestra\Testbench\TestCase as BaseTestCase; @@ -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. * diff --git a/tests/Utils/Models/Company.php b/tests/Utils/Models/Company.php new file mode 100644 index 0000000000..262c1b1396 --- /dev/null +++ b/tests/Utils/Models/Company.php @@ -0,0 +1,11 @@ +belongsTo(Company::class); + } + public function tasks() { return $this->hasMany(Task::class); diff --git a/tests/database/factories/CompanyFactory.php b/tests/database/factories/CompanyFactory.php new file mode 100644 index 0000000000..fe73de919a --- /dev/null +++ b/tests/database/factories/CompanyFactory.php @@ -0,0 +1,11 @@ +define(Tests\Utils\Models\Company::class, function (Faker $faker) { + return [ + 'name' => $faker->sentence, + 'created_at' => now(), + 'updated_at' => now(), + ]; +}); diff --git a/tests/database/factories/UserFactory.php b/tests/database/factories/UserFactory.php index 13c7d830e1..341d6dce28 100644 --- a/tests/database/factories/UserFactory.php +++ b/tests/database/factories/UserFactory.php @@ -1,9 +1,13 @@ 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 diff --git a/tests/database/migrations/2018_02_28_000000_create_testbench_companies_table.php b/tests/database/migrations/2018_02_28_000000_create_testbench_companies_table.php new file mode 100644 index 0000000000..4c3a13a705 --- /dev/null +++ b/tests/database/migrations/2018_02_28_000000_create_testbench_companies_table.php @@ -0,0 +1,28 @@ +increments('id'); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down() + { + Schema::drop('companies'); + } +} diff --git a/tests/database/migrations/2018_02_28_000001_create_testbench_users_table.php b/tests/database/migrations/2018_02_28_000001_create_testbench_users_table.php index 3f0a66bb42..d8215d48c5 100644 --- a/tests/database/migrations/2018_02_28_000001_create_testbench_users_table.php +++ b/tests/database/migrations/2018_02_28_000001_create_testbench_users_table.php @@ -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. @@ -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');