Add Getter for Seach layer option

This commit is contained in:
philipp lang 2023-05-18 01:10:13 +02:00
parent 33875d36fa
commit c5ea29af1b
5 changed files with 131 additions and 0 deletions

View File

@ -19,7 +19,9 @@ use Zoomyboy\LaravelNami\Data\Member;
use Zoomyboy\LaravelNami\Data\MemberEntry;
use Zoomyboy\LaravelNami\Data\Membership;
use Zoomyboy\LaravelNami\Data\MembershipEntry;
use Zoomyboy\LaravelNami\Data\SearchLayerOption;
use Zoomyboy\LaravelNami\Data\Subactivity;
use Zoomyboy\LaravelNami\Enum\SearchLayer;
use Zoomyboy\LaravelNami\Exceptions\ConflictException;
use Zoomyboy\LaravelNami\Exceptions\HttpException;
use Zoomyboy\LaravelNami\Exceptions\MemberDataCorruptedException;
@ -84,6 +86,17 @@ class Api
);
}
/**
* @return Collection<int, SearchLayerOption>
*/
public function searchLayerOptions(SearchLayer $layer, ?int $parentId = null): Collection
{
return $this->fetchCollection(
$layer->url($parentId),
'Fetching search layer options failed'
)->map(fn ($option) => SearchLayerOption::from($option));
}
/**
* @param array<string, mixed> $payload
*

View File

@ -0,0 +1,7 @@
<?php
namespace Zoomyboy\LaravelNami\Data;
class SearchLayerOption extends EnumData
{
}

18
src/Enum/SearchLayer.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace Zoomyboy\LaravelNami\Enum;
enum SearchLayer : int
{
case ROOT = 0;
case FEDERAL = 1;
case REGION = 2;
public function url(?int $parent = null): string
{
return match ($this) {
static::ROOT => '/ica/rest/nami/search-multi/ebene/1',
static::FEDERAL => '/ica/rest/nami/search-multi/ebene/2/gruppierung1/'.($parent ?: ''),
static::REGION => '/ica/rest/nami/search-multi/ebene/3/gruppierung2/'.($parent ?: ''),
};
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace Zoomyboy\LaravelNami\Fakes;
use Illuminate\Support\Facades\Http;
class SearchLayerFake extends Fake
{
/**
* @param array<int, array{descriptor: string, id: int}> $data
*/
public function fetches(string $resource, array $data): self
{
Http::fake(function ($request) use ($resource, $data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/ebene/'.$resource) {
return $this->collection(collect($data));
}
});
return $this;
}
public function assertFetched(string $resource): void
{
Http::assertSent(function ($request) use ($resource) {
return $request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/ebene/'.$resource && 'GET' === $request->method();
});
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Data\SearchLayerOption;
use Zoomyboy\LaravelNami\Enum\SearchLayer;
use Zoomyboy\LaravelNami\Fakes\SearchLayerFake;
use Zoomyboy\LaravelNami\Tests\TestCase;
class SearchLayerTest extends TestCase
{
public function testItGetsSearchLayerOptionsForFirstLayer(): void
{
app(SearchLayerFake::class)->fetches('1', [
['descriptor' => 'aa', 'id' => 5],
['descriptor' => 'bb', 'id' => 6],
]);
$options = $this->login()->searchLayerOptions(SearchLayer::ROOT);
$this->assertCount(2, $options);
$this->assertInstanceOf(SearchLayerOption::class, $options->first());
$this->assertEquals('aa', $options->first()->name);
$this->assertEquals(5, $options->first()->id);
Http::assertSentCount(1);
app(SearchLayerFake::class)->assertFetched('1');
}
public function testItGetsSecondLayer(): void
{
app(SearchLayerFake::class)->fetches('2/gruppierung1/50', [
['descriptor' => 'kind', 'id' => 15],
]);
$options = $this->login()->searchLayerOptions(SearchLayer::FEDERAL, 50);
$this->assertCount(1, $options);
$this->assertInstanceOf(SearchLayerOption::class, $options->first());
$this->assertEquals('kind', $options->first()->name);
$this->assertEquals(15, $options->first()->id);
Http::assertSentCount(1);
app(SearchLayerFake::class)->assertFetched('2/gruppierung1/50');
}
public function testItGetsThirdLayer(): void
{
app(SearchLayerFake::class)->fetches('3/gruppierung2/100', [
['descriptor' => 'kinda', 'id' => 16],
]);
$options = $this->login()->searchLayerOptions(SearchLayer::REGION, 100);
$this->assertCount(1, $options);
$this->assertInstanceOf(SearchLayerOption::class, $options->first());
$this->assertEquals('kinda', $options->first()->name);
$this->assertEquals(16, $options->first()->id);
Http::assertSentCount(1);
app(SearchLayerFake::class)->assertFetched('3/gruppierung2/100');
}
}