Add error handling for failed member fetch

This commit is contained in:
philipp lang 2022-02-19 22:11:46 +01:00
parent 96269d16c7
commit 3916d72837
4 changed files with 32 additions and 2 deletions

20
src/Fakes/MemberFake.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace Zoomyboy\LaravelNami\Fakes;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class MemberFake extends Fake {
public function fetchFails(int $groupId, int $memberId, string $error = 'wrong message'): void
{
Http::fake(function($request) use ($groupId, $memberId, $error) {
$url = 'https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/'.$groupId.'/'.$memberId;
if ($request->url() === $url && $request->method() === 'GET') {
return $this->errorResponse($error);
}
});
}
}

View File

@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Http;
class SubactivityFake extends Fake {
public function fetchFailed(int $activityId, ?string $error = 'wrong message'): void
public function fetchFails(int $activityId, ?string $error = 'wrong message'): void
{
Http::fake(function($request) use ($activityId, $error) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/'.$activityId) {

View File

@ -53,7 +53,7 @@ class PullActivitiesTest extends TestCase
public function test_throw_error_when_subactivities_request_fails(): void
{
$this->expectException(NamiException::class);
app(SubactivityFake::class)->fetchFailed(4, 'sorry dude');
app(SubactivityFake::class)->fetchFails(4, 'sorry dude');
Http::fake([
'https://nami.dpsg.de/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/4' => Http::response($this->fakeJson('subactivities-4.json'), 200)
]);

View File

@ -4,9 +4,11 @@ namespace Zoomyboy\LaravelNami\Tests\Unit;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Fakes\MemberFake;
use Zoomyboy\LaravelNami\Group;
use Zoomyboy\LaravelNami\LoginException;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\NamiException;
use Zoomyboy\LaravelNami\NamiServiceProvider;
use Zoomyboy\LaravelNami\Tests\TestCase;
@ -177,4 +179,12 @@ class PullMemberTest extends TestCase
Http::assertSentCount(3);
}
public function test_member_fetch_can_fail(): void
{
$this->expectException(NamiException::class);
app(MemberFake::class)->fetchFails(103, 16);
$this->login()->member(103, 16);
}
}