This commit is contained in:
Philipp Lang 2023-02-17 18:55:26 +01:00
parent 3c3d0df50e
commit 0ee7755077
6 changed files with 34 additions and 285 deletions

View File

@ -111,16 +111,6 @@ class Api
return $this->authenticator->isLoggedIn();
}
public function membersOf(int $groupId): Collection
{
$this->assertLoggedIn();
return $this->fetchCollection(
'/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/'.$groupId.'/flist',
'Member fetch failed'
);
}
public function putMember(Member $member, ?int $firstActivity = null, ?int $firstSubactivity = null): int
{
$this->assertLoggedIn();
@ -237,7 +227,7 @@ class Api
}
/**
* @return Collection<Course>
* @return Collection<int, Course>
*/
public function coursesOf(int $memberId): Collection
{
@ -344,6 +334,9 @@ class Api
return $this->groups()->first(fn ($group) => $group->id == $groupId);
}
/**
* @return Collection<int, Gender>
*/
public function genders(): Collection
{
$this->assertLoggedIn();
@ -354,6 +347,9 @@ class Api
->filter(fn ($gender) => !$gender->isNull);
}
/**
* @return Collection<int, Nationality>
*/
public function nationalities(): Collection
{
$this->assertLoggedIn();
@ -362,6 +358,9 @@ class Api
->map(fn ($nationality) => Nationality::fromNami($nationality));
}
/**
* @return Collection<int, Country>
*/
public function countries(): Collection
{
$this->assertLoggedIn();
@ -370,6 +369,9 @@ class Api
->map(fn ($country) => Country::fromNami($country));
}
/**
* @return Collection<int, Region>
*/
public function regions(): Collection
{
$this->assertLoggedIn();
@ -378,6 +380,9 @@ class Api
->map(fn ($region) => Region::fromNami($region));
}
/**
* @return Collection<int, Fee>
*/
public function feesOf(int $groupid): Collection
{
$this->assertLoggedIn();
@ -386,6 +391,9 @@ class Api
->map(fn ($fee) => Fee::fromNami($fee));
}
/**
* @return Collection<int, Confession>
*/
public function confessions(): Collection
{
$this->assertLoggedIn();

View File

@ -7,12 +7,19 @@ use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Assert;
use Zoomyboy\LaravelNami\LoginException;
/**
* @template Account of array{mglnr: int, password: string}
*/
class FakeCookie extends Authenticator
{
/** @var array<int, Account> */
private array $validAccounts = [];
public ?array $invalidAccounts = null;
/** @var Account|null */
public ?array $authenticated = null;
/**
* @return self<Account>
*/
public function login(int $mglnr, string $password): self
{
$authenticated = collect($this->validAccounts)->search(
@ -50,14 +57,6 @@ class FakeCookie extends Authenticator
$this->validAccounts[] = ['mglnr' => $mglnr, 'password' => $password];
}
/**
* Reisters an account that cannot login with the given password.
*/
public function fails(int $mglnr, string $password): void
{
$this->invalidAccounts[] = ['mglnr' => $mglnr, 'password' => $password];
}
public function assertLoggedInWith(int $mglnr, string $password): void
{
Assert::assertSame($mglnr, data_get($this->authenticated, 'mglnr'));

View File

@ -1,264 +0,0 @@
<?php
namespace Zoomyboy\LaravelNami\Backend;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Fakes\CourseFake;
class FakeBackend
{
/**
* @param array <string, mixed> $data
*/
public function addSearch(int $mitgliedsNr, array $data): self
{
Http::fake(function ($request) use ($data, $mitgliedsNr) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode(['mitgliedsNummber' => $mitgliedsNr]) ?: '{}').'&page=1&start=0&limit=100') {
$content = [
'success' => true,
'data' => [$data],
'responseType' => 'OK',
'totalEntries' => 1,
];
return Http::response(json_encode($content) ?: '{}', 200);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeNationalities(array $data): self
{
Http::fake(function ($request) use ($data) {
if ('https://nami.dpsg.de/ica/rest/baseadmin/staatsangehoerigkeit' === $request->url()) {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<string, string|int|true> $data
*/
public function fakeMember(array $data): self
{
return $this->fakeMembers([$data]);
}
public function singleMemberUrl(int $gruppierungId, int $memberId): string
{
return "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$gruppierungId}/{$memberId}";
}
/**
* @param array<int, array<string, mixed>> $data
*/
public function fakeMembers(array $data): self
{
Http::fake(function ($request) use ($data) {
foreach ($data as $member) {
if ($request->url() === $this->singleMemberUrl($member['gruppierungId'], $member['id']) && 'GET' === $request->method()) {
$content = [
'success' => true,
'data' => $member,
];
return Http::response(json_encode($content) ?: '{}', 200);
}
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$member['id']}/flist") {
$content = [
'success' => true,
'data' => array_map(function ($membership) {
return (object) [
'entries_aktivVon' => $membership['aktivVon'],
'entries_aktivBis' => $membership['aktivBis'],
'entries_gruppierung' => $membership['gruppierung'],
'id' => $membership['id'],
'entries_taetigkeit' => $membership['taetigkeit'],
'entries_untergliederung' => $membership['untergliederung'],
];
}, $member['memberships'] ?? []),
];
return Http::response(json_encode($content) ?: '{}', 200);
}
}
foreach (collect($data)->chunk(100) as $i => $chunk) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('{}').'&page='.($i + 1).'&start='.($i * 100).'&limit=100') {
return Http::response(json_encode([
'success' => true,
'totalEntries' => count($data),
'data' => collect($chunk)->map(fn ($member) => [
'entries_id' => $member['id'],
'entries_gruppierungId' => $member['gruppierungId'],
])->toArray(),
]) ?: '{}', 200);
}
}
});
foreach ($data as $member) {
$courseFake = app(CourseFake::class)
->fetches($member['id'], collect(data_get($member, 'courses', []))->pluck('id')->toArray());
foreach (data_get($member, 'courses', []) as $course) {
$courseFake->shows($member['id'], $course);
}
}
return $this;
}
public function fakeSingleMembership(int $memberId, int $membershipId, array $data)
{
Http::fake(function ($request) use ($data, $memberId, $membershipId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}/{$membershipId}") {
$content = [
'success' => true,
'data' => $data,
'responseType' => 'OK',
];
return Http::response(json_encode($content) ?: '{}', 200);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeCountries(array $data): self
{
Http::fake(function ($request) use ($data) {
if ('https://nami.dpsg.de/ica/rest/baseadmin/land' === $request->url()) {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeCourses(array $data): self
{
Http::fake(function ($request) use ($data) {
if ('https://nami.dpsg.de/ica/rest/module/baustein' === $request->url()) {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeGenders(array $data): self
{
Http::fake(function ($request) use ($data) {
if ('https://nami.dpsg.de/ica/rest/baseadmin/geschlecht' === $request->url()) {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeRegions(array $data): self
{
Http::fake(function ($request) use ($data) {
if ('https://nami.dpsg.de/ica/rest/baseadmin/region' === $request->url()) {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeActivities(int $groupId, array $data): self
{
Http::fake(function ($request) use ($data, $groupId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/{$groupId}") {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array<int, array{name: string, id: int}>> $matches
*/
public function fakeSubactivities(array $matches): self
{
Http::fake(function ($request) use ($matches) {
foreach ($matches as $activityId => $data) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/{$activityId}") {
return $this->dataResponse($data);
}
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeFees(int $groupId, array $data): self
{
Http::fake(function ($request) use ($data, $groupId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/namiBeitrag/beitragsartmgl/gruppierung/{$groupId}") {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeConfessions(array $data): self
{
Http::fake(function ($request) use ($data) {
if ('https://nami.dpsg.de/ica/rest/baseadmin/konfession' === $request->url()) {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
private function dataResponse(array $data): PromiseInterface
{
$content = [
'success' => true,
'data' => collect($data)->map(fn ($item) => ['descriptor' => $item['name'], 'id' => $item['id'], 'name' => ''])->toArray(),
'responseType' => 'OK',
'totalEntries' => count($data),
];
return Http::response(json_encode($content) ?: '{}', 200);
}
}

View File

@ -16,6 +16,9 @@ abstract class Fake
]));
}
/**
* @param Collection<int, mixed> $collection
*/
public function collection(Collection $collection): PromiseInterface
{
return Http::response(json_encode([

View File

@ -12,6 +12,8 @@ class Paginator
* @param callable(int): Response $responseFactory
* @param callable(Response): Generator $generator
* @param callable(Response): int $totalFetcher
*
* @return LazyCollection<int, mixed>
*/
public function result(callable $responseFactory, callable $generator, callable $totalFetcher): LazyCollection
{
@ -27,6 +29,8 @@ class Paginator
* @param callable(int, int): Response $responseFactory
* @param callable(Response): Generator $generator
* @param callable(Response): int $totalFetcher
*
* @return LazyCollection<int, mixed>
*/
public function startResult(int $perPage, callable $responseFactory, callable $generator, callable $totalFetcher): LazyCollection
{

View File

@ -48,7 +48,6 @@ class TestCase extends \Orchestra\Testbench\TestCase
public function loginWithWrongCredentials(): Api
{
Auth::fake();
Auth::fails(12345, 'wrong');
return Nami::login(12345, 'wrong');
}