Test actions for space invites
MODIFY
database/factories/ModelFactory.php
database/factories/ModelFactory.php
@@ -10,6 +10,7 @@ use App\Models\Tag;
use App\Models\Earning;
use App\Models\Spending;
use App\Models\Recurring;
+use App\Models\SpaceInvite;
use Illuminate\Support\Str;
$factory->define(User::class, function (Generator $faker) {
@@ -28,6 +29,15 @@ $factory->define(Space::class, function (Generator $faker) {
];
});
+$factory->define(SpaceInvite::class, function (Generator $faker) {
+ return [
+ 'space_id' => Space::all()->random()->id,
+ 'invitee_user_id' => User::all()->random()->id,
+ 'inviter_user_id' => User::all()->random()->id,
+ 'role' => 'regular'
+ ];
+});
+
$factory->define(Tag::class, function (Generator $faker) {
return [
'name' => ucfirst($faker->word)

ADD
tests/Unit/Actions/SpaceInvite/AcceptanceTest.php
+<?php
+
+namespace Tests\Unit\Actions\SpaceInvite;
+
+use App\Actions\AcceptSpaceInviteAction;
+use App\Exceptions\SpaceInviteNotFoundException;
+use App\Models\SpaceInvite;
+use App\Models\User;
+use Tests\TestCase;
+
+class AcceptanceTest extends TestCase
+{
+ public function testInviteNotFound(): void
+ {
+ $this->expectException(SpaceInviteNotFoundException::class);
+
+ (new AcceptSpaceInviteAction())->execute(999);
+ }
+
+ public function testSuccessfulAcceptance(): void
+ {
+ $user = factory(User::class)->create();
+ $invite = factory(SpaceInvite::class)->create();
+
+ $this->be($user); // Necessary because AcceptSpaceInviteAction uses authentication
+
+ $this->assertEmpty($user->spaces, 'User should not be part of any spaces');
+
+ (new AcceptSpaceInviteAction())->execute($invite->id);
+
+ $this->assertTrue(SpaceInvite::find($invite->id)->accepted, 'Invite should have been accepted');
+ $this->assertNotEmpty(User::find($user->id)->spaces, 'User should be part of 1 space');
+ }
+}

ADD
tests/Unit/Actions/SpaceInvite/CreationTest.php
+<?php
+
+namespace Tests\Unit\Actions\SpaceInvite;
+
+use App\Actions\CreateSpaceInviteAction;
+use App\Exceptions\SpaceInviteAlreadyExistsException;
+use App\Exceptions\SpaceInviteInviteeAlreadyPresentException;
+use App\Models\Space;
+use App\Models\SpaceInvite;
+use App\Models\User;
+use Tests\TestCase;
+
+class CreationTest extends TestCase
+{
+ public function testInviteeAlreadyPresent(): void
+ {
+ $space = factory(Space::class)->create();
+ $inviteeUser = factory(User::class)->create();
+ $inviterUser = factory(User::class)->create();
+
+ $inviteeUser->spaces()->attach($space->id, ['role' => 'regular']);
+
+ $this->expectException(SpaceInviteInviteeAlreadyPresentException::class);
+
+ (new CreateSpaceInviteAction())->execute(
+ $space->id,
+ $inviteeUser->id,
+ $inviterUser->id,
+ 'regular'
+ );
+ }
+
+ public function testInviteAlreadyExists(): void
+ {
+ $space = factory(Space::class)->create();
+ $inviteeUser = factory(User::class)->create();
+ $inviterUser = factory(User::class)->create();
+
+ factory(SpaceInvite::class)->create([
+ 'space_id' => $space->id,
+ 'invitee_user_id' => $inviteeUser->id
+ ]);
+
+ $this->expectException(SpaceInviteAlreadyExistsException::class);
+
+ (new CreateSpaceInviteAction())->execute(
+ $space->id,
+ $inviteeUser->id,
+ $inviterUser->id,
+ 'regular'
+ );
+ }
+
+ public function testSuccessfulCreation(): void
+ {
+ $space = factory(Space::class)->create();
+ $inviteeUser = factory(User::class)->create();
+ $inviterUser = factory(User::class)->create();
+
+ $invite = (new CreateSpaceInviteAction())->execute(
+ $space->id,
+ $inviteeUser->id,
+ $inviterUser->id,
+ 'regular'
+ );
+
+ $this->assertNotNull($invite->id, 'Invite should have been persisted');
+ }
+}

ADD
tests/Unit/Actions/SpaceInvite/DenialTest.php
+<?php
+
+namespace Tests\Unit\Actions\SpaceInvite;
+
+use App\Actions\DenySpaceInviteAction;
+use App\Exceptions\SpaceInviteNotFoundException;
+use App\Models\SpaceInvite;
+use Tests\TestCase;
+
+class DenialTest extends TestCase
+{
+ public function testInviteNotFound(): void
+ {
+ $this->expectException(SpaceInviteNotFoundException::class);
+
+ (new DenySpaceInviteAction())->execute(999);
+ }
+
+ public function testSuccessfulDenial(): void
+ {
+ $invite = factory(SpaceInvite::class)->create();
+
+ (new DenySpaceInviteAction())->execute($invite->id);
+
+ $this->assertFalse(SpaceInvite::find($invite->id)->accepted, 'Invite should not be accepted');
+ }
+}

Test actions for space invites