Skip to content

Commit

Permalink
Finish test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jonerickson committed Sep 1, 2024
1 parent 57de89b commit 113d52f
Show file tree
Hide file tree
Showing 29 changed files with 409 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public function deleting_a_single_relation_resource_with_multiple_route_paramete
public function deleting_a_single_relation_resource_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function restoring_a_single_relation_resource_with_multiple_route_paramet
public function restoring_a_single_relation_resource_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function getting_a_single_relation_resource_with_multiple_route_parameter
public function getting_a_single_relation_resource_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public function updating_a_single_relation_resource_with_multiple_route_paramete
public function updating_a_single_relation_resource_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Orion\Tests\Feature\Relations\BelongsToMany;

use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Orion\Tests\Feature\TestCase;
use Orion\Tests\Fixtures\App\Drivers\TwoRouteParameterKeyResolver;
use Orion\Tests\Fixtures\App\Models\Role;
use Orion\Tests\Fixtures\App\Models\User;
use Orion\Tests\Fixtures\App\Policies\GreenPolicy;
Expand Down Expand Up @@ -46,7 +49,6 @@ public function attaching_relation_resources_when_authorized_only_on_parent(): v

self::assertEquals(0, $user->roles()->count());


$response = $this->post(
"/api/users/{$user->id}/roles/attach",
[
Expand Down Expand Up @@ -754,4 +756,307 @@ public function updating_pivot_of_relation_resource_casted_to_json_pivot_field()
['references' => ['key' => 'value']]
);
}

/** @test */
public function attaching_relation_resources_with_multiple_route_parameters(): void
{
$this->useKeyResolver(TwoRouteParameterKeyResolver::class);

/** @var User $user */
$user = factory(User::class)->create();
$roleA = factory(Role::class)->create();
$roleB = factory(Role::class)->create();

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

self::assertEquals(0, $user->roles()->count());

$response = $this->post(
"/api/v1/users/{$user->id}/roles/attach",
[
'resources' => [$roleA->id, $roleB->id],
]
);

$this->assertResourcesAttached(
$response,
'roles',
$user,
collect([$roleA, $roleB])
);
}

/** @test */
public function attaching_relation_resources_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

/** @var User $user */
$user = factory(User::class)->create();
$roleA = factory(Role::class)->create();
$roleB = factory(Role::class)->create();

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

self::assertEquals(0, $user->roles()->count());

$response = $this->post(
"/api/v1/users/{$user->id}/roles/attach",
[
'resources' => [$roleA->id, $roleB->id],
]
);

$response->assertNotFound();
}

/** @test */
public function detaching_relation_resources_with_multiple_route_parameters(): void
{
$this->useKeyResolver(TwoRouteParameterKeyResolver::class);

/** @var User $user */
$user = factory(User::class)->create();
$roles = factory(Role::class)->times(5)->make();
$user->roles()->saveMany($roles);

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

$roles = $user->roles()->get();

self::assertEquals(5, $roles->count());

$response = $this->delete(
"/api/v1/users/{$user->id}/roles/detach",
[
'resources' => $roles->pluck('id')->toArray(),
]
);

$this->assertResourcesDetached($response, 'roles', $user, $roles);
}

/** @test */
public function detaching_relation_resources_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

/** @var User $user */
$user = factory(User::class)->create();
$roles = factory(Role::class)->times(5)->make();
$user->roles()->saveMany($roles);

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

$roles = $user->roles()->get();

self::assertEquals(5, $roles->count());

$response = $this->delete(
"/api/v1/users/{$user->id}/roles/detach",
[
'resources' => $roles->pluck('id')->toArray(),
]
);

$response->assertNotFound();
}

/** @test */
public function syncing_relation_resources_with_multiple_route_parameters(): void
{
$this->useKeyResolver(TwoRouteParameterKeyResolver::class);

/** @var User $user */
$user = factory(User::class)->create();
$roleToAttach = factory(Role::class)->create();
$roleToDetach = factory(Role::class)->create();
$roleToUpdate = factory(Role::class)->create();
$user->roles()->attach($roleToDetach->id);
$user->roles()->attach($roleToUpdate->id, ['custom_name' => 'test original']);

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

self::assertEquals(2, $user->roles()->count());

$response = $this->patch(
"/api/v1/users/{$user->id}/roles/sync",
[
'resources' => [
$roleToAttach->id,
$roleToUpdate->id => ['custom_name' => 'test updated'],
],
]
);

$this->assertResourcesSynced(
$response,
'roles',
$user,
$this->buildSyncMap([$roleToAttach], [$roleToDetach], [$roleToUpdate]),
[$roleToUpdate->id => ['custom_name' => 'test updated']]
);
}

/** @test */
public function syncing_relation_resources_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

/** @var User $user */
$user = factory(User::class)->create();
$roleToAttach = factory(Role::class)->create();
$roleToDetach = factory(Role::class)->create();
$roleToUpdate = factory(Role::class)->create();
$user->roles()->attach($roleToDetach->id);
$user->roles()->attach($roleToUpdate->id, ['custom_name' => 'test original']);

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

self::assertEquals(2, $user->roles()->count());

$response = $this->patch(
"/api/v1/users/{$user->id}/roles/sync",
[
'resources' => [
$roleToAttach->id,
$roleToUpdate->id => ['custom_name' => 'test updated'],
],
]
);

$response->assertNotFound();
}

/** @test */
public function toggling_relation_resources_with_multiple_route_parameters(): void
{
$this->useKeyResolver(TwoRouteParameterKeyResolver::class);

/** @var User $user */
$user = factory(User::class)->create();
$roleToAttach = factory(Role::class)->create();
$roleToDetach = factory(Role::class)->create();
$user->roles()->attach($roleToDetach->id);

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

self::assertEquals(1, $user->roles()->count());

$response = $this->patch(
"/api/v1/users/{$user->id}/roles/toggle",
[
'resources' => [$roleToAttach->id, $roleToDetach->id],
]
);

$this->assertResourcesToggled(
$response,
'roles',
$user,
$this->buildSyncMap([$roleToAttach], [$roleToDetach])
);
}

/** @test */
public function toggling_relation_resources_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

/** @var User $user */
$user = factory(User::class)->create();
$roleToAttach = factory(Role::class)->create();
$roleToDetach = factory(Role::class)->create();
$user->roles()->attach($roleToDetach->id);

Gate::policy(User::class, GreenPolicy::class);
Gate::policy(Role::class, GreenPolicy::class);

self::assertEquals(1, $user->roles()->count());

$response = $this->patch(
"/api/v1/users/{$user->id}/roles/toggle",
[
'resources' => [$roleToAttach->id, $roleToDetach->id],
]
);

$response->assertNotFound();
}

/** @test */
public function updating_pivot_of_relation_resource_with_multiple_route_parameters(): void
{
$this->useKeyResolver(TwoRouteParameterKeyResolver::class);

/** @var User $user */
$user = factory(User::class)->create();
$role = factory(Role::class)->create();
$user->roles()->save($role);

Gate::policy(Role::class, GreenPolicy::class);

$response = $this->patch(
"/api/v1/users/{$user->id}/roles/{$role->id}/pivot",
[
'pivot' => [
'custom_name' => 'test value',
],
]
);

$this->assertResourcePivotUpdated(
$response,
'roles',
$user,
$role,
['custom_name' => 'test value']
);
}

/** @test */
public function updating_pivot_of_relation_resource_with_multiple_route_parameters_fails_with_default_key_resolver(): void
{
if (DB::connection()->getDriverName() === 'pgsql') {
$this->withoutExceptionHandling();
$this->expectException(QueryException::class);
}

/** @var User $user */
$user = factory(User::class)->create();
$role = factory(Role::class)->create();
$user->roles()->save($role);

Gate::policy(Role::class, GreenPolicy::class);

$response = $this->patch(
"/api/v1/users/{$user->id}/roles/{$role->id}/pivot",
[
'pivot' => [
'custom_name' => 'test value',
],
]
);

$response->assertNotFound();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Orion\Tests\Feature\Relations\BelongsToMany;

use Illuminate\Support\Facades\Gate;
use Mockery;
use Orion\Contracts\ComponentsResolver;
use Orion\Tests\Feature\TestCase;
use Orion\Tests\Fixtures\App\Http\Resources\SampleResource;
use Orion\Tests\Fixtures\App\Models\Notification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Orion\Tests\Feature\Relations\BelongsToMany;

use Illuminate\Support\Facades\Gate;
use Mockery;
use Orion\Contracts\ComponentsResolver;
use Orion\Tests\Feature\TestCase;
use Orion\Tests\Fixtures\App\Http\Resources\SampleCollectionResource;
use Orion\Tests\Fixtures\App\Http\Resources\SampleResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Orion\Tests\Feature\Relations\BelongsToMany;

use Illuminate\Support\Facades\Gate;
use Mockery;
use Orion\Contracts\ComponentsResolver;
use Orion\Tests\Feature\TestCase;
use Orion\Tests\Fixtures\App\Http\Resources\SampleResource;
use Orion\Tests\Fixtures\App\Models\Notification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function getting_a_single_trashed_relation_resource_when_with_trashed_que
$this->assertResourceShown($response, $user->notifications()->withTrashed()->first()->toArray());
}


/** @test */
public function getting_a_single_transformed_relation_resource(): void
{
Expand Down
Loading

0 comments on commit 113d52f

Please sign in to comment.