Skip to content

Commit

Permalink
Test BelongsTo relation to itself
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed May 16, 2020
1 parent c3fe556 commit 3d27a3a
Showing 1 changed file with 70 additions and 19 deletions.
89 changes: 70 additions & 19 deletions tests/Integration/Schema/Directives/BelongsToDirectiveTest.php
Expand Up @@ -4,6 +4,7 @@

use Tests\DBTestCase;
use Tests\Utils\Models\Company;
use Tests\Utils\Models\Post;
use Tests\Utils\Models\Product;
use Tests\Utils\Models\Team;
use Tests\Utils\Models\User;
Expand Down Expand Up @@ -47,21 +48,21 @@ public function testCanResolveBelongsToRelationship(): void
{
$this->be($this->user);

$this->schema = '
$this->schema = /** @lang GraphQL */ '
type Company {
name: String!
}
type User {
company: Company @belongsTo
}
type Query {
user: User @auth
}
';

$this->graphQL('
$this->graphQL(/** @lang GraphQL */ '
{
user {
company {
Expand All @@ -84,21 +85,21 @@ public function testCanResolveBelongsToWithCustomName(): void
{
$this->be($this->user);

$this->schema = '
$this->schema = /** @lang GraphQL */ '
type Company {
name: String!
}
type User {
account: Company @belongsTo(relation: "company")
}
type Query {
user: User @auth
}
';

$this->graphQL('
$this->graphQL(/** @lang GraphQL */ '
{
user {
account {
Expand All @@ -121,26 +122,26 @@ public function testCanResolveBelongsToRelationshipWithTwoRelation(): void
{
$this->be($this->user);

$this->schema = '
$this->schema = /** @lang GraphQL */ '
type Company {
name: String!
}
type Team {
name: String!
}
type User {
company: Company @belongsTo
team: Team @belongsTo
}
type Query {
user: User @auth
}
';

$this->graphQL('
$this->graphQL(/** @lang GraphQL */ '
{
user {
company {
Expand Down Expand Up @@ -171,28 +172,28 @@ public function testCanResolveBelongsToRelationshipWhenMainModelHasCompositePrim

$products = factory(Product::class, 2)->create();

$this->schema = '
$this->schema = /** @lang GraphQL */ '
type Color {
id: ID!
name: String
}
type Product {
barcode: String!
uuid: String!
name: String!
color: Color @belongsTo
}
type Query {
products: [Product] @paginate
}
';

$this->graphQL('
$this->graphQL(/** @lang GraphQL */ '
{
products(first: 2) {
products(first: 2) {
data{
barcode
uuid
Expand All @@ -201,7 +202,7 @@ public function testCanResolveBelongsToRelationshipWhenMainModelHasCompositePrim
id
name
}
}
}
}
}
')->assertJson([
Expand All @@ -223,4 +224,54 @@ public function testCanResolveBelongsToRelationshipWhenMainModelHasCompositePrim
],
]);
}

public function testBelongsToItself(): void
{
/** @var \Tests\Utils\Models\Post $parent */
$parent = factory(Post::class)->create();

/** @var \Tests\Utils\Models\Post $child */
$child = factory(Post::class)->make();
$child->parent()->associate($parent);
$child->save();

$this->schema = /** @lang GraphQL */ '
type Post {
id: Int!
parent: Post @belongsTo
}
type Query {
posts: [Post!]! @all
}
';

$this
->graphQL(/** @lang GraphQL */ '
{
posts {
id
parent {
id
}
}
}
')
->assertJson([
'data' => [
'posts' => [
[
'id' => $parent->id,
'parent' => null,
],
[
'id' => $child->id,
'parent' => [
'id' => $parent->id,
]
]
]
]
]);
}
}

0 comments on commit 3d27a3a

Please sign in to comment.