diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e6f52..1a4055e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 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 +- Fixed ImprintBuilder cannot return imprint with find ## v1.3.0 - Added a throw to stub responses 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/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/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 eed250e..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,24 +136,52 @@ 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)) { 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); } @@ -157,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/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'); } } 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 e897786..c85d4c7 100644 --- a/src/Services/OnOfficeService.php +++ b/src/Services/OnOfficeService.php @@ -12,9 +12,9 @@ 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; use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType; use Innobrain\OnOfficeAdapter\Exceptions\OnOfficeException; use Throwable; @@ -68,7 +68,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( @@ -97,17 +97,8 @@ 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 { - if (! empty($this->getApiClaim())) { - $parameters = array_replace([self::EXTENDEDCLAIM => $this->getApiClaim()], $parameters); - } - + public function requestApi(OnOfficeRequest $request): Response + { $retryOnlyOnConnectionError = static function ($exception): bool { return $exception instanceof ConnectionException; }; @@ -123,25 +114,8 @@ 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) { - $response = Http::onOffice() - ->post('/', [ - 'token' => $this->getToken(), - 'request' => [ - 'actions' => [ - [ - 'actionid' => $actionId->value, - 'resourceid' => $resourceId instanceof OnOfficeResourceId ? $resourceId->value : $resourceId, - 'resourcetype' => $resourceType->value, - 'identifier' => $identifier, - 'timestamp' => Carbon::now()->timestamp, - 'hmac' => $this->getHmac($actionId, $resourceType), - 'hmac_version' => 2, - 'parameters' => $parameters, - ], - ], - ], - ]); + retry($this->getRetryCount(), function () use ($request, &$response) { + $response = Http::onOffice()->post('/', $request->toRequestArray()); $this->throwIfResponseIsFailed($response); }, $this->getRetryDelay(), $retryOnlyOnConnectionError); 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)); }); 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); + }); +}); 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); }); });