Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Implement sort by created and modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert Ramakers committed May 2, 2019
1 parent 2e29f13 commit 65f8171
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Offer/ElasticSearchOfferQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,22 @@ public function withSortByAvailableTo(SortOrder $sortOrder)
return $this->withFieldSort('availableTo', $sortOrder->toNative());
}

/**
* @inheritdoc
*/
public function withSortByCreated(SortOrder $sortOrder)
{
return $this->withFieldSort('created', $sortOrder->toNative());
}

/**
* @inheritdoc
*/
public function withSortByModified(SortOrder $sortOrder)
{
return $this->withFieldSort('modified', $sortOrder->toNative());
}

/**
* @inheritdoc
*
Expand Down
62 changes: 62 additions & 0 deletions tests/Offer/ElasticSearchOfferQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,68 @@ public function it_should_build_a_query_with_multiple_sorts()
$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @test
*/
public function it_should_build_a_query_with_sort_by_created()
{
/* @var ElasticSearchOfferQueryBuilder $builder */
$builder = (new ElasticSearchOfferQueryBuilder())
->withStart(new Natural(30))
->withLimit(new Natural(10))
->withSortByCreated(SortOrder::ASC());

$expectedQueryArray = [
'from' => 30,
'size' => 10,
'query' => [
'match_all' => (object) [],
],
'sort' => [
[
'created' => [
'order' => 'asc',
],
],
],
];

$actualQueryArray = $builder->build()->toArray();

$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @test
*/
public function it_should_build_a_query_with_sort_by_modified()
{
/* @var ElasticSearchOfferQueryBuilder $builder */
$builder = (new ElasticSearchOfferQueryBuilder())
->withStart(new Natural(30))
->withLimit(new Natural(10))
->withSortByModified(SortOrder::ASC());

$expectedQueryArray = [
'from' => 30,
'size' => 10,
'query' => [
'match_all' => (object) [],
],
'sort' => [
[
'modified' => [
'order' => 'asc',
],
],
],
];

$actualQueryArray = $builder->build()->toArray();

$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @return FacetName
*/
Expand Down

0 comments on commit 65f8171

Please sign in to comment.