generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from innobraingmbh/11-feature-request-get-esta…
…te-filters-api-call feat: get filters for address and estate
- Loading branch information
Showing
8 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_replace_recursive([ | ||
'status' => [ | ||
'code' => 200, | ||
'errorcode' => 0, | ||
'message' => 'OK', | ||
], | ||
'response' => [ | ||
'results' => [ | ||
[ | ||
'data' => [ | ||
'meta' => [ | ||
'cntabsolute' => $count, | ||
], | ||
'records' => $records, | ||
], | ||
], | ||
], | ||
], | ||
], $data); | ||
} | ||
} |