Skip to content

Commit

Permalink
Merge branch 'main' into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Feb 9, 2022
2 parents 0da1248 + 2434f20 commit d91311d
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 27 deletions.
17 changes: 16 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,20 @@
'api_secret',
'secret',
]
]
],

/*
* Configuration for server-driven pagination
*/
'pagination' => [
/**
* The maximum page size this service will return, null for no limit
*/
'max' => null,

/**
* The default page size to use if the client does not request one, null for no default
*/
'default' => 200,
],
];
33 changes: 17 additions & 16 deletions src/EntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,22 +203,12 @@ public function emitJson(Transaction $transaction): void

$transaction = $this->transaction ?: $transaction;

// Validate $orderby
$orderby = $transaction->getOrderBy();
$orderby->getSortOrders();

$top = $transaction->getTop();

$maxPageSize = $transaction->getPreferenceValue(Constants::maxPageSize);
if (!$top->hasValue() && $maxPageSize) {
$top->setValue($maxPageSize);
}

/** @var Generator $results */
$results = $this->query();

$transaction->outputJsonArrayStart();

$top = $this->getTop();
$limit = $top->getValue();

while ($results->valid()) {
Expand Down Expand Up @@ -257,11 +247,22 @@ public function get(Transaction $transaction, ?ContextInterface $context = null)
Gate::query($this, $transaction)->ensure();

$top = $this->getTop();
$maxPageSize = $transaction->getPreferenceValue(Constants::maxPageSize);

if (!$top->hasValue() && $maxPageSize) {
$transaction->preferenceApplied(Constants::maxPageSize, $maxPageSize);
$top->setValue($maxPageSize);
$defaultPageSize = config('lodata.pagination.default');
$maxPageSize = config('lodata.pagination.max');
$preferPageSize = $transaction->getPreferenceValue(Constants::maxPageSize);
$targetPageSize = $top->getValue() ?: $preferPageSize ?: $defaultPageSize;

if ($maxPageSize && (!$targetPageSize || $targetPageSize > $maxPageSize)) {
$targetPageSize = $maxPageSize;
}

if ($preferPageSize && $targetPageSize === $preferPageSize) {
$transaction->preferenceApplied(Constants::maxPageSize, (string) $targetPageSize);
}

if ($targetPageSize !== $top->getValue()) {
$top->setValue((string) $targetPageSize);
}

$this->assertValidOrderBy();
Expand Down Expand Up @@ -740,7 +741,7 @@ public function addTrailingMetadata(Transaction $transaction, MetadataContainer
$top = $transaction->getTop();
$paginationParams = [];

if ($top->hasValue()) {
if ($top->hasValue() && ($count === null || $top->getValue() < $count)) {
switch (true) {
case $this instanceof TokenPaginationInterface:
$skipToken = $transaction->getSkipToken();
Expand Down
5 changes: 5 additions & 0 deletions tests/Helpers/UseODataAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ protected function assertConflict(Request $request): TestResponse
{
return $this->assertODataError($request, Response::HTTP_CONFLICT);
}

protected function assertResultCount(TestResponse $response, int $count)
{
$this->assertEquals($count, count(json_decode($response->streamedContent())->value));
}
}
1 change: 0 additions & 1 deletion tests/Parser/LoopbackEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Flat3\Lodata\Expression\Node\Literal\Date;
use Flat3\Lodata\Expression\Node\Literal\DateTimeOffset;
use Flat3\Lodata\Expression\Node\Literal\Duration;
use Flat3\Lodata\Expression\Node\Literal\Guid;
use Flat3\Lodata\Expression\Node\Literal\String_;
use Flat3\Lodata\Expression\Node\Literal\TimeOfDay;
use Flat3\Lodata\Expression\Node\Operator\Comparison\And_;
Expand Down
1 change: 0 additions & 1 deletion tests/Protocol/AuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public function setUp(): void
{
parent::setUp();
config(['lodata.authorization' => true]);
config(['lodata.readonly' => false]);
}

public function test_no_authorization()
Expand Down
1 change: 0 additions & 1 deletion tests/Protocol/BatchJSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Flat3\Lodata\Tests\Protocol;

use Flat3\Lodata\Drivers\SQLEntitySet;
use Flat3\Lodata\Facades\Lodata;
use Flat3\Lodata\Operation;
use Flat3\Lodata\Tests\Drivers\WithSQLDriver;
Expand Down
1 change: 0 additions & 1 deletion tests/Protocol/BatchMultipartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Flat3\Lodata\Tests\Protocol;

use Flat3\Lodata\Drivers\SQLEntitySet;
use Flat3\Lodata\Facades\Lodata;
use Flat3\Lodata\Operation;
use Flat3\Lodata\Tests\Drivers\WithSQLDriver;
Expand Down
145 changes: 145 additions & 0 deletions tests/Protocol/MaxPageSizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,149 @@ public function test_uses_odata_maxpagesize_preference()
->header('Prefer', 'odata.maxpagesize=1')
);
}

public function test_unlimited()
{
config([
'lodata.pagination.default' => null,
'lodata.pagination.max' => null
]);

$response = $this->req(
(new Request)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 5);
}

public function test_unlimited_uses_top()
{
config([
'lodata.pagination.default' => null,
'lodata.pagination.max' => null
]);

$response = $this->req(
(new Request)
->top(2)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 2);
}

public function test_unlimited_uses_maxpagesize()
{
config([
'lodata.pagination.default' => null,
'lodata.pagination.max' => null
]);

$response = $this->req(
(new Request)
->preference('maxpagesize', 2)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 2);
}

public function test_limits_to_default_if_unspecified()
{
config(['lodata.pagination.default' => 1]);

$response = $this->req(
(new Request)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 1);
}

public function test_limits_to_max_if_unspecified()
{
config(['lodata.pagination.max' => 1]);

$response = $this->req(
(new Request)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 1);
}

public function test_preference_overrides_default()
{
config(['lodata.pagination.default' => 1]);

$response = $this->req(
(new Request)
->preference('maxpagesize', 2)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 2);
}

public function test_preference_does_not_override_max()
{
config(['lodata.pagination.max' => 1]);

$response = $this->req(
(new Request)
->preference('maxpagesize', 2)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 1);
}

public function test_top_overrides_default()
{
config(['lodata.pagination.default' => 1]);

$response = $this->req(
(new Request)
->top(2)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 2);
}

public function test_top_does_not_override_max()
{
config(['lodata.pagination.max' => 1]);

$response = $this->req(
(new Request)
->top(2)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 1);
}

public function test_top_overrides_maxpagesize()
{
$response = $this->req(
(new Request)
->top(1)
->preference('maxpagesize', 2)
->path($this->entitySetPath)
);

$this->assertResponseHeaderSnapshot($response);
$this->assertResultCount($response, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
preference-applied: [maxpagesize=2]
vary: [prefer]
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
preference-applied: [maxpagesize=2]
vary: [prefer]
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
headers:
cache-control: ['no-cache, private']
content-type: [application/json]
odata-version: ['4.01']
status: 200
15 changes: 9 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Faker\Factory;
use Faker\Generator as FakerGenerator;
use Flat3\Lodata\DeclaredProperty;
use Flat3\Lodata\Drivers\SQLEntitySet;
use Flat3\Lodata\DynamicProperty;
use Flat3\Lodata\EntitySet;
use Flat3\Lodata\EntityType;
Expand Down Expand Up @@ -80,11 +79,15 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase

public function getEnvironmentSetUp($app)
{
config(['database.redis.client' => 'mock']);
config(['filesystems.disks.testing' => ['driver' => 'vfs']]);
config(['lodata.readonly' => false]);
config(['lodata.disk' => 'testing']);
config(['lodata.streaming' => false]);
config([
'database.redis.client' => 'mock',
'filesystems.disks.testing' => ['driver' => 'vfs'],
'lodata.readonly' => false,
'lodata.disk' => 'testing',
'lodata.streaming' => false,
'lodata.pagination.max' => null,
'lodata.pagination.default' => 200,
]);

$app->register(RedisMockServiceProvider::class);

Expand Down

0 comments on commit d91311d

Please sign in to comment.