From 50f2ec68206835b2c4267a5f7f5deb64c3959fcd Mon Sep 17 00:00:00 2001 From: Konstantin Auffinger Date: Thu, 10 Oct 2024 20:11:10 +0200 Subject: [PATCH] feat: get filters for address and estate --- src/Enums/OnOfficeResourceType.php | 1 + src/Facades/FilterRepository.php | 30 +++++++ .../Testing/RecordFactories/FilterFactory.php | 10 +++ src/Query/FilterBuilder.php | 82 +++++++++++++++++++ src/Repositories/FilterRepository.php | 15 ++++ src/Services/OnOfficeParameterConst.php | 2 + tests/Repositories/FilterRepositoryTest.php | 53 ++++++++++++ tests/Stubs/GetFiltersResponse.php | 50 +++++++++++ 8 files changed, 243 insertions(+) create mode 100644 src/Facades/FilterRepository.php create mode 100644 src/Facades/Testing/RecordFactories/FilterFactory.php create mode 100644 src/Query/FilterBuilder.php create mode 100644 src/Repositories/FilterRepository.php create mode 100644 tests/Repositories/FilterRepositoryTest.php create mode 100644 tests/Stubs/GetFiltersResponse.php diff --git a/src/Enums/OnOfficeResourceType.php b/src/Enums/OnOfficeResourceType.php index 6d02e6c..fdfe836 100644 --- a/src/Enums/OnOfficeResourceType.php +++ b/src/Enums/OnOfficeResourceType.php @@ -9,6 +9,7 @@ enum OnOfficeResourceType: string case Address = 'address'; case Estate = 'estate'; case Fields = 'fields'; + case Filters = 'filters'; case File = 'file'; case IdsFromRelation = 'idsfromrelation'; case User = 'user'; diff --git a/src/Facades/FilterRepository.php b/src/Facades/FilterRepository.php new file mode 100644 index 0000000..d0626e4 --- /dev/null +++ b/src/Facades/FilterRepository.php @@ -0,0 +1,30 @@ +fake($stubCallables); + }); + } + + protected static function getFacadeAccessor(): string + { + return RootRepository::class; + } +} diff --git a/src/Facades/Testing/RecordFactories/FilterFactory.php b/src/Facades/Testing/RecordFactories/FilterFactory.php new file mode 100644 index 0000000..aaa1276 --- /dev/null +++ b/src/Facades/Testing/RecordFactories/FilterFactory.php @@ -0,0 +1,10 @@ + $this->module, + ] + ); + + return $this->requestAll($request); + } + + /** + * @throws Throwable + */ + public function first(): ?array + { + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Filters, + parameters: [ + 'module' => $this->module, + ] + ); + + return $this->requestApi($request) + ->json('response.results.0.data.records.0'); + + } + + /** + * @throws OnOfficeException + */ + public function each(callable $callback): void + { + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Filters, + parameters: [ + OnOfficeService::MODULE => $this->module, + ], + ); + + $this->requestAllChunked($request, $callback); + } + + public function estate(): static + { + $this->module = 'estate'; + + return $this; + } + + public function address(): static + { + $this->module = 'address'; + + return $this; + } +} diff --git a/src/Repositories/FilterRepository.php b/src/Repositories/FilterRepository.php new file mode 100644 index 0000000..e875cd2 --- /dev/null +++ b/src/Repositories/FilterRepository.php @@ -0,0 +1,15 @@ +id(1) + ->data([ + 'scope' => 'office', + 'name' => 'Büroadressen', + 'userId' => null, + 'groupId' => 195, + ]), + ]), + ])); + + $response = FilterRepository::query()->address()->get(); + + expect($response->count())->toBe(1) + ->and($response->first()['id'])->toBe(1); + + FilterRepository::assertSentCount(1); + }); +}); + +describe('real responses', function () { + test('get', function () { + Http::preventStrayRequests(); + Http::fake([ + 'https://api.onoffice.de/api/stable/api.php/' => Http::sequence([ + GetFiltersResponse::make(count: 1500), + GetFiltersResponse::make(count: 1500), + GetFiltersResponse::make(count: 1500), + ]), + ]); + + FilterRepository::record(); + + $response = FilterRepository::query()->estate()->get(); + + expect($response->count())->toBe(4500); + + FilterRepository::assertSentCount(3); + }); +}); diff --git a/tests/Stubs/GetFiltersResponse.php b/tests/Stubs/GetFiltersResponse.php new file mode 100644 index 0000000..23e0eb9 --- /dev/null +++ b/tests/Stubs/GetFiltersResponse.php @@ -0,0 +1,50 @@ + fake()->randomNumber(), + 'type' => 'filter', + 'elements' => [ + 'scope' => fake()->randomElement(['estate', 'address']), + 'name' => fake()->words(3, true), + 'userId' => fake()->optional()->randomNumber(), + 'groupId' => fake()->randomNumber(), + ], + ]); + + return array_merge_recursive([ + 'status' => [ + 'code' => 200, + 'errorcode' => 0, + 'message' => 'OK', + ], + 'response' => [ + 'results' => [ + [ + 'data' => [ + 'meta' => [ + 'cntabsolute' => $count, + ], + 'records' => $records, + ], + ], + ], + ], + ], $data); + } +}