adrema/tests/EndToEnd/SyncActionTest.php

87 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2023-08-25 00:23:38 +02:00
<?php
namespace Tests\EndToEnd;
use App\Activity;
use App\Group;
use App\Maildispatcher\Actions\ResyncAction;
use App\Member\Member;
use App\Member\Membership;
use App\Membership\Actions\MembershipDestroyAction;
use App\Membership\Actions\MembershipStoreAction;
2023-10-16 12:36:56 +02:00
use App\Membership\Actions\StoreForGroupAction;
2023-08-25 00:23:38 +02:00
use App\Subactivity;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
use Throwable;
use Zoomyboy\LaravelNami\Fakes\MembershipFake;
class SyncActionTest extends TestCase
{
use DatabaseMigrations;
public function testItFiresActionJobWhenUsingController(): void
{
Queue::fake();
$this->login()->loginNami()->withoutExceptionHandling();
$member = Member::factory()->defaults()->create();
$activity = Activity::factory()->create();
$subactivity = Subactivity::factory()->create();
$group = Group::factory()->create();
$this->postJson('/api/membership/sync', [
'members' => [$member->id],
'activity_id' => $activity->id,
'subactivity_id' => $subactivity->id,
'group_id' => $group->id,
]);
2023-10-16 12:36:56 +02:00
StoreForGroupAction::assertPushed(fn ($action, $params) => $params[0]->is($group) && $params[1]->is($activity) && $params[2]->is($subactivity) && $params[3][0] === $member->id);
2023-08-25 00:23:38 +02:00
}
public function testItCreatesAMembership(): void
{
MembershipDestroyAction::partialMock()->shouldReceive('handle')->never();
MembershipStoreAction::partialMock()->shouldReceive('handle')->once();
$member = Member::factory()->defaults()->create();
$activity = Activity::factory()->create();
$subactivity = Subactivity::factory()->create();
$group = Group::factory()->create();
2023-10-16 12:36:56 +02:00
StoreForGroupAction::run($group, $activity, $subactivity, [$member->id]);
2023-08-25 00:23:38 +02:00
}
public function testItDeletesAMembership(): void
{
MembershipDestroyAction::partialMock()->shouldReceive('handle')->once();
MembershipStoreAction::partialMock()->shouldReceive('handle')->never();
ResyncAction::partialMock()->shouldReceive('handle')->once();
$member = Member::factory()->defaults()->has(Membership::factory()->inLocal('Leiter*in', 'Rover'))->create();
2023-10-16 12:36:56 +02:00
StoreForGroupAction::run($member->memberships->first()->group, $member->memberships->first()->activity, $member->memberships->first()->subactivity, []);
2023-08-25 00:23:38 +02:00
}
public function testItRollsbackWhenDeletionFails(): void
{
app(MembershipFake::class)
->shows(3, ['id' => 55])
->shows(3, ['id' => 56])
->destroysSuccessfully(3, 55)
->failsDeleting(3, 56);
$this->login()->loginNami();
$member = Member::factory()->defaults()->inNami(3)
->has(Membership::factory()->in('Leiter*in', 10, 'Rover', 11)->inNami(55))
->has(Membership::factory()->in('Leiter*in', 10, 'Jungpfadfinder', 12)->inNami(56))
->create();
try {
2023-10-16 12:36:56 +02:00
StoreForGroupAction::run($member->memberships->first()->group, $member->memberships->first()->activity, $member->memberships->first()->subactivity, []);
2023-08-25 00:23:38 +02:00
} catch (Throwable $e) {
}
$this->assertDatabaseCount('memberships', 2);
}
}