laravel-nami-api/tests/Unit/Api/MembershipTest.php

159 lines
5.3 KiB
PHP
Raw Normal View History

2022-03-05 20:22:09 +01:00
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit\Api;
2022-03-05 22:02:03 +01:00
use Carbon\Carbon;
2023-02-08 01:26:01 +01:00
use Exception;
2022-03-05 20:22:09 +01:00
use Zoomyboy\LaravelNami\Data\Membership;
2023-02-08 01:26:01 +01:00
use Zoomyboy\LaravelNami\Exceptions\NoJsonReceivedException;
use Zoomyboy\LaravelNami\Exceptions\NotSuccessfulException;
use Zoomyboy\LaravelNami\Exceptions\RightException;
2022-03-05 20:22:09 +01:00
use Zoomyboy\LaravelNami\Fakes\MembershipFake;
use Zoomyboy\LaravelNami\Tests\TestCase;
class MembershipTest extends TestCase
{
public function testMembershipIsInstanceOfDto(): void
{
app(MembershipFake::class)
->shows(6, [
'id' => 10,
'aktivBis' => '',
'aktivVon' => '2013-05-06 00:00:00',
'gruppierungId' => 1000,
'taetigkeitId' => 15,
'untergliederungId' => 16,
2022-03-06 02:50:00 +01:00
'gruppierung' => '::group::',
2022-03-05 20:22:09 +01:00
]);
2022-03-06 02:41:40 +01:00
$membership = $this->login()->membership(6, 10);
2022-03-05 20:22:09 +01:00
$this->assertInstanceOf(Membership::class, $membership);
$this->assertSame(10, $membership->id);
$this->assertSame('2013-05-06 00:00:00', $membership->startsAt->toDateTimeString());
$this->assertSame(null, $membership->endsAt);
$this->assertSame(1000, $membership->groupId);
$this->assertSame(15, $membership->activityId);
$this->assertSame(16, $membership->subactivityId);
2023-02-17 14:54:17 +01:00
$this->assertSame('::group::', $membership->group()->name);
$this->assertSame(1000, $membership->group()->id);
2022-03-05 20:22:09 +01:00
}
public function testFetchesMembership(): void
{
2022-03-06 02:41:40 +01:00
app(MembershipFake::class)->shows(6, ['id' => 10]);
2022-03-05 20:22:09 +01:00
2022-03-06 02:41:40 +01:00
$this->login()->membership(6, 10);
2022-03-05 20:22:09 +01:00
app(MembershipFake::class)->assertFetchedSingle(6, 10);
}
public function testThrowExceptionWhenFetchingFails(): void
{
2022-03-06 02:41:40 +01:00
app(MembershipFake::class)->failsShowing(6, 11);
2023-02-08 01:26:01 +01:00
$this->expectException(NotSuccessfulException::class);
2022-03-05 20:22:09 +01:00
2022-03-06 02:41:40 +01:00
$this->login()->membership(6, 11);
2022-03-05 20:22:09 +01:00
}
public function testReturnsNothingWhenFetchingFailsWithHtml(): void
{
2023-02-08 01:26:01 +01:00
$this->expectException(NoJsonReceivedException::class);
2022-03-06 02:41:40 +01:00
app(MembershipFake::class)->failsShowingWithHtml(6, 11);
2022-03-05 20:22:09 +01:00
2023-02-08 01:26:01 +01:00
$this->login()->membership(6, 11);
2022-03-05 20:22:09 +01:00
}
/**
* @testWith ["Sicherheitsverletzung: Zugriff auf Rechte Recht (n:2001002 o:2) fehlgeschlagen", "Access denied - no right for requested operation"]
*/
2022-03-11 20:20:00 +01:00
public function testItGetsNoMembershipsWithNoRights(string $error): void
2022-03-05 20:22:09 +01:00
{
2023-02-08 01:26:01 +01:00
$this->expectException(RightException::class);
2022-03-05 20:22:09 +01:00
app(MembershipFake::class)->failsShowing(16, 68, $error);
$membership = $this->login()->membership(16, 68);
$this->assertNull($membership);
}
2022-03-05 22:02:03 +01:00
public function testItCanCreateAMembership(): void
{
Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00'));
app(MembershipFake::class)->createsSuccessfully(6, 133);
2023-02-02 00:06:46 +01:00
$membershipId = $this->login()->putMembership(6, Membership::from([
2022-03-05 22:02:03 +01:00
'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',
2023-02-21 13:01:37 +01:00
'aktivBis' => null,
2022-03-05 22:02:03 +01:00
'gruppierungId' => 1400,
]);
}
2022-03-11 21:18:44 +01:00
public function testItCanDeleteAMembership(): void
{
Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00'));
2022-11-11 00:17:56 +01:00
app(MembershipFake::class)->destroysSuccessfully(6, 133);
2022-03-11 21:18:44 +01:00
2023-02-02 00:06:46 +01:00
$this->login()->deleteMembership(6, Membership::from([
2022-03-11 21:18:44 +01:00
'id' => 133,
'subactivityId' => 3,
'activityId' => 2,
'groupId' => 1400,
'startsAt' => Carbon::parse('2022-02-03 00:00:00'),
'endsAt' => null,
]));
app(MembershipFake::class)->assertDeleted(6, 133);
}
public function testItSetsAMembershipsEndDateWhenDeletingFails(): void
{
Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00'));
app(MembershipFake::class)->failsDeleting(6, 133);
app(MembershipFake::class)->updatesSuccessfully(6, 133);
2023-02-02 00:06:46 +01:00
$this->login()->deleteMembership(6, Membership::from([
2022-03-11 21:18:44 +01:00
'id' => 133,
'subactivityId' => 3,
'activityId' => 2,
'groupId' => 1400,
'startsAt' => Carbon::parse('2022-02-03 00:00:00'),
'endsAt' => null,
]));
app(MembershipFake::class)->assertUpdated(6, [
'aktivBis' => '2022-02-03T00:00:00',
'id' => 133,
]);
}
public function testItDoesntUpdateMembershipWhenNoIdGiven(): void
{
2023-02-08 01:26:01 +01:00
$this->expectException(Exception::class);
2022-03-11 21:18:44 +01:00
Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00'));
app(MembershipFake::class)->failsDeleting(6, null);
app(MembershipFake::class)->updatesSuccessfully(6, null);
2023-02-02 00:06:46 +01:00
$this->login()->deleteMembership(6, Membership::from([
2022-03-11 21:18:44 +01:00
'id' => null,
'subactivityId' => 3,
'activityId' => 2,
'groupId' => 1400,
'startsAt' => Carbon::parse('2022-02-03 00:00:00'),
'endsAt' => null,
]));
}
2022-03-05 20:22:09 +01:00
}