Skip to content

Commit

Permalink
Improve MorphToDirectiveTest.php (#2109)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Apr 7, 2022
1 parent fdbf068 commit c0acd3a
Showing 1 changed file with 61 additions and 56 deletions.
117 changes: 61 additions & 56 deletions tests/Integration/Schema/Directives/MorphToDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,27 @@
use Tests\Utils\Models\Task;
use Tests\Utils\Models\User;

class MorphToDirectiveTest extends DBTestCase
final class MorphToDirectiveTest extends DBTestCase
{
/**
* The authenticated user.
*
* @var \Tests\Utils\Models\User
*/
protected $user;

/**
* User's task.
*
* @var \Tests\Utils\Models\Task
*/
protected $task;

/**
* Task's image.
*
* @var \Tests\Utils\Models\Image
*/
protected $image;

public function setUp(): void
public function testResolveMorphToRelationship(): void
{
parent::setUp();
$user = factory(User::class)->create();
assert($user instanceof User);

$this->user = factory(User::class)->create();
$this->task = factory(Task::class)->create([
'user_id' => $this->user->id,
]);
$this->image = $this->task->images()
->save(
factory(Image::class)->create()
);
}
$task = factory(Task::class)->make();
assert($task instanceof Task);
$task->user()->associate($user);
$task->save();

$image = factory(Image::class)->make();
assert($image instanceof Image);
$image->imageable()->associate($task);
$image->save();

public function testResolveMorphToRelationship(): void
{
$this->schema = /** @lang GraphQL */ '
type Image {
id: ID!
imageable: Task! @morphTo(relation: "imageable")
imageable: Task! @morphTo
}
type Task {
Expand All @@ -76,14 +54,14 @@ public function testResolveMorphToRelationship(): void
}
}
', [
'id' => $this->image->id,
'id' => $image->id,
])->assertJson([
'data' => [
'image' => [
'id' => $this->image->id,
'id' => $image->id,
'imageable' => [
'id' => $this->task->id,
'name' => $this->task->name,
'id' => $task->id,
'name' => $task->name,
],
],
],
Expand All @@ -92,6 +70,19 @@ public function testResolveMorphToRelationship(): void

public function testResolveMorphToWithCustomName(): void
{
$user = factory(User::class)->create();
assert($user instanceof User);

$task = factory(Task::class)->make();
assert($task instanceof Task);
$task->user()->associate($user);
$task->save();

$image = factory(Image::class)->make();
assert($image instanceof Image);
$image->imageable()->associate($task);
$image->save();

$this->schema = /** @lang GraphQL */ '
type Image {
id: ID!
Expand Down Expand Up @@ -121,14 +112,14 @@ public function testResolveMorphToWithCustomName(): void
}
}
', [
'id' => $this->image->id,
'id' => $image->id,
])->assertJson([
'data' => [
'image' => [
'id' => $this->image->id,
'id' => $image->id,
'customImageable' => [
'id' => $this->task->id,
'name' => $this->task->name,
'id' => $task->id,
'name' => $task->name,
],
],
],
Expand All @@ -137,14 +128,28 @@ public function testResolveMorphToWithCustomName(): void

public function testResolveMorphToUsingInterfaces(): void
{
/** @var \Tests\Utils\Models\Post $post */
$post = factory(Post::class)->create([
'user_id' => $this->user->id,
]);
/** @var \Tests\Utils\Models\Image $postImage */
$postImage = $post->images()->save(
factory(Image::class)->create()
);
$user = factory(User::class)->create();
assert($user instanceof User);

$task = factory(Task::class)->make();
assert($task instanceof Task);
$task->user()->associate($user);
$task->save();

$image = factory(Image::class)->make();
assert($image instanceof Image);
$image->imageable()->associate($task);
$image->save();

$post = factory(Post::class)->make();
assert($post instanceof Post);
$post->user()->associate($user->id);
$post->save();

$postImage = factory(Image::class)->make();
assert($postImage instanceof Image);
$postImage->imageable()->associate($post);
$postImage->save();

$this->schema = /** @lang GraphQL */ '
interface Imageable {
Expand Down Expand Up @@ -203,15 +208,15 @@ interface Imageable {
}
}
', [
'taskImage' => $this->image->id,
'taskImage' => $image->id,
'postImage' => $postImage->id,
])->assertJson([
'data' => [
'taskImage' => [
'id' => $this->image->id,
'id' => $image->id,
'imageable' => [
'id' => $this->task->id,
'name' => $this->task->name,
'id' => $task->id,
'name' => $task->name,
],
],
'postImage' => [
Expand Down

0 comments on commit c0acd3a

Please sign in to comment.