adrema/tests/Feature/Member/DeleteTest.php

80 lines
2.7 KiB
PHP
Raw Normal View History

2022-09-05 10:57:20 +02:00
<?php
namespace Tests\Feature\Member;
2023-04-25 00:32:44 +02:00
use App\Course\Models\Course;
use App\Course\Models\CourseMember;
use App\Lib\Events\ClientMessage;
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;
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;
class DeleteTest extends TestCase
{
use DatabaseTransactions;
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');
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
$this->assertDatabaseCount('members', 0);
}
public function testItFiresEventWhenFinished(): void
{
Event::fake([JobStarted::class, JobFinished::class]);
$this->withoutExceptionHandling()->login()->loginNami();
$member = Member::factory()->defaults()->create(['firstname' => 'Max', 'lastname' => 'Muster']);
MemberDeleteAction::dispatch($member->id);
Event::assertDispatched(JobStarted::class, fn ($event) => $event->broadcastOn()->name === 'member' && $event->message === 'Lösche Mitglied Max Muster' && $event->reload === false);
Event::assertDispatched(JobFinished::class, fn ($event) => $event->message === 'Mitglied Max Muster gelöscht' && $event->reload === true);
}
2022-09-05 10:57:20 +02:00
}