Skip to content

Commit

Permalink
Merge pull request #163 from 5pm-HDH/feat/160/pagination-page-size
Browse files Browse the repository at this point in the history
feat(pagination): set page-size option #160
  • Loading branch information
DumbergerL authored Jun 19, 2023
2 parents f723807 + 511715c commit 4898796
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Add SongStatistic ([PR140](https://github.com/5pm-HDH/churchtools-api/pull/140))
- Add multi-factor authentication support ([PR146](https://github.com/5pm-HDH/churchtools-api/pull/146))
- Group-member-fields and DBFields-API ([PR147](https://github.com/5pm-HDH/churchtools-api/issues/147))
- Pagination Page-Size Option ([PR163](https://github.com/5pm-HDH/churchtools-api/pull/163))

### Changed
- Authenticate CTClient with session cookie instead of api-key ([PR142](https://github.com/5pm-HDH/churchtools-api/pull/142))
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ From now on all features of the ChurchTools-API are available.

### Requests and Models

The whole ChurchTools-API client is build on top of the Requests and Models. [Requests](/docs/out/Requests.md) provide
an interface to specify your api call by adding filtering, pagination and sorting. [Models](/docs/out/Models.md)
represent the data, that the Requests retrieve. More informations can be found in the documentation.
The whole ChurchTools-API client is build on top of the Requests and Models. Requests provide an interface to specify your api call by adding filtering, pagination and sorting. Models represent the data, that the Requests retrieve. More informations can be found in the documentation:

* [Requests](/docs/out/Requests.md)
* [Models](/docs/out/Models.md)

All APIs with examples:

Expand Down
13 changes: 12 additions & 1 deletion docs/out/CTConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,18 @@ $response = $client->get(

```

## 4. CSRF-Token
## 4. Pagination

Set Page-Size of Pagination-Requests.

```php
use CTApi\CTConfig;

CTConfig::setPaginationPageSize(400);

```

## 5. CSRF-Token

```php
use CTApi\Requests\CSRFTokenRequest;
Expand Down
9 changes: 9 additions & 0 deletions docs/out/Requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ Iterating over all records is quite easy.

```

If you want to set the Pagination Page-Size for all Requests you can use the CTConfig.

```php
use CTApi\CTConfig;

CTConfig::setPaginationPageSize(400);

```

**Get single record**

The `find`-method returns the Model. If there is no record with the given id, it will return null. The `findOrFail`
Expand Down
12 changes: 11 additions & 1 deletion docs/src/ressources/CTConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ $response = $client->get(
);
```

## 4. CSRF-Token
## 4. Pagination

Set Page-Size of Pagination-Requests.

```php
use CTApi\CTConfig;

CTConfig::setPaginationPageSize(400);
```

## 5. CSRF-Token

{{ \Tests\Unit\Docs\CSRFTokenRequestTest.testGetCSRFToken }}
8 changes: 8 additions & 0 deletions docs/src/ressources/Requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ Iterating over all records is quite easy.

{{ \Tests\Unit\Docs\PaginationTest.testIteratePages }}

If you want to set the Pagination Page-Size for all Requests you can use the CTConfig.

```php
use CTApi\CTConfig;

CTConfig::setPaginationPageSize(400);
```

**Get single record**

The `find`-method returns the Model. If there is no record with the given id, it will return null. The `findOrFail`
Expand Down
14 changes: 14 additions & 0 deletions src/CTConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class CTConfig
*/
private array $requestOptions;

private ?int $paginationPageSize = null;

private function __construct()
{
$this->cookieJar = new CookieJar();
Expand Down Expand Up @@ -143,6 +145,18 @@ public static function getApiKey(): ?string
return self::getRequestOption(self::PATH_LOGIN_TOKEN);
}

public static function setPaginationPageSize(int $pageSize): void
{
self::getConfig()->paginationPageSize = $pageSize;
}

public static function getPaginationPageSize(): ?int
{
$config = self::getConfig();
$size = $config->paginationPageSize;
return $size;
}

public static function validateConfig(): void
{
$apiUrl = self::getRequestOption('base_uri');
Expand Down
9 changes: 9 additions & 0 deletions src/Requests/Traits/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CTApi\Requests\Traits;

use CTApi\CTClient;
use CTApi\CTConfig;
use CTApi\Utils\CTResponseUtil;

trait Pagination
Expand All @@ -26,6 +27,7 @@ protected function collectDataFromPages(string $url, array $options = []): array
}

//Collect Data from First Page
$this->addPaginationPageSizeIfNotExists($options);
$response = $client->get($url, $options);

$metaInformation = CTResponseUtil::metaAsArray($response);
Expand All @@ -47,4 +49,11 @@ protected function collectDataFromPages(string $url, array $options = []): array

return $collectedData;
}

private function addPaginationPageSizeIfNotExists(&$options)
{
if (CTConfig::getPaginationPageSize() != null && !array_key_exists("limit", $options["query"])) {
$options["query"]["limit"] = CTConfig::getPaginationPageSize();
}
}
}
51 changes: 51 additions & 0 deletions tests/unit/Requests/PaginationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php


namespace Tests\Unit\Requests;


use CTApi\CTClient;
use CTApi\CTConfig;
use CTApi\Requests\EventRequest;
use CTApi\Requests\PersonRequest;
use Tests\Unit\HttpMock\CTClientMock;
use Tests\Unit\TestCaseHttpMocked;

class PaginationTest extends TestCaseHttpMocked
{

public function testPagination()
{
CTConfig::setPaginationPageSize(6);
EventRequest::all();
$this->assertAllRequestContainLimit(6);
}

public function testManualPagination()
{
EventRequest::where("limit", 9)->get();
$this->assertAllRequestContainLimit(9);
}

public function testOverrideManualPagination()
{
CTConfig::setPaginationPageSize(12);
EventRequest::where("limit", 9)->get();
$this->assertAllRequestContainLimit(9);
}

private function assertAllRequestContainLimit(?int $pageSize)
{
foreach ($this->getClientMock()->getAllRequestCalls() as $requestCall) {
$this->assertRequestContainsLimit($requestCall, $pageSize);
}
}

private function assertRequestContainsLimit(array $request, ?int $pageSize)
{
$this->assertArrayHasKey("options", $request);
$this->assertArrayHasKey("query", $request["options"]);
$this->assertArrayHasKey("limit", $request["options"]["query"]);
$this->assertEquals($pageSize, $request["options"]["query"]["limit"]);
}
}
11 changes: 9 additions & 2 deletions tests/unit/TestCaseHttpMocked.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use CTApi\CTClient;
use CTApi\CTConfig;
use CTApi\Utils\CTUtil;
use PHPUnit\Framework\TestCase;
use Tests\Unit\HttpMock\CTClientMock;

class TestCaseHttpMocked extends TestCase
{
private $ctClientMock;
private CTClientMock $ctClientMock;

protected function setUp(): void
{
Expand All @@ -22,11 +23,17 @@ protected function setUp(): void
CTClient::setClient($this->ctClientMock);
}

protected function setClientMock(CTClientMock $clientMock){
protected function setClientMock(CTClientMock $clientMock)
{
$this->ctClientMock = $clientMock;
CTClient::setClient($this->ctClientMock);
}

protected function getClientMock(): CTClientMock
{
return $this->ctClientMock;
}

protected function assertRequestCallExists(string $method, $uri = null): array
{
return $this->ctClientMock->assertRequestCallExists($method, $uri);
Expand Down

0 comments on commit 4898796

Please sign in to comment.