Add exception when searching

This commit is contained in:
philipp lang 2022-02-19 22:33:33 +01:00
parent 3916d72837
commit ff82886738
3 changed files with 31 additions and 2 deletions

View File

@ -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 ];

19
src/Fakes/SearchFake.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace Zoomyboy\LaravelNami\Fakes;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class SearchFake extends Fake {
public function fetchFails(int $page, int $start, ?string $error = 'wrong message'): void
{
Http::fake(function($request) use ($error, $page, $start) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('{}').'&page='.$page.'&start='.$start.'&limit=100') {
return $this->errorResponse($error);
}
});
}
}

View File

@ -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));