Skip to content

Commit

Permalink
Merge pull request #9 from innobraingmbh/bug/fake-pages
Browse files Browse the repository at this point in the history
feat: add better handling for page sizes in stubs
  • Loading branch information
Katalam authored Oct 1, 2024
2 parents 27e230b + 6068bee commit f761430
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/Dtos/OnOfficeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function isEmpty(): bool
{
return $this->pages->isEmpty();
}

public function count(): int
{
return $this->pages->count();
}
}
45 changes: 35 additions & 10 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Traits\Conditionable;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponsePage;
use Innobrain\OnOfficeAdapter\Exceptions\OnOfficeException;
use Innobrain\OnOfficeAdapter\Exceptions\StrayRequestException;
use Innobrain\OnOfficeAdapter\Query\Concerns\BuilderInterface;
Expand Down Expand Up @@ -86,6 +87,16 @@ class Builder implements BuilderInterface
*/
protected BaseRepository $repository;

/**
* The last request that was made. Needed for the stubbing.
*/
private ?OnOfficeRequest $requestCache = null;

/**
* The last stub response.
*/
private ?OnOfficeResponse $responseCache = null;

public function __construct() {}

protected function getOnOfficeService(): OnOfficeService
Expand Down Expand Up @@ -157,7 +168,21 @@ protected function requestAll(OnOfficeRequest $request): Collection
data_set($request->parameters, OnOfficeService::LISTOFFSET, $offset);

return $this->requestApi($request);
}, pageSize: $this->pageSize, offset: $this->offset, limit: $this->limit);
}, pageSize: $this->pageSize, offset: $this->offset, limit: $this->limit, pageOverwrite: $this->getPageOverwrite());
}

/**
* To have a more flexible way to overwrite the page sizes
* in stub responses.
* We need to determine the page size by
* counting the number of pages in the stub response.
*/
private function getPageOverwrite(): ?int
{
/** @var ?OnOfficeResponse $stub */
$stub = ($this->stubCallables ?? collect())->first();

return $stub?->count();
}

/**
Expand All @@ -173,28 +198,28 @@ protected function requestAllChunked(OnOfficeRequest $request, callable $callbac
data_set($request->parameters, OnOfficeService::LISTOFFSET, $offset);

return $this->requestApi($request);
}, $callback, pageSize: $this->pageSize, offset: $this->offset, limit: $this->limit);
}, $callback, pageSize: $this->pageSize, offset: $this->offset, limit: $this->limit, pageOverwrite: $this->getPageOverwrite());
}

/**
* @throws JsonException
*/
protected function getStubCallable(OnOfficeRequest $request): ?Response
{
$response = ($this->stubCallables ?? collect())->first();
// if the request is different from the last one, reset the response cache
if ($this->requestCache !== $request) {
$this->requestCache = $request;
$this->responseCache = ($this->stubCallables ?? collect())->shift();
}

($this->stubCallables ?? collect())->shift();
/** @var ?OnOfficeResponsePage $response */
$response = $this->responseCache?->shift();

if (is_null($response)) {
return null;
}

/** @var OnOfficeResponse $response */
if ($response->isEmpty()) {
return null;
}

$response = $response->shift()->toResponse();
$response = $response->toResponse();

$response = new Psr7Response(200, [], json_encode($response, JSON_THROW_ON_ERROR));

Expand Down
10 changes: 6 additions & 4 deletions src/Services/OnOfficeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,17 @@ public function requestAll(
int $pageSize = 500,
int $offset = 0,
int $limit = -1,
?int $pageOverwrite = null,
): Collection {
$maxPage = 0;
$maxPage = $pageOverwrite ?? 0;
$data = new Collection;
do {
try {
$response = $request($pageSize, $offset);
} catch (OnOfficeException $exception) {
Log::error("{$exception->getMessage()} - {$exception->getCode()}");

if ($maxPage === 0) {
if ($maxPage === 0 || $pageOverwrite !== null) {
throw $exception;
}

Expand Down Expand Up @@ -190,16 +191,17 @@ public function requestAllChunked(
int $pageSize = 500,
int $offset = 0,
int $limit = -1,
?int $pageOverwrite = null,
): void {
$maxPage = 0;
$maxPage = $pageOverwrite ?? 0;
$elementCount = 0;
do {
try {
$response = $request($pageSize, $offset);
} catch (OnOfficeException $exception) {
Log::error("{$exception->getMessage()} - {$exception->getCode()}");

if ($maxPage === 0) {
if ($maxPage === 0 || $pageOverwrite !== null) {
throw $exception;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Dtos/OnOfficeResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
$response = new OnOfficeResponse(collect([$page]));

expect($response->shift())->toBeInstanceOf(OnOfficeResponsePage::class)
->and($response->count())->toBe(0)
->and($response->isEmpty())->toBeTrue();
});
97 changes: 97 additions & 0 deletions tests/Repositories/BaseRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType;
use Innobrain\OnOfficeAdapter\Facades\Testing\RecordFactories\BaseFactory;
use Innobrain\OnOfficeAdapter\Repositories\BaseRepository;

describe('stray requests', function () {
Expand Down Expand Up @@ -143,6 +144,102 @@

expect($m->getValue($builder)->toArray())->toHaveCount(20);
});

it('can fake a response with more than one page', function () {
$builder = new BaseRepository;

$builder->fake([
$builder->response([
$builder->page(recordFactories: [
BaseFactory::make(),
]),
$builder->page(recordFactories: [
BaseFactory::make(),
]),
]),
]);

$result = $builder->query()->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

expect($result->count())->toBe(2);
});

it('can fake a response with more than one page and another response', function () {
$builder = new BaseRepository;

$builder->fake([
$builder->response([
$builder->page(recordFactories: [
BaseFactory::make(),
]),
$builder->page(recordFactories: [
BaseFactory::make(),
]),
]),
$builder->response([
$builder->page(recordFactories: [
BaseFactory::make(),
]),
]),
]);

$result = $builder->query()->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

expect($result->count())->toBe(2);

$result = $builder->query()->call(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
));

expect($result->count())->toBe(1);
});

it('can fake a response with more than one page and another response on each page', function () {
$builder = new BaseRepository;

$builder->fake([
$builder->response([
$builder->page(recordFactories: [
BaseFactory::make(),
]),
$builder->page(recordFactories: [
BaseFactory::make(),
]),
]),
$builder->response([
$builder->page(recordFactories: [
BaseFactory::make(),
]),
]),
]);

$result = [];
$builder->query()->chunked(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
), function (array $records) use (&$result) {
$result[] = count($records);
});

expect($result[0])->toBe(1)
->and($result[1])->toBe(1);

$builder->query()->chunked(new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
), function (array $records) use (&$result) {
$result[] = count($records);
});

expect($result[2])->toBe(1);
});
});

describe('assert', function () {
Expand Down

0 comments on commit f761430

Please sign in to comment.