Add exception when searching
This commit is contained in:
parent
3916d72837
commit
ff82886738
|
@ -60,10 +60,10 @@ class Api {
|
||||||
$start = ($page-1) * 100;
|
$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';
|
$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);
|
$response = $this->http()->get($url);
|
||||||
$totalEntries = $response->json()['totalEntries'];
|
|
||||||
if ($response->json()['success'] !== true) {
|
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) {
|
foreach ($response->json()['data'] as $member) {
|
||||||
yield Member::fromNami(collect($member)->mapWithKeys(function($value, $key) {
|
yield Member::fromNami(collect($member)->mapWithKeys(function($value, $key) {
|
||||||
return [ str_replace('entries_', '', (string) $key) => $value ];
|
return [ str_replace('entries_', '', (string) $key) => $value ];
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,10 +5,12 @@ namespace Zoomyboy\LaravelNami\Tests\Unit;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Config;
|
use Illuminate\Support\Facades\Config;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Zoomyboy\LaravelNami\Fakes\SearchFake;
|
||||||
use Zoomyboy\LaravelNami\Group;
|
use Zoomyboy\LaravelNami\Group;
|
||||||
use Zoomyboy\LaravelNami\LoginException;
|
use Zoomyboy\LaravelNami\LoginException;
|
||||||
use Zoomyboy\LaravelNami\Member;
|
use Zoomyboy\LaravelNami\Member;
|
||||||
use Zoomyboy\LaravelNami\Nami;
|
use Zoomyboy\LaravelNami\Nami;
|
||||||
|
use Zoomyboy\LaravelNami\NamiException;
|
||||||
use Zoomyboy\LaravelNami\NamiServiceProvider;
|
use Zoomyboy\LaravelNami\NamiServiceProvider;
|
||||||
use Zoomyboy\LaravelNami\Tests\TestCase;
|
use Zoomyboy\LaravelNami\Tests\TestCase;
|
||||||
|
|
||||||
|
@ -57,6 +59,14 @@ class SearchTest extends TestCase
|
||||||
Http::assertSentCount(1);
|
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
|
private function url(array $payload): string
|
||||||
{
|
{
|
||||||
$payload = rawurlencode(json_encode($payload));
|
$payload = rawurlencode(json_encode($payload));
|
||||||
|
|
Loading…
Reference in New Issue