Skip to content

Commit

Permalink
Clean up MutationExecutor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Oct 12, 2023
1 parent 7b774a8 commit 50b150f
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 211 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
->in(__DIR__)
->name('*.php')
->notPath('vendor')
->notPath('phpstan-tmp-dir')
->ignoreDotFiles(false)
->ignoreVCS(true);

Expand Down
138 changes: 80 additions & 58 deletions tests/Integration/Execution/MutationExecutor/BelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ public function testUpsertWithBelongsToManyOnNonExistentData(): void
],
]);

/** @var Role $role */
$role = Role::first();
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(2, $role->users()->get());
$this->assertSame('is_user', $role->name);
}
Expand Down Expand Up @@ -310,16 +310,23 @@ public function testUpsertBelongsToManyWithoutId(): void
],
]);

/** @var Role $role */
$role = Role::first();
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(2, $role->users()->get());
$this->assertSame('is_user', $role->name);
}

public function testCreateAndConnectWithBelongsToMany(): void
{
factory(User::class)->create(['name' => 'user_one']);
factory(User::class)->create(['name' => 'user_two']);
$user = factory(User::class)->make();
assert($user instanceof User);
$user->name = 'user_one';
$user->save();

$user = factory(User::class)->make();
assert($user instanceof User);
$user->name = 'user_two';
$user->save();

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -359,8 +366,15 @@ public function testCreateAndConnectWithBelongsToMany(): void

public function testUpsertUsingCreationAndConnectWithBelongsToMany(): void
{
factory(User::class)->create(['name' => 'user_one']);
factory(User::class)->create(['name' => 'user_two']);
$user = factory(User::class)->make();
assert($user instanceof User);
$user->name = 'user_one';
$user->save();

$user = factory(User::class)->make();
assert($user instanceof User);
$user->name = 'user_two';
$user->save();

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -401,9 +415,10 @@ public function testUpsertUsingCreationAndConnectWithBelongsToMany(): void

public function testCreateWithBelongsToMany(): void
{
factory(Role::class)->create([
'name' => 'is_admin',
]);
$role = factory(Role::class)->make();
assert($role instanceof Role);
$role->name = 'is_admin';
$role->save();

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -448,8 +463,8 @@ public function testCreateWithBelongsToMany(): void
],
]);

/** @var \Tests\Utils\Models\Role $role */
$role = Role::first();
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(2, $role->users()->get());
$this->assertSame('is_user', $role->name);
}
Expand Down Expand Up @@ -495,9 +510,10 @@ public function testAllowsNullOperations(): void

public function testUpsertUsingCreationWithBelongsToMany(): void
{
factory(Role::class)->create([
'name' => 'is_admin',
]);
$role = factory(Role::class)->make();
assert($role instanceof Role);
$role->name = 'is_admin';
$role->save();

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -544,8 +560,8 @@ public function testUpsertUsingCreationWithBelongsToMany(): void
],
]);

/** @var \Tests\Utils\Models\Role $role */
$role = Role::first();
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(2, $role->users()->get());
$this->assertSame('is_user', $role->name);
}
Expand All @@ -562,11 +578,12 @@ public static function existingModelMutations(): array
/** @dataProvider existingModelMutations */
public function testUpdateWithBelongsToMany(string $action): void
{
factory(Role::class)
->create([
'name' => 'is_admin',
])
->users()
$role = factory(Role::class)->make();
assert($role instanceof Role);
$role->name = 'is_admin';
$role->save();

$role->users()
->attach(
factory(User::class, 2)->create(),
);
Expand Down Expand Up @@ -595,8 +612,7 @@ public function testUpdateWithBelongsToMany(string $action): void
}
}
}
GRAPHQL
)->assertJson([
GRAPHQL)->assertJson([
'data' => [
"{$action}Role" => [
'id' => '1',
Expand All @@ -615,20 +631,21 @@ public function testUpdateWithBelongsToMany(string $action): void
],
]);

/** @var Role $role */
$role = Role::first();
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(2, $role->users()->get());
$this->assertSame('is_user', $role->name);
}

/** @dataProvider existingModelMutations */
public function testDeleteWithBelongsToMany(string $action): void
{
factory(Role::class)
->create([
'name' => 'is_admin',
])
->users()
$role = factory(Role::class)->make();
assert($role instanceof Role);
$role->name = 'is_admin';
$role->save();

$role->users()
->attach(
factory(User::class, 2)->create(),
);
Expand All @@ -649,8 +666,7 @@ public function testDeleteWithBelongsToMany(string $action): void
}
}
}
GRAPHQL
)->assertJson([
GRAPHQL)->assertJson([
'data' => [
"{$action}Role" => [
'id' => '1',
Expand All @@ -664,8 +680,8 @@ public function testDeleteWithBelongsToMany(string $action): void
],
]);

/** @var Role $role */
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(1, $role->users()->get());
$this->assertSame('is_user', $role->name);

Expand All @@ -677,9 +693,11 @@ public function testDeleteWithBelongsToMany(string $action): void
public function testConnectWithBelongsToMany(string $action): void
{
factory(User::class)->create();
factory(Role::class)
->create()
->users()

$role = factory(Role::class)->create();
assert($role instanceof Role);

$role->users()
->attach(
factory(User::class)->create(),
);
Expand All @@ -699,8 +717,7 @@ public function testConnectWithBelongsToMany(string $action): void
}
}
}
GRAPHQL
)->assertJson([
GRAPHQL)->assertJson([
'data' => [
"{$action}Role" => [
'id' => '1',
Expand All @@ -716,18 +733,19 @@ public function testConnectWithBelongsToMany(string $action): void
],
]);

/** @var Role $role */
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(2, $role->users()->get());
}

/** @dataProvider existingModelMutations */
public function testSyncWithBelongsToMany(string $action): void
{
factory(User::class)->create();
factory(Role::class)
->create()
->users()

$role = factory(Role::class)->create();
assert($role instanceof Role);
$role->users()
->attach(
factory(User::class)->create(),
);
Expand All @@ -747,8 +765,7 @@ public function testSyncWithBelongsToMany(string $action): void
}
}
}
GRAPHQL
)->assertJson([
GRAPHQL)->assertJson([
'data' => [
"{$action}Role" => [
'id' => '1',
Expand All @@ -764,17 +781,18 @@ public function testSyncWithBelongsToMany(string $action): void
],
]);

/** @var Role $role */
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(2, $role->users()->get());
}

/** @dataProvider existingModelMutations */
public function testDisconnectWithBelongsToMany(string $action): void
{
factory(Role::class)
->create()
->users()
$role = factory(Role::class)->create();
assert($role instanceof Role);

$role->users()
->attach(
factory(User::class, 2)->create(),
);
Expand All @@ -793,8 +811,7 @@ public function testDisconnectWithBelongsToMany(string $action): void
}
}
}
GRAPHQL
)->assertJson([
GRAPHQL)->assertJson([
'data' => [
"{$action}Role" => [
'id' => '1',
Expand All @@ -807,8 +824,8 @@ public function testDisconnectWithBelongsToMany(string $action): void
],
]);

/** @var Role $role */
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertCount(1, $role->users()->get());

$this->assertNotNull(User::find(1));
Expand Down Expand Up @@ -893,12 +910,12 @@ public function testSyncExistingUsersDuringCreateUsingUpsertToABelongsToManyRela
/** @dataProvider existingModelMutations */
public function testDisconnectAllRelatedModelsOnEmptySync(string $action): void
{
/** @var User $user */
$user = factory(User::class)->create();
/** @var Role $role */
$role = $user->roles()->save(
factory(Role::class)->make(),
);
assert($user instanceof User);

$role = factory(Role::class)->make();
assert($role instanceof Role);
$user->roles()->save($role);

$this->assertCount(1, $role->users);

Expand Down Expand Up @@ -934,7 +951,10 @@ public function testDisconnectAllRelatedModelsOnEmptySync(string $action): void
public function testConnectUserWithRoleAndPivotMetaByUsingSync(): void
{
$user = factory(User::class)->create();
assert($user instanceof User);

factory(Role::class)->create();

$role2 = factory(Role::class)->create();
$user->roles()->attach($role2);

Expand Down Expand Up @@ -991,6 +1011,8 @@ public function testConnectUserWithRoleAndPivotMetaByUsingSync(): void
public function testConnectUserWithRoleAndPivotMetaByUsingSyncWithoutDetach(): void
{
$user = factory(User::class)->create();
assert($user instanceof User);

factory(Role::class)->create();
$role2 = factory(Role::class)->create();
$user->roles()->attach($role2);
Expand Down
23 changes: 14 additions & 9 deletions tests/Integration/Execution/MutationExecutor/BelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,10 @@ public function testUpsertUsingCreateWithNewUpsertBelongsTo(): void

public function testCreateAndUpdateBelongsTo(): void
{
factory(User::class)->create([
'name' => 'foo',
]);
$user = factory(User::class)->make();
assert($user instanceof User);
$user->name = 'foo';
$user->save();

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -413,9 +414,10 @@ public function testCreateAndUpdateBelongsTo(): void

public function testUpsertUsingCreateAndUpdateBelongsTo(): void
{
factory(User::class)->create([
'name' => 'foo',
]);
$user = factory(User::class)->make();
assert($user instanceof User);
$user->name = 'foo';
$user->save();

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -453,9 +455,10 @@ public function testUpsertUsingCreateAndUpdateBelongsTo(): void

public function testUpsertUsingCreateAndUpdateUsingUpsertBelongsTo(): void
{
factory(User::class)->create([
'name' => 'foo',
]);
$user = factory(User::class)->make();
assert($user instanceof User);
$user->name = 'foo';
$user->save();

$this->graphQL(/** @lang GraphQL */ '
mutation {
Expand Down Expand Up @@ -541,6 +544,7 @@ public function testUpdateAndDisconnectBelongsTo(string $action): void
);

$task = Task::findOrFail(1);
assert($task instanceof Task);
$this->assertNull($task->user, 'Must disconnect the parent relationship.');
}

Expand Down Expand Up @@ -896,6 +900,7 @@ public function testUpsertAcrossTwoNestedBelongsToRelationsAndOverrideExistingMo

// The first User has the first Role.
$role = Role::firstOrFail();
assert($role instanceof Role);
$this->assertSame([1], $role->users()->pluck('users.id')->toArray());

// Create another User.
Expand Down
Loading

0 comments on commit 50b150f

Please sign in to comment.