Add tests for membrsip

This commit is contained in:
philipp lang 2022-03-05 22:02:03 +01:00
parent d954f0d0b8
commit a4d8a2aefb
7 changed files with 132 additions and 58 deletions

View File

@ -141,18 +141,18 @@ class Api {
return $response->json()['data'];
}
public function putMembership(int $memberId, array $data): int
public function putMembership(int $memberId, Membership $data): int
{
$this->assertLoggedIn();
if (data_get($data, 'id')) {
$url = $this->url."/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}/{$data['id']}";
$response = $this->http()->put($url, $data);
if ($data->id) {
$url = $this->url."/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}/{$data->id}";
$response = $this->http()->put($url, $data->toNami());
} else {
$url = $this->url."/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}";
$response = $this->http()->post($url, $data);
$response = $this->http()->post($url, $data->toNami());
}
if (data_get($response->json(), 'success') !== true) {
$this->exception('Update failed', ['url' => $url, 'data' => $data], $response->json());
$this->exception('Update failed', $url, $response->json(), $data->toArray());
}
if (data_get($data, 'id')) {

View File

@ -11,7 +11,7 @@ use Zoomyboy\LaravelNami\Casters\NullableCarbonCaster;
class Membership extends DataTransferObject {
public int $id;
public ?int $id;
#[MapFrom('gruppierungId')]
public int $groupId;
@ -30,5 +30,29 @@ class Membership extends DataTransferObject {
#[MapFrom('untergliederungId')]
public int $subactivityId;
public function toNami(): array
{
return [
'gruppierungId' => $this->groupId,
'id' => $this->id,
'aktivVon' => $this->startsAt->format('Y-m-d').'T00:00:00',
'aktivBis' => $this->endsAt ? $this->endsAt->toDateTimeString() : null,
'taetigkeitId' => $this->activityId,
'untergliederungId' => $this->subactivityId,
];
}
public static function fromArray(array $data): self
{
return new self([
'gruppierungId' => data_get($data, 'groupId'),
'id' => data_get($data, 'id'),
'aktivVon' => data_get($data, 'startsAt'),
'aktivBis' => data_get($data, 'endsAt'),
'taetigkeitId' => data_get($data, 'activityId'),
'untergliederungId' => data_get($data, 'subactivityId'),
]);
}
}

View File

@ -105,4 +105,38 @@ class MembershipFake extends Fake {
});
}
public function createsSuccessfully(int $memberId, int $membershipId): void
{
Http::fake(function($request) use ($memberId, $membershipId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}" && $request->method() === 'POST') {
return $this->idResponse($membershipId);
}
});
}
public function assertCreated(int $memberId, array $payload): void
{
$url = "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}";
Http::assertSent(function ($request) use ($url, $payload) {
if ($request->url() !== $url || $request->method() !== 'POST') {
return false;
}
if (
data_get($request, 'gruppierungId') !== data_get($payload, 'gruppierungId')
&& data_get($request, 'id') !== data_get($payload, 'id')
&& data_get($request, 'taetigkeitId') !== data_get($payload, 'taetigkeitId')
&& data_get($request, 'untergliederungId') !== data_get($payload, 'untergliederungId')
) {
return false;
}
if (data_get($request, 'aktivVon') && $request['aktivVon'] !== data_get($payload, 'aktivVon')) {
return false;
}
return true;
});
}
}

View File

@ -149,14 +149,9 @@ class Member extends Model {
return Nami::membershipsOf($this->id);
}
public function putMembership(array $data): int
public function putMembership(Membership $membership): int
{
return Nami::putMembership($this->id, [
'gruppierungId' => $data['group_id'],
'taetigkeitId' => $data['activity_id'],
'untergliederungId' => $data['subactivity_id'],
'aktivVon' => $data['starts_at']->format('Y-m-d').'T00:00:00',
]);
return Nami::putMembership($this->id, $membership);
}
public function deleteMembership(int $id): int

View File

@ -2,6 +2,7 @@
namespace Zoomyboy\LaravelNami\Tests\Unit\Api;
use Carbon\Carbon;
use Zoomyboy\LaravelNami\Data\Membership;
use Zoomyboy\LaravelNami\Exceptions\RightException;
use Zoomyboy\LaravelNami\Fakes\MembershipFake;
@ -120,4 +121,28 @@ class MembershipTest extends TestCase
$this->assertNull($membership);
}
public function testItCanCreateAMembership(): void
{
Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00'));
app(MembershipFake::class)->createsSuccessfully(6, 133);
$membershipId = $this->login()->putMembership(6, Membership::fromArray([
'id' => null,
'subactivityId' => 3,
'activityId' => 2,
'groupId' => 1400,
'startsAt' => Carbon::parse('2022-02-03 00:00:00'),
'endsAt' => null,
]));
$this->assertEquals(133, $membershipId);
app(MembershipFake::class)->assertCreated(6, [
'taetigkeitId' => 2,
'untergliederungId' => 3,
'aktivVon' => '2022-02-03T00:00:00',
'aktivBis' => '',
'gruppierungId' => 1400,
]);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit\Member;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Data\Membership;
use Zoomyboy\LaravelNami\Fakes\MembershipFake;
use Zoomyboy\LaravelNami\Member;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\Tests\TestCase;
class PushMembershipsTest extends TestCase
{
public function test_create_a_membership(): void
{
Carbon::setTestNow(Carbon::parse('2021-02-03 06:00:00'));
app(MembershipFake::class)->createsSuccessfully(16, 65);
$this->login();
$member = new Member(['id' => 16]);
$id = $member->putMembership(Membership::fromArray([
'startsAt' => now(),
'groupId' => 150,
'activityId' => 56,
'subactivityId' => 89,
]));
$this->assertEquals(65, $id);
Http::assertSentCount(1);
app(MembershipFake::class)->assertCreated(16, [
'taetigkeitId' => 56,
'untergliederungId' => 89,
'aktivVon' => '2021-02-03T00:00:00',
'gruppierungId' => 150,
]);
}
}

View File

@ -1,44 +0,0 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Member;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\Tests\TestCase;
class PushMembershipsTest extends TestCase
{
public function test_create_a_membership(): void
{
Carbon::setTestNow(Carbon::parse('2021-02-03 06:00:00'));
Http::fake([
'https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/16/flist' => Http::response($this->fakeJson('membership-overview.json'), 200),
'https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/16' => Http::response($this->fakeJson('membership-create.json'), 200),
]);
$this->login();
$member = new Member(['id' => 16]);
$id = $member->putMembership([
'created_at' => now(),
'group_id' => 150,
'activity_id' => 56,
'subactivity_id' => 89,
'starts_at' => now(),
]);
$this->assertEquals(65, $id);
Http::assertSentCount(1);
Http::assertSent(fn ($request) => $request->method() === 'POST'
&& $request->url() === 'https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/16'
&& $request['gruppierungId'] === 150
&& $request['taetigkeitId'] === 56
&& $request['untergliederungId'] === 89
&& $request['aktivVon'] === '2021-02-03T00:00:00'
);
}
}