adrema/tests/Feature/Member/DeleteTest.php

109 lines
3.7 KiB
PHP
Raw Normal View History

2022-09-05 10:57:20 +02:00
<?php
namespace Tests\Feature\Member;
2023-08-17 12:46:48 +02:00
use Illuminate\Support\Str;
2023-04-25 00:32:44 +02:00
use App\Course\Models\Course;
use App\Course\Models\CourseMember;
2023-08-16 01:07:48 +02:00
use App\Lib\Events\JobFailed;
use App\Lib\Events\JobFinished;
use App\Lib\Events\JobStarted;
use App\Member\Actions\MemberDeleteAction;
2023-08-15 23:00:01 +02:00
use App\Member\Actions\NamiDeleteMemberAction;
2022-09-05 10:57:20 +02:00
use App\Member\Member;
2023-08-16 01:07:48 +02:00
use Exception;
2022-09-05 10:57:20 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
2022-09-05 10:57:20 +02:00
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
2023-08-16 01:07:48 +02:00
use Throwable;
2022-09-05 10:57:20 +02:00
class DeleteTest extends TestCase
{
use DatabaseTransactions;
2023-08-17 12:46:48 +02:00
public function setUp(): void
{
parent::setUp();
Event::fake([JobStarted::class, JobFinished::class, JobFailed::class]);
}
public function testItFiresJob(): void
2022-09-05 10:57:20 +02:00
{
2022-09-05 11:37:54 +02:00
Queue::fake();
2022-09-05 10:57:20 +02:00
$this->login()->loginNami();
$member = Member::factory()->defaults()->inNami(123)->create();
$response = $this->from('/member')->delete("/member/{$member->id}");
$response->assertRedirect('/member');
2023-08-17 12:46:48 +02:00
Event::assertDispatched(JobStarted::class);
MemberDeleteAction::assertPushed(fn ($action, $parameters) => $parameters[0] === $member->id);
2022-09-05 10:57:20 +02:00
}
public function testItDeletesMemberFromNami(): void
2022-09-05 10:57:20 +02:00
{
$this->login()->loginNami();
NamiDeleteMemberAction::partialMock()->shouldReceive('handle')->with(123)->once();
$member = Member::factory()->defaults()->inNami(123)->create();
2022-09-05 10:57:20 +02:00
MemberDeleteAction::run($member->id);
2022-09-05 10:57:20 +02:00
$this->assertDatabaseMissing('members', [
'id' => $member->id,
]);
}
2022-09-05 11:37:54 +02:00
public function testItDoesntRunActionWhenMemberIsNotInNami(): void
2022-09-05 11:37:54 +02:00
{
$this->login()->loginNami();
NamiDeleteMemberAction::partialMock()->shouldReceive('handle')->never();
$member = Member::factory()->defaults()->create();
2022-09-05 11:37:54 +02:00
MemberDeleteAction::run($member->id);
2022-09-05 11:37:54 +02:00
}
2023-04-25 00:32:44 +02:00
2023-04-25 00:32:44 +02:00
public function testItDeletesMembersWithCourses(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
$member = Member::factory()->defaults()->has(CourseMember::factory()->for(Course::factory()), 'courses')->create();
MemberDeleteAction::run($member->id);
2023-04-25 00:32:44 +02:00
2023-08-17 12:46:48 +02:00
$this->assertDatabaseMissing('members', ['id' => $member->id]);
2023-04-25 00:32:44 +02:00
}
public function testItFiresEventWhenFinished(): void
{
Event::fake([JobStarted::class, JobFinished::class]);
$this->withoutExceptionHandling()->login()->loginNami();
$member = Member::factory()->defaults()->create(['firstname' => 'Max', 'lastname' => 'Muster']);
2023-08-17 12:46:48 +02:00
$uuid = Str::uuid();
2023-08-17 12:46:48 +02:00
MemberDeleteAction::dispatch($member->id, $uuid);
2023-08-17 12:46:48 +02:00
Event::assertNotDispatched(JobStarted::class);
2023-10-16 15:48:03 +02:00
Event::assertDispatched(JobFinished::class, fn ($event) => $event->message === 'Mitglied Max Muster gelöscht' && $event->jobId->serialize() === $uuid->serialize());
}
2023-08-16 01:07:48 +02:00
public function testItFiresEventWhenDeletingFailed(): void
{
Event::fake([JobStarted::class, JobFinished::class, JobFailed::class]);
$this->login()->loginNami();
$member = Member::factory()->defaults()->create(['firstname' => 'Max', 'lastname' => 'Muster']);
MemberDeleteAction::partialMock()->shouldReceive('handle')->andThrow(new Exception('sorry'));
2023-08-17 12:46:48 +02:00
$uuid = Str::uuid();
2023-08-16 01:07:48 +02:00
try {
2023-08-17 12:46:48 +02:00
MemberDeleteAction::dispatch($member->id, $uuid);
2023-08-16 01:07:48 +02:00
} catch (Throwable) {
}
2023-08-17 12:46:48 +02:00
Event::assertNotDispatched(JobStarted::class);
2023-10-16 15:48:03 +02:00
Event::assertDispatched(JobFailed::class, fn ($event) => $event->message === 'Löschen von Max Muster fehlgeschlagen.' && $event->jobId->serialize() === $uuid->serialize());
2023-08-16 01:07:48 +02:00
Event::assertNotDispatched(JobFinished::class);
}
2022-09-05 10:57:20 +02:00
}