From ff828867385d42a76db139da9d94f0e90fa5efac Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 19 Feb 2022 22:33:33 +0100 Subject: [PATCH] Add exception when searching --- src/Api.php | 4 ++-- src/Fakes/SearchFake.php | 19 +++++++++++++++++++ tests/Unit/SearchTest.php | 10 ++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/Fakes/SearchFake.php diff --git a/src/Api.php b/src/Api.php index 7b51ab8..e7f4877 100644 --- a/src/Api.php +++ b/src/Api.php @@ -60,10 +60,10 @@ class Api { $start = ($page-1) * 100; $url = $this->url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode((object) $payload) ?: '{}').'&page='.$page.'&start='.$start.'&limit=100'; $response = $this->http()->get($url); - $totalEntries = $response->json()['totalEntries']; if ($response->json()['success'] !== true) { - $this->exception('Search failed', ['url' => $url], $response->json()); + $this->exception('Search failed', $url, $response->json(), ['page' => $page, 'start' => $start]); } + $totalEntries = $response->json()['totalEntries']; foreach ($response->json()['data'] as $member) { yield Member::fromNami(collect($member)->mapWithKeys(function($value, $key) { return [ str_replace('entries_', '', (string) $key) => $value ]; diff --git a/src/Fakes/SearchFake.php b/src/Fakes/SearchFake.php new file mode 100644 index 0000000..244bd2e --- /dev/null +++ b/src/Fakes/SearchFake.php @@ -0,0 +1,19 @@ +url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('{}').'&page='.$page.'&start='.$start.'&limit=100') { + return $this->errorResponse($error); + } + }); + } + +} diff --git a/tests/Unit/SearchTest.php b/tests/Unit/SearchTest.php index 82b2739..14052c2 100644 --- a/tests/Unit/SearchTest.php +++ b/tests/Unit/SearchTest.php @@ -5,10 +5,12 @@ namespace Zoomyboy\LaravelNami\Tests\Unit; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; +use Zoomyboy\LaravelNami\Fakes\SearchFake; use Zoomyboy\LaravelNami\Group; use Zoomyboy\LaravelNami\LoginException; use Zoomyboy\LaravelNami\Member; use Zoomyboy\LaravelNami\Nami; +use Zoomyboy\LaravelNami\NamiException; use Zoomyboy\LaravelNami\NamiServiceProvider; use Zoomyboy\LaravelNami\Tests\TestCase; @@ -57,6 +59,14 @@ class SearchTest extends TestCase Http::assertSentCount(1); } + public function test_it_throws_exception_when_search_fails(): void + { + $this->withoutExceptionHandling()->expectException(NamiException::class); + app(SearchFake::class)->fetchFails($page = 1, $start = 0, 'unknown error'); + + $this->login()->search([])->first(); + } + private function url(array $payload): string { $payload = rawurlencode(json_encode($payload));