From 87f46d9e6714747f26cbd30e14b595408dc70bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20G=C3=B6r=C3=9F?= Date: Fri, 11 Oct 2024 15:45:20 +0200 Subject: [PATCH 1/6] ref: remove the unpack of the request when passed to on office service --- src/Query/Builder.php | 8 +- src/Services/OnOfficeService.php | 26 ++--- tests/Services/OnOfficeServiceTest.php | 146 +++++++++++++++---------- 3 files changed, 99 insertions(+), 81 deletions(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index eed250e..d5765fa 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -141,13 +141,7 @@ public function requestApi(OnOfficeRequest $request): Response if (is_null($response)) { throw_if($this->preventStrayRequests, new StrayRequestException(request: $request)); - $response = $this->getOnOfficeService()->requestApi( - $request->actionId, - $request->resourceType, - $request->resourceId, - $request->identifier, - $request->parameters, - ); + $response = $this->getOnOfficeService()->requestApi($request); } else { $this->getOnOfficeService()->throwIfResponseIsFailed($response); } diff --git a/src/Services/OnOfficeService.php b/src/Services/OnOfficeService.php index e897786..234dfc1 100644 --- a/src/Services/OnOfficeService.php +++ b/src/Services/OnOfficeService.php @@ -12,6 +12,7 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; +use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest; use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction; use Innobrain\OnOfficeAdapter\Enums\OnOfficeError; use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceId; @@ -97,15 +98,10 @@ private function getHmac(OnOfficeAction $actionId, OnOfficeResourceType $resourc * @throws OnOfficeException * @throws Throwable */ - public function requestApi( - OnOfficeAction $actionId, - OnOfficeResourceType $resourceType, - OnOfficeResourceId|string|int $resourceId = OnOfficeResourceId::None, - string|int $identifier = '', - array $parameters = [], - ): Response { + public function requestApi(OnOfficeRequest $request): Response + { if (! empty($this->getApiClaim())) { - $parameters = array_replace([self::EXTENDEDCLAIM => $this->getApiClaim()], $parameters); + $request->parameters = array_replace([self::EXTENDEDCLAIM => $this->getApiClaim()], $request->parameters); } $retryOnlyOnConnectionError = static function ($exception): bool { @@ -123,21 +119,21 @@ public function requestApi( * To avoid this, we need to retry the request with payload creation until we get a valid response. */ $response = null; - retry($this->getRetryCount(), function () use ($parameters, $identifier, $resourceType, $resourceId, $actionId, &$response) { + retry($this->getRetryCount(), function () use ($request, &$response) { $response = Http::onOffice() ->post('/', [ 'token' => $this->getToken(), 'request' => [ 'actions' => [ [ - 'actionid' => $actionId->value, - 'resourceid' => $resourceId instanceof OnOfficeResourceId ? $resourceId->value : $resourceId, - 'resourcetype' => $resourceType->value, - 'identifier' => $identifier, + 'actionid' => $request->actionId->value, + 'resourceid' => $request->resourceId instanceof OnOfficeResourceId ? $request->resourceId->value : $request->resourceId, + 'resourcetype' => $request->resourceType->value, + 'identifier' => $request->identifier, 'timestamp' => Carbon::now()->timestamp, - 'hmac' => $this->getHmac($actionId, $resourceType), + 'hmac' => $this->getHmac($request->actionId, $request->resourceType), 'hmac_version' => 2, - 'parameters' => $parameters, + 'parameters' => $request->parameters, ], ], ], diff --git a/tests/Services/OnOfficeServiceTest.php b/tests/Services/OnOfficeServiceTest.php index fde7962..35517d7 100644 --- a/tests/Services/OnOfficeServiceTest.php +++ b/tests/Services/OnOfficeServiceTest.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; +use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest; use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction; use Innobrain\OnOfficeAdapter\Enums\OnOfficeError; use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType; @@ -71,10 +72,12 @@ $onOfficeService = app(OnOfficeService::class); - $onOfficeService->requestApi( + $request = new OnOfficeRequest( OnOfficeAction::Get, OnOfficeResourceType::Estate, ); + + $onOfficeService->requestApi($request); }) ->throws(OnOfficeException::class) ->with([300, 301, 400, 401, 500, 501]); @@ -93,12 +96,13 @@ $onOfficeService = app(OnOfficeService::class); - expect( - fn () => $onOfficeService->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ) - )->toThrow(OnOfficeException::class, 'Customer unknown!'); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + + expect(fn () => $onOfficeService->requestApi($request)) + ->toThrow(OnOfficeException::class, 'Customer unknown!'); }); it('throws an exception on failed request inside response', function () { @@ -109,12 +113,14 @@ $onOfficeService = app(OnOfficeService::class); - expect(/** - * @throws OnOfficeException - */ static fn () => $onOfficeService->requestApi( + $request = new OnOfficeRequest( OnOfficeAction::Get, OnOfficeResourceType::Estate, - ) + ); + + expect(/** + * @throws OnOfficeException + */ static fn () => $onOfficeService->requestApi($request) )->toThrow(OnOfficeException::class, 'The HMAC is invalid'); }); @@ -131,12 +137,14 @@ $onOfficeService = app(OnOfficeService::class); - expect(/** - * @throws OnOfficeException - */ static fn () => $onOfficeService->requestApi( + $request = new OnOfficeRequest( OnOfficeAction::Get, OnOfficeResourceType::Estate, - ) + ); + + expect(/** + * @throws OnOfficeException + */ static fn () => $onOfficeService->requestApi($request) )->toThrow(OnOfficeException::class, $message); })->with([ [32, 64, 'The HMAC is invalid'], @@ -181,12 +189,14 @@ $onOfficeService = app(OnOfficeService::class); - expect(/** - * @throws OnOfficeException - */ static fn () => $onOfficeService->requestApi( + $request = new OnOfficeRequest( OnOfficeAction::Get, OnOfficeResourceType::Estate, - ) + ); + + expect(/** + * @throws OnOfficeException + */ static fn () => $onOfficeService->requestApi($request) )->toThrow(OnOfficeException::class, 'Empty request'); })->with([ [32, 64], @@ -204,11 +214,13 @@ $onOfficeService = app(OnOfficeService::class); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + try { - $onOfficeService->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $onOfficeService->requestApi($request); } catch (OnOfficeException $exception) { expect($exception->getError())->toBe(OnOfficeError::The_HMAC_Is_Invalid); } @@ -233,11 +245,13 @@ $onOfficeService = app(OnOfficeService::class); - $onOfficeService->requestAll(function () { - app(OnOfficeService::class)->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + + $onOfficeService->requestAll(function () use ($request) { + app(OnOfficeService::class)->requestApi($request); }); })->with([300, 301, 400, 401, 500, 501])->throws(OnOfficeException::class); @@ -264,11 +278,13 @@ $onOfficeService = app(OnOfficeService::class); - $response = $onOfficeService->requestAll(function () { - return app(OnOfficeService::class)->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + + $response = $onOfficeService->requestAll(function () use ($request) { + return app(OnOfficeService::class)->requestApi($request); }); expect($response)->toBeInstanceOf(Collection::class) @@ -309,11 +325,13 @@ $onOfficeService = app(OnOfficeService::class); - $response = $onOfficeService->requestAll(function () { - return app(OnOfficeService::class)->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + + $response = $onOfficeService->requestAll(function () use ($request) { + return app(OnOfficeService::class)->requestApi($request); }, limit: 1); expect($response)->toBeInstanceOf(Collection::class) @@ -339,11 +357,13 @@ $onOfficeService = app(OnOfficeService::class); - $onOfficeService->requestAllChunked(function () { - app(OnOfficeService::class)->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + + $onOfficeService->requestAllChunked(function () use ($request) { + app(OnOfficeService::class)->requestApi($request); }, function () {}); })->with([300, 301, 400, 401, 500, 501])->throws(OnOfficeException::class); @@ -361,11 +381,13 @@ $callback = Mockery::mock(); $callback->shouldReceive('call')->once(); - $onOfficeService->requestAllChunked(function () { - return app(OnOfficeService::class)->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + + $onOfficeService->requestAllChunked(function () use ($request) { + return app(OnOfficeService::class)->requestApi($request); }, function () use ($callback) { $callback->call(); }); @@ -405,12 +427,14 @@ $onOfficeService = app(OnOfficeService::class); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + $count = 0; - $onOfficeService->requestAllChunked(function () { - return app(OnOfficeService::class)->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $onOfficeService->requestAllChunked(function () use ($request) { + return app(OnOfficeService::class)->requestApi($request); }, function ($elements) use (&$count) { $count += count($elements); }, limit: 1); @@ -475,12 +499,14 @@ $onOfficeService = app(OnOfficeService::class); + $request = new OnOfficeRequest( + OnOfficeAction::Get, + OnOfficeResourceType::Estate, + ); + $count = 0; - $onOfficeService->requestAllChunked(function () { - return app(OnOfficeService::class)->requestApi( - OnOfficeAction::Get, - OnOfficeResourceType::Estate, - ); + $onOfficeService->requestAllChunked(function () use ($request) { + return app(OnOfficeService::class)->requestApi($request); }, function ($elements) use (&$count) { $count += count($elements); }, pageSize: 2, limit: 3); @@ -548,11 +574,13 @@ $onOfficeService = app(OnOfficeService::class); - $onOfficeService->requestApi( + $request = new OnOfficeRequest( OnOfficeAction::Get, OnOfficeResourceType::Estate, ); + $onOfficeService->requestApi($request); + Http::assertSentCount(2); }); }); From 4e29dc1b6d6610b660cdac6d3cc39cbf0fbcbde9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20G=C3=B6r=C3=9F?= Date: Fri, 11 Oct 2024 15:54:38 +0200 Subject: [PATCH 2/6] ref: add request formatting to request class --- src/Dtos/OnOfficeRequest.php | 32 ++++++++++++++++++++++++++++++++ src/Services/OnOfficeService.php | 25 ++----------------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/Dtos/OnOfficeRequest.php b/src/Dtos/OnOfficeRequest.php index fd5fb97..630dfea 100644 --- a/src/Dtos/OnOfficeRequest.php +++ b/src/Dtos/OnOfficeRequest.php @@ -4,9 +4,11 @@ namespace Innobrain\OnOfficeAdapter\Dtos; +use Illuminate\Support\Carbon; use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction; use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceId; use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType; +use Innobrain\OnOfficeAdapter\Services\OnOfficeService; class OnOfficeRequest { @@ -28,4 +30,34 @@ public function toArray(): array 'parameters' => $this->parameters, ]; } + + public function toRequestArray(): array + { + /** @var OnOfficeService $onOfficeService */ + $onOfficeService = app(OnOfficeService::class); + + if (! empty($onOfficeService->getApiClaim())) { + $this->parameters = array_replace([OnOfficeService::EXTENDEDCLAIM => $onOfficeService->getApiClaim()], $this->parameters); + } + + return [ + 'token' => $onOfficeService->getToken(), + 'request' => [ + 'actions' => [ + [ + 'actionid' => $this->actionId->value, + 'resourceid' => $this->resourceId instanceof OnOfficeResourceId + ? $this->resourceId->value + : $this->resourceId, + 'resourcetype' => $this->resourceType->value, + 'identifier' => $this->identifier, + 'timestamp' => Carbon::now()->timestamp, + 'hmac' => $onOfficeService->getHmac($this->actionId, $this->resourceType), + 'hmac_version' => 2, + 'parameters' => $this->parameters, + ], + ], + ], + ]; + } } diff --git a/src/Services/OnOfficeService.php b/src/Services/OnOfficeService.php index 234dfc1..19dac02 100644 --- a/src/Services/OnOfficeService.php +++ b/src/Services/OnOfficeService.php @@ -69,7 +69,7 @@ public function retryOnlyOnConnectionError(): bool * * Read more: https://apidoc.onoffice.de/onoffice-api-request/request-elemente/action/#hmac */ - private function getHmac(OnOfficeAction $actionId, OnOfficeResourceType $resourceType): string + public function getHmac(OnOfficeAction $actionId, OnOfficeResourceType $resourceType): string { return base64_encode( hash_hmac( @@ -100,10 +100,6 @@ private function getHmac(OnOfficeAction $actionId, OnOfficeResourceType $resourc */ public function requestApi(OnOfficeRequest $request): Response { - if (! empty($this->getApiClaim())) { - $request->parameters = array_replace([self::EXTENDEDCLAIM => $this->getApiClaim()], $request->parameters); - } - $retryOnlyOnConnectionError = static function ($exception): bool { return $exception instanceof ConnectionException; }; @@ -120,24 +116,7 @@ public function requestApi(OnOfficeRequest $request): Response */ $response = null; retry($this->getRetryCount(), function () use ($request, &$response) { - $response = Http::onOffice() - ->post('/', [ - 'token' => $this->getToken(), - 'request' => [ - 'actions' => [ - [ - 'actionid' => $request->actionId->value, - 'resourceid' => $request->resourceId instanceof OnOfficeResourceId ? $request->resourceId->value : $request->resourceId, - 'resourcetype' => $request->resourceType->value, - 'identifier' => $request->identifier, - 'timestamp' => Carbon::now()->timestamp, - 'hmac' => $this->getHmac($request->actionId, $request->resourceType), - 'hmac_version' => 2, - 'parameters' => $request->parameters, - ], - ], - ], - ]); + $response = Http::onOffice()->post('/', $request->toRequestArray()); $this->throwIfResponseIsFailed($response); }, $this->getRetryDelay(), $retryOnlyOnConnectionError); From 9d11b09c5e279f5eb71a478ee95fcf402a9fe9bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20G=C3=B6r=C3=9F?= Date: Fri, 11 Oct 2024 16:18:58 +0200 Subject: [PATCH 3/6] feat: add dd, dump, raw to repositories --- README.md | 49 +++++++ docs/BaseRepository.md | 94 ++++++++++++ src/Facades/BaseRepository.php | 16 ++ src/Query/Builder.php | 53 +++++++ src/Repositories/BaseRepository.php | 15 ++ src/Services/OnOfficeService.php | 1 - tests/Repositories/BaseRepositoryTest.php | 170 ++++++++++++++++++++++ 7 files changed, 397 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index edf09e4..763c117 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,55 @@ Config::set('onoffice.secret', 'secret'); Config::set('onoffice.api_claim', 'api_claim'); ``` +### Middlewares +```php +use Illuminate\Support\Facades\Log; +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::query() + ->before(static function (OnOfficeRequest $request) { + Log::info('About to send request', [ + 'request' => $request->toArray(), + ]); + }) + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); +``` + +### Debugging +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::query() + ->dd() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); +``` +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::record(); + +BaseRepository::query() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); + +$result = BaseRepository::lastRecorded(); + +/* + $result = [ + OnOfficeRequest, + OnOfficeResponse, + ]; +*/ +``` + ### Usage in tests ```php use Innobrain\OnOfficeAdapter\Facades\EstateRepository; diff --git a/docs/BaseRepository.md b/docs/BaseRepository.md index 504fded..aa98490 100644 --- a/docs/BaseRepository.md +++ b/docs/BaseRepository.md @@ -31,3 +31,97 @@ BaseRepository::query() }, ); ``` + +## dd, dump +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::query() + ->dd() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); + +// → will dump the request and die +``` +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::query() + ->dump() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); + +// → will dump the request +``` + +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::query() + ->raw() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); + +// → will dump the request exactly as fired and die +``` + +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::record(); + +BaseRepository::query() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); + +$result = BaseRepository::lastRecorded(); + +/* + $result = [ + OnOfficeRequest, + OnOfficeResponse, + ]; +*/ +``` +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::record(); + +BaseRepository::query() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); + +$result = BaseRepository::lastRecordedRequest(); + +/* + $result = OnOfficeRequest; +*/ +``` +```php +use Innobrain\OnOfficeAdapter\Facades\BaseRepository; + +BaseRepository::record(); + +BaseRepository::query() + ->call(new OnOfficeRequest( + OnOfficeAction::Read, + OnOfficeResourceType::Estate, + )); + +$result = BaseRepository::lastRecordedResponse(); + +/* + $result = OnOfficeResponse; +*/ +``` diff --git a/src/Facades/BaseRepository.php b/src/Facades/BaseRepository.php index abeba90..39a447d 100644 --- a/src/Facades/BaseRepository.php +++ b/src/Facades/BaseRepository.php @@ -5,6 +5,7 @@ namespace Innobrain\OnOfficeAdapter\Facades; use Illuminate\Support\Facades\Facade; +use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest; use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse; use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponsePage; use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction; @@ -87,6 +88,21 @@ public static function stopRecording(): RootRepository return static::getFacadeRoot()->stopRecording(); } + public static function lastRecorded(): ?array + { + return static::getFacadeRoot()->lastRecorded(); + } + + public static function lastRecordedRequest(): ?OnOfficeRequest + { + return static::getFacadeRoot()->lastRecordedRequest(); + } + + public static function lastRecordedResponse(): ?array + { + return static::getFacadeRoot()->lastRecordedResponse(); + } + public static function assertSent(?callable $callback = null): void { static::getFacadeRoot()->assertSent($callback); diff --git a/src/Query/Builder.php b/src/Query/Builder.php index d5765fa..7f24f4b 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -19,6 +19,7 @@ use Innobrain\OnOfficeAdapter\Repositories\BaseRepository; use Innobrain\OnOfficeAdapter\Services\OnOfficeService; use JsonException; +use Symfony\Component\VarDumper\VarDumper; use Throwable; class Builder implements BuilderInterface @@ -97,6 +98,11 @@ class Builder implements BuilderInterface */ private ?OnOfficeResponse $responseCache = null; + /** + * The before sending middlewares. + */ + protected array $beforeSendingCallbacks = []; + public function __construct() {} protected function getOnOfficeService(): OnOfficeService @@ -130,12 +136,46 @@ public function preventStrayRequests(bool $value = true): static return $this; } + public function before(callable $callback): static + { + $this->beforeSendingCallbacks[] = $callback; + + return $this; + } + + public function dump(): static + { + return $this->before(static function (OnOfficeRequest $request) { + VarDumper::dump($request); + }); + } + + public function dd(): static + { + return $this->before(static function (OnOfficeRequest $request) { + VarDumper::dump($request); + + exit(1); + }); + } + + public function raw(): static + { + return $this->before(static function (OnOfficeRequest $request) { + VarDumper::dump($request->toRequestArray()); + + exit(1); + }); + } + /** * @throws OnOfficeException * @throws Throwable */ public function requestApi(OnOfficeRequest $request): Response { + $request = $this->runBeforeSendingCallbacks($request); + $response = $this->getStubCallable($request); if (is_null($response)) { @@ -151,6 +191,19 @@ public function requestApi(OnOfficeRequest $request): Response return $response; } + protected function runBeforeSendingCallbacks(OnOfficeRequest $request): OnOfficeRequest + { + return tap($request, function (OnOfficeRequest &$request) { + collect($this->beforeSendingCallbacks)->each(function (callable $callback) use (&$request) { + $result = $callback($request); + + if ($result instanceof OnOfficeRequest) { + $request = $result; + } + }); + }); + } + /** * @throws OnOfficeException */ diff --git a/src/Repositories/BaseRepository.php b/src/Repositories/BaseRepository.php index e80ffbd..f00d72f 100644 --- a/src/Repositories/BaseRepository.php +++ b/src/Repositories/BaseRepository.php @@ -186,6 +186,21 @@ public function recordRequestResponsePair(OnOfficeRequest $request, array $respo return $this; } + public function lastRecorded(): ?array + { + return collect($this->recorded)->last(); + } + + public function lastRecordedRequest(): ?OnOfficeRequest + { + return collect($this->recorded)->last()[0] ?? null; + } + + public function lastRecordedResponse(): ?array + { + return collect($this->recorded)->last()[1] ?? null; + } + /** * Get a collection of the request / response pairs matching the given truth test. */ diff --git a/src/Services/OnOfficeService.php b/src/Services/OnOfficeService.php index 19dac02..c85d4c7 100644 --- a/src/Services/OnOfficeService.php +++ b/src/Services/OnOfficeService.php @@ -15,7 +15,6 @@ use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest; use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction; use Innobrain\OnOfficeAdapter\Enums\OnOfficeError; -use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceId; use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType; use Innobrain\OnOfficeAdapter\Exceptions\OnOfficeException; use Throwable; diff --git a/tests/Repositories/BaseRepositoryTest.php b/tests/Repositories/BaseRepositoryTest.php index 85901b5..d2be56e 100644 --- a/tests/Repositories/BaseRepositoryTest.php +++ b/tests/Repositories/BaseRepositoryTest.php @@ -12,6 +12,7 @@ use Innobrain\OnOfficeAdapter\Exceptions\OnOfficeException; use Innobrain\OnOfficeAdapter\Facades\Testing\RecordFactories\BaseFactory; use Innobrain\OnOfficeAdapter\Repositories\BaseRepository; +use Symfony\Component\VarDumper\VarDumper; describe('stray requests', function () { it('will set the preventStrayRequests property when calling preventStrayRequests default', function () { @@ -108,6 +109,48 @@ expect($m->getValue($builder))->toBe([]); }); + + it('will dump the last record', function () { + $builder = new BaseRepository; + + $builder->record(); + + $request = new OnOfficeRequest(OnOfficeAction::Read, OnOfficeResourceType::Estate); + + $builder->recordRequestResponsePair($request, ['response']); + + $response = $builder->lastRecorded(); + + expect($response)->toBe([$request, ['response']]); + }); + + it('will dump the last record request', function () { + $builder = new BaseRepository; + + $builder->record(); + + $request = new OnOfficeRequest(OnOfficeAction::Read, OnOfficeResourceType::Estate); + + $builder->recordRequestResponsePair($request, ['response']); + + $response = $builder->lastRecordedRequest(); + + expect($response)->toBe($request); + }); + + it('will dump the last record response', function () { + $builder = new BaseRepository; + + $builder->record(); + + $request = new OnOfficeRequest(OnOfficeAction::Read, OnOfficeResourceType::Estate); + + $builder->recordRequestResponsePair($request, ['response']); + + $response = $builder->lastRecordedResponse(); + + expect($response)->toBe(['response']); + }); }); describe('fake', function () { @@ -393,3 +436,130 @@ Http::assertSentCount(1); }); }); + +describe('middlewares', function () { + it('will call the before callbacks', function () { + $builder = new BaseRepository; + + Http::preventStrayRequests(); + Http::fake([ + 'https://api.onoffice.de/api/stable/api.php/' => Http::response([ + 'status' => [ + 'code' => 200, + ], + ]), + ]); + + $builder->record(); + + $request = new OnOfficeRequest(OnOfficeAction::Read, OnOfficeResourceType::Estate); + + $builder->query() + ->before(function (OnOfficeRequest $request) { + $request->identifier = 'before'; + }) + ->call($request); + + $builder->assertSent(function (OnOfficeRequest $request) { + return $request->identifier === 'before'; + }); + }); + + it('will call the before callbacks in the order they are added', function () { + $builder = new BaseRepository; + + Http::preventStrayRequests(); + Http::fake([ + 'https://api.onoffice.de/api/stable/api.php/' => Http::response([ + 'status' => [ + 'code' => 200, + ], + ]), + ]); + + $builder->record(); + + $request = new OnOfficeRequest(OnOfficeAction::Read, OnOfficeResourceType::Estate); + + $builder->query() + ->before(function (OnOfficeRequest $request) { + $request->identifier = 'before'; + }) + ->before(function (OnOfficeRequest $request) { + $request->identifier = 'after_the_first_before'; + }) + ->call($request); + + $builder->assertSent(function (OnOfficeRequest $request) { + return $request->identifier === 'after_the_first_before'; + }); + }); + + it('can dump the request', function () { + $builder = new BaseRepository; + + Http::preventStrayRequests(); + Http::fake([ + 'https://api.onoffice.de/api/stable/api.php/' => Http::response([ + 'status' => [ + 'code' => 200, + ], + ]), + ]); + + $dumped = []; + + VarDumper::setHandler(static function (mixed $value) use (&$dumped) { + $dumped[] = $value; + }); + + $builder->record(); + + $request = new OnOfficeRequest(OnOfficeAction::Read, OnOfficeResourceType::Estate); + + $builder->query() + ->dump() + ->call($request); + + expect($dumped)->toHaveCount(1) + ->and($dumped[0])->toBeInstanceOf(OnOfficeRequest::class) + ->and($dumped[0]->actionId)->toBe(OnOfficeAction::Read) + ->and($dumped[0]->resourceType)->toBe(OnOfficeResourceType::Estate); + + VarDumper::setHandler(null); + }); + + it('can raw the request', function () { + $builder = new BaseRepository; + + Http::preventStrayRequests(); + Http::fake([ + 'https://api.onoffice.de/api/stable/api.php/' => Http::response([ + 'status' => [ + 'code' => 200, + ], + ]), + ]); + + $dumped = []; + + VarDumper::setHandler(static function (mixed $value) use (&$dumped) { + $dumped[] = $value; + }); + + $builder->record(); + + $request = new OnOfficeRequest(OnOfficeAction::Read, OnOfficeResourceType::Estate); + + $builder->query() + ->dump() + ->call($request); + + expect($dumped)->toHaveCount(1) + ->and($dumped[0])->toBeInstanceOf(OnOfficeRequest::class) + ->and($dumped[0]->actionId)->toBe(OnOfficeAction::Read) + ->and($dumped[0]->resourceType)->toBe(OnOfficeResourceType::Estate); + + VarDumper::setHandler(null); + }); +}); From 9884cf6ad6c187caf9ecfc75fe1cb0e6728ca4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20G=C3=B6r=C3=9F?= Date: Fri, 11 Oct 2024 16:23:35 +0200 Subject: [PATCH 4/6] test: remove test overhead from on office error test --- tests/Enums/OnOfficeErrorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Enums/OnOfficeErrorTest.php b/tests/Enums/OnOfficeErrorTest.php index 9d61867..4b3cb18 100644 --- a/tests/Enums/OnOfficeErrorTest.php +++ b/tests/Enums/OnOfficeErrorTest.php @@ -8,5 +8,5 @@ it('can stringify an error', function (OnOfficeError $error) { expect($error->toString())->toBeString() ->toBe(data_get(OnOfficeError::errorTexts(), $error->value, 'unknown error')); - })->with(OnOfficeError::cases()); + })->with(array_slice(OnOfficeError::cases(), 0, 10)); }); From d354a01fdcc47610b5b66ec4337a28acc240cbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20G=C3=B6r=C3=9F?= Date: Fri, 11 Oct 2024 16:25:06 +0200 Subject: [PATCH 5/6] chore: changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e6f52..03a7142 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog ## main +- Refactored the passing of the request to the on office service +- Added a method to get the last request response pair from the repository +- Added die and dump functions to the queries ## v1.3.0 - Added a throw to stub responses From 20546d64115e0dc0f2d4de5e25557d65ce3a4fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20G=C3=B6r=C3=9F?= Date: Fri, 11 Oct 2024 16:26:59 +0200 Subject: [PATCH 6/6] fix: imprint builder will return correct info on find --- CHANGELOG.md | 1 + src/Query/ImprintBuilder.php | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03a7142..1a4055e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Refactored the passing of the request to the on office service - Added a method to get the last request response pair from the repository - Added die and dump functions to the queries +- Fixed ImprintBuilder cannot return imprint with find ## v1.3.0 - Added a throw to stub responses diff --git a/src/Query/ImprintBuilder.php b/src/Query/ImprintBuilder.php index 55333c5..8d457b7 100644 --- a/src/Query/ImprintBuilder.php +++ b/src/Query/ImprintBuilder.php @@ -55,11 +55,11 @@ public function first(): ?array } /** - * @throws OnOfficeException + * @throws Throwable */ public function find(int $id): array { - $response = $this->onOfficeService->requestApi( + $request = new OnOfficeRequest( OnOfficeAction::Read, OnOfficeResourceType::Impressum, resourceId: $id, @@ -69,6 +69,7 @@ public function find(int $id): array ] ); - return $response->json('response.results.0.data.records.0'); + return $this->requestApi($request) + ->json('response.results.0.data.records.0'); } }