Skip to content

Commit

Permalink
feat: get filters for address and estate
Browse files Browse the repository at this point in the history
  • Loading branch information
kauffinger committed Oct 10, 2024
1 parent 1771811 commit 50f2ec6
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Enums/OnOfficeResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
30 changes: 30 additions & 0 deletions src/Facades/FilterRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Facades;

use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponsePage;
use Innobrain\OnOfficeAdapter\Query\FilterBuilder;
use Innobrain\OnOfficeAdapter\Repositories\FilterRepository as RootRepository;

/**
* @see RootRepository
*
* @method static FilterBuilder query()
*/
class FilterRepository extends BaseRepository
{
public static function fake(OnOfficeResponsePage|OnOfficeResponse|array|null $stubCallables): RootRepository
{
return tap(static::getFacadeRoot(), static function (RootRepository $fake) use ($stubCallables) {
$fake->fake($stubCallables);
});
}

protected static function getFacadeAccessor(): string
{
return RootRepository::class;
}
}
10 changes: 10 additions & 0 deletions src/Facades/Testing/RecordFactories/FilterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Facades\Testing\RecordFactories;

class FilterFactory extends BaseFactory
{
public string $type = 'filter';
}
82 changes: 82 additions & 0 deletions src/Query/FilterBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Query;

use Illuminate\Support\Collection;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType;
use Innobrain\OnOfficeAdapter\Exceptions\OnOfficeException;
use Innobrain\OnOfficeAdapter\Services\OnOfficeService;
use Throwable;

class FilterBuilder extends Builder
{
public string $module;

/**
* @throws OnOfficeException
*/
public function get(): Collection
{
$request = new OnOfficeRequest(
OnOfficeAction::Get,
OnOfficeResourceType::Filters,
parameters: [
OnOfficeService::MODULE => $this->module,
]
);

return $this->requestAll($request);
}

/**
* @throws Throwable<OnOfficeException>
*/
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;
}
}
15 changes: 15 additions & 0 deletions src/Repositories/FilterRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Repositories;

use Innobrain\OnOfficeAdapter\Query\FilterBuilder;

class FilterRepository extends BaseRepository
{
protected function createBuilder(): FilterBuilder
{
return new FilterBuilder;
}
}
2 changes: 2 additions & 0 deletions src/Services/OnOfficeParameterConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ trait OnOfficeParameterConst
public const INPUT = 'input';

public const MODUL = 'modul';

public const MODULE = 'module';
}
53 changes: 53 additions & 0 deletions tests/Repositories/FilterRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Http;
use Innobrain\OnOfficeAdapter\Facades\FilterRepository;
use Innobrain\OnOfficeAdapter\Facades\Testing\RecordFactories\FilterFactory;
use Innobrain\OnOfficeAdapter\Tests\Stubs\GetFiltersResponse;

describe('fake responses', function () {
test('get', function () {
FilterRepository::fake(FilterRepository::response([
FilterRepository::page(recordFactories: [
FilterFactory::make()
->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);
});
});
50 changes: 50 additions & 0 deletions tests/Stubs/GetFiltersResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Tests\Stubs;

use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Facades\Http;

class GetFiltersResponse
{
public static function make(array $data = [], int $count = 1): PromiseInterface
{
return Http::response(self::getBody($data, $count));
}

private static function getBody(array $data, int $count): array
{
$records = array_fill(0, $count, [
'id' => 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);
}
}

0 comments on commit 50f2ec6

Please sign in to comment.