diff --git a/src/Api.php b/src/Api.php index 8bf3cf1..3a14b41 100644 --- a/src/Api.php +++ b/src/Api.php @@ -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')) { diff --git a/src/Data/Membership.php b/src/Data/Membership.php index ac150f6..ce33dfd 100644 --- a/src/Data/Membership.php +++ b/src/Data/Membership.php @@ -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'), + ]); + } + } diff --git a/src/Fakes/MembershipFake.php b/src/Fakes/MembershipFake.php index 45bb73d..1be9483 100644 --- a/src/Fakes/MembershipFake.php +++ b/src/Fakes/MembershipFake.php @@ -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; + }); + } + } diff --git a/src/Member.php b/src/Member.php index f3766c5..0c4e1a0 100644 --- a/src/Member.php +++ b/src/Member.php @@ -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 diff --git a/tests/Unit/Api/MembershipTest.php b/tests/Unit/Api/MembershipTest.php index 4551d99..ccf7b69 100644 --- a/tests/Unit/Api/MembershipTest.php +++ b/tests/Unit/Api/MembershipTest.php @@ -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, + ]); + } + } diff --git a/tests/Unit/Member/PushMembershipsTest.php b/tests/Unit/Member/PushMembershipsTest.php new file mode 100644 index 0000000..26788d5 --- /dev/null +++ b/tests/Unit/Member/PushMembershipsTest.php @@ -0,0 +1,40 @@ +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, + ]); + } + +} diff --git a/tests/Unit/PushMembershipsTest.php b/tests/Unit/PushMembershipsTest.php deleted file mode 100644 index 3bec703..0000000 --- a/tests/Unit/PushMembershipsTest.php +++ /dev/null @@ -1,44 +0,0 @@ - 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' - ); - } - -}