laravel-nami-api/tests/Unit/SearchTest.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2021-06-19 00:44:56 +02:00
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
2021-11-18 19:57:59 +01:00
use Illuminate\Support\Facades\Http;
2023-02-08 01:26:01 +01:00
use Zoomyboy\LaravelNami\Exceptions\NotSuccessfulException;
2022-02-19 22:33:33 +01:00
use Zoomyboy\LaravelNami\Fakes\SearchFake;
2021-11-18 19:57:59 +01:00
use Zoomyboy\LaravelNami\Tests\TestCase;
2021-06-19 00:44:56 +02:00
class SearchTest extends TestCase
{
2022-11-08 16:07:10 +01:00
/**
* @var array<int, array<string, string|int|null>>
*/
2022-02-18 18:29:02 +01:00
public array $attributes = [
2021-06-19 00:44:56 +02:00
[
'firstname' => 'Max',
'lastname' => 'Nach1',
'group_id' => 103,
'nickname' => 'spitz1',
'gender_id' => 17,
'id' => 16,
], [
'firstname' => 'Jane',
'lastname' => 'Nach2',
'nickname' => null,
'group_id' => 103,
'gender_id' => null,
'id' => 17,
2022-03-11 20:20:00 +01:00
],
2021-06-19 00:44:56 +02:00
];
2022-11-08 16:07:10 +01:00
/**
* @return array<string, array{0: string, 1: array<int, string>}>
*/
2022-02-18 18:29:02 +01:00
public function dataProvider(): array
{
2021-06-19 00:44:56 +02:00
return [
'firstname' => ['vorname', ['Max', 'Jane']],
];
}
2022-03-11 20:20:00 +01:00
public function testFindAMemberByMglnr(): void
2022-02-18 18:29:02 +01:00
{
Http::fake([
2021-06-19 00:44:56 +02:00
$this->url(['mitgliedsNummber' => 150]) => Http::response($this->fakeJson('searchResponse.json'), 200),
2022-02-18 18:29:02 +01:00
]);
2021-06-19 00:44:56 +02:00
2022-02-18 18:29:02 +01:00
$member = $this->login()->findNr(150);
2021-06-19 00:44:56 +02:00
$this->assertEquals('Philipp', $member->firstname);
2023-02-07 01:29:00 +01:00
$this->assertEquals(89418, $member->id);
$this->assertEquals(100105, $member->groupId);
$this->assertEquals(90166, $member->memberId);
$this->assertEquals('Wö', $member->agegroup);
2022-03-11 20:20:00 +01:00
Http::assertSent(function ($request) {
2021-06-19 00:44:56 +02:00
return $request->url() == $this->url(['mitgliedsNummber' => 150])
2022-03-11 20:20:00 +01:00
&& 'GET' == $request->method();
2021-06-19 00:44:56 +02:00
});
2022-02-18 18:29:02 +01:00
Http::assertSentCount(1);
2021-06-19 00:44:56 +02:00
}
2022-03-11 20:20:00 +01:00
public function testItThrowsExceptionWhenSearchFails(): void
2022-02-19 22:33:33 +01:00
{
2023-02-08 01:26:01 +01:00
$this->withoutExceptionHandling()->expectException(NotSuccessfulException::class);
2023-05-07 21:13:50 +02:00
app(SearchFake::class)->fetchFails($page = 1, $start = 0, $perPage = 100, 'unknown error');
2022-02-19 22:33:33 +01:00
$this->login()->search([])->first();
}
2022-11-08 16:07:10 +01:00
/**
* @param array<string, string|int|null> $payload
*/
2022-02-18 18:29:02 +01:00
private function url(array $payload): string
{
2021-06-19 00:44:56 +02:00
$payload = rawurlencode(json_encode($payload));
2022-02-18 18:29:02 +01:00
2021-11-18 19:57:59 +01:00
return "https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues={$payload}&page=1&start=0&limit=100";
2021-06-19 00:44:56 +02:00
}
}