adrema/tests/Feature/Membership/StoreTest.php

224 lines
7.2 KiB
PHP
Raw Normal View History

2022-03-06 01:53:15 +01:00
<?php
namespace Tests\Feature\Membership;
use App\Activity;
use App\Group;
use App\Member\Member;
use App\Subactivity;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-11-16 22:59:49 +01:00
use Tests\RequestFactories\MembershipRequestFactory;
2022-03-06 01:53:15 +01:00
use Tests\TestCase;
use Zoomyboy\LaravelNami\Fakes\MemberFake;
use Zoomyboy\LaravelNami\Fakes\MembershipFake;
class StoreTest extends TestCase
{
use DatabaseTransactions;
2022-11-16 22:59:49 +01:00
public function setUp(): void
2022-03-06 01:53:15 +01:00
{
2022-11-16 22:59:49 +01:00
parent::setUp();
2022-03-06 01:53:15 +01:00
Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00'));
2022-11-16 22:59:49 +01:00
$this->login()->loginNami();
}
public function testItCreatesAMembership(): void
{
$this->withoutExceptionHandling();
2022-03-06 01:53:15 +01:00
app(MembershipFake::class)->createsSuccessfully(6, 133);
app(MemberFake::class)->shows(1400, 6, ['version' => 1506]);
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
2022-11-16 22:59:49 +01:00
->create();
2022-03-06 01:53:15 +01:00
$activity = Activity::factory()
2022-11-16 22:59:49 +01:00
->inNami(6)
->hasAttached(Subactivity::factory()->inNami(4))
2022-03-06 01:53:15 +01:00
->createOne();
2022-11-16 22:59:49 +01:00
$response = $this->from('/member')->post(
"/member/{$member->id}/membership",
2022-12-11 21:00:48 +01:00
MembershipRequestFactory::new()->promise(now())->in($activity, $activity->subactivities->first())->create()
2022-11-16 22:59:49 +01:00
);
$response->assertRedirect('/member');
$this->assertEquals(1506, $member->fresh()->version);
$this->assertDatabaseHas('memberships', [
'member_id' => $member->id,
2022-03-06 01:53:15 +01:00
'activity_id' => $activity->id,
'subactivity_id' => $activity->subactivities->first()->id,
2022-11-16 22:59:49 +01:00
'nami_id' => 133,
2022-12-11 21:00:48 +01:00
'promised_at' => now()->format('Y-m-d'),
2022-03-06 01:53:15 +01:00
]);
2022-11-16 22:59:49 +01:00
app(MembershipFake::class)->assertCreated(6, [
'untergliederungId' => 4,
'taetigkeitId' => 6,
'gruppierungId' => 1400,
'aktivVon' => '2022-02-03T00:00:00',
'aktivBis' => null,
]);
}
public function testItDoesntFireNamiWhenMembershipIsLocal(): void
{
$this->withoutExceptionHandling();
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
->create();
$activity = Activity::factory()->hasAttached(Subactivity::factory())->create();
$this->from('/member')->post(
"/member/{$member->id}/membership",
MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create()
);
$this->assertDatabaseHas('memberships', [
'member_id' => $member->id,
'activity_id' => $activity->id,
'subactivity_id' => $activity->subactivities->first()->id,
'nami_id' => null,
]);
}
public function testItDoesntFireNamiWhenSubactivityIsLocal(): void
{
$this->withoutExceptionHandling();
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
->create();
$activity = Activity::factory()->inNami(666)->hasAttached(Subactivity::factory())->create();
$this->from('/member')->post(
"/member/{$member->id}/membership",
MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create()
);
$this->assertDatabaseHas('memberships', [
'member_id' => $member->id,
'activity_id' => $activity->id,
'subactivity_id' => $activity->subactivities->first()->id,
'nami_id' => null,
]);
}
2022-11-16 22:59:49 +01:00
public function testActivityIsRequired(): void
{
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
->create();
$response = $this->post(
"/member/{$member->id}/membership",
MembershipRequestFactory::new()->missingAll()->create(),
);
$this->assertErrors(['activity_id' => 'Tätigkeit ist erforderlich.'], $response);
}
public function testActivityShouldBeValid(): void
{
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
->create();
$response = $this->post(
"/member/{$member->id}/membership",
MembershipRequestFactory::new()->invalidActivity()->create(),
);
$this->assertErrors(['activity_id' => 'Tätigkeit ist nicht vorhanden.'], $response);
}
public function testSubactivityShouldBeFromActivity(): void
{
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
->create();
$response = $this->post(
"/member/{$member->id}/membership",
MembershipRequestFactory::new()->unmatchingSubactivity()->create(),
);
$this->assertErrors(['subactivity_id' => 'Untertätigkeit ist nicht vorhanden.'], $response);
}
public function testSubactivityCanBeEmpty(): void
{
$this->withoutExceptionHandling();
app(MembershipFake::class)->createsSuccessfully(6, 133);
app(MemberFake::class)->shows(1400, 6, ['version' => 1506]);
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
->create();
$activity = Activity::factory()
->inNami(6)
->createOne();
$this->post(
"/member/{$member->id}/membership",
MembershipRequestFactory::new()->in($activity, null)->create()
);
2022-03-06 01:53:15 +01:00
$this->assertEquals(1506, $member->fresh()->version);
$this->assertDatabaseHas('memberships', [
'member_id' => $member->id,
'activity_id' => $activity->id,
2022-11-16 22:59:49 +01:00
'subactivity_id' => null,
2022-03-06 01:53:15 +01:00
'nami_id' => 133,
]);
app(MembershipFake::class)->assertCreated(6, [
2022-11-16 22:59:49 +01:00
'untergliederungId' => null,
'taetigkeitId' => 6,
2022-03-06 01:53:15 +01:00
'gruppierungId' => 1400,
'aktivVon' => '2022-02-03T00:00:00',
'aktivBis' => null,
]);
}
2022-11-16 22:59:49 +01:00
/**
* @testWith ["namierror<br>", "namierror&lt;br&gt;"]
* ["", "Erstellen der Mitgliedschaft fehlgeschlagen"]
*/
public function testItReturnsNamiError(string $namiError, string $validationError): void
{
app(MembershipFake::class)->failsCreating(6, $namiError);
$member = Member::factory()
->defaults()
->for(Group::factory()->inNami(1400))
->inNami(6)
->create();
$activity = Activity::factory()
->inNami(6)
->hasAttached(Subactivity::factory()->inNami(4))
->createOne();
$response = $this->post(
"/member/{$member->id}/membership",
MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create()
);
$this->assertErrors(['nami' => $validationError], $response);
$this->assertDatabaseMissing('memberships', [
'member_id' => $member->id,
]);
}
2022-03-06 01:53:15 +01:00
}