Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Apply correct typing to $limit in BasicSearchContext #11535

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/ORM/Search/BasicSearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BasicSearchContext extends SearchContext
* the parameter name should have the dots replaced with double underscores,
* for example "Comments__Name" instead of the filter name "Comments.Name".
* @param array|bool|string $sort Field to sort on.
* @param array|null|string $limit
* @param int|array|null $limit
* @param Filterable&Sortable&Limitable $existingQuery
*/
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null): Filterable&Sortable&Limitable
Expand All @@ -45,12 +45,16 @@ public function getQuery($searchParams, $sort = false, $limit = false, $existing
throw new InvalidArgumentException('getQuery requires a pre-existing filterable/sortable/limitable list to be passed as $existingQuery.');
}

if ((count(func_get_args()) >= 3) && (!in_array(gettype($limit), ['array', 'NULL', 'string']))) {
if ((count(func_get_args()) >= 3) && (!in_array(gettype($limit), ['array', 'NULL', 'integer']))) {
Deprecation::notice(
'5.1.0',
'$limit should be type of array|string|null'
'$limit should be type of int|array|null'
);
$limit = null;
if (is_string($limit) && is_numeric($limit)) {
$limit = (int) $limit;
} else {
$limit = null;
}
}

$searchParams = $this->applySearchFilters($this->normaliseSearchParams($searchParams));
Expand All @@ -67,7 +71,14 @@ public function getQuery($searchParams, $sort = false, $limit = false, $existing
}

// Limit must be last so that ArrayList results don't have an applied limit before they can be filtered/sorted.
$result = $result->limit($limit);
if (is_array($limit)) {
$result = $result->limit(
isset($limit['limit']) ? $limit['limit'] : null,
isset($limit['start']) ? $limit['start'] : null
);
} else {
$result = $result->limit($limit);
}

return $result;
}
Expand Down
65 changes: 65 additions & 0 deletions tests/php/ORM/Search/BasicSearchContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,71 @@ public function testGetQuery(array $searchParams, array $expected)
$this->assertSame($expected, $results->column('Name'));
}

public function provideGetQueryLimit(): array
{
return [
'no limit' => [
'limit' => null,
'expected' => [
'James',
'John',
'Jane',
'Hemi',
'Sara',
'MatchNothing',
],
],
'limit int' => [
'limit' => 3,
'expected' => [
'James',
'John',
'Jane',
],
],
'paginated limit' => [
'limit' => [
'limit' => 2,
'start' => 3,
],
'expected' => [
'Hemi',
'Sara',
],
],
'limit numeric string' => [
'limit' => '4',
'expected' => [
'James',
'John',
'Jane',
'Hemi',
],
],
'limit invalid string' => [
'limit' => 'blah',
'expected' => [
'James',
'John',
'Jane',
'Hemi',
'Sara',
'MatchNothing',
],
],
];
}

/**
* @dataProvider provideGetQueryLimit
*/
public function testGetQueryLimit(mixed $limit, array $expected): void
{
$context = new BasicSearchContext(ArrayData::class);
$results = $context->getQuery([], limit: $limit, existingQuery: $this->getList());
$this->assertSame($expected, $results->column('Name'));
}

public function testGeneralSearch()
{
$list = $this->getList();
Expand Down
Loading