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

Commit

Permalink
III-1957: Add labels parameter to offer search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Mar 13, 2017
1 parent ff7269a commit 8253626
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/OfferSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CultuurNet\UDB3\Search\Http;

use CultuurNet\UDB3\Label\ValueObjects\LabelName;
use CultuurNet\UDB3\Search\Offer\OfferSearchParameters;
use CultuurNet\UDB3\Search\Offer\OfferSearchServiceInterface;
use CultuurNet\UDB3\Search\QueryStringFactoryInterface;
Expand Down Expand Up @@ -93,6 +94,19 @@ public function search(Request $request)
);
}

if (!empty($request->query->get('labels'))) {
$labels = (array) $request->query->get('labels');

$labelNames = array_map(
function ($label) {
return new LabelName($label);
},
$labels
);

$parameters = $parameters->withLabels(...$labelNames);
}

$resultSet = $this->searchService->search($parameters);

$pagedCollection = $this->pagedCollectionFactory->fromPagedResultSet(
Expand Down
34 changes: 34 additions & 0 deletions tests/OfferSearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CultuurNet\UDB3\Search\Http;

use CultuurNet\UDB3\Label\ValueObjects\LabelName;
use CultuurNet\UDB3\ReadModel\JsonDocument;
use CultuurNet\UDB3\Search\Offer\OfferSearchParameters;
use CultuurNet\UDB3\Search\Offer\OfferSearchServiceInterface;
Expand Down Expand Up @@ -66,6 +67,7 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
'limit' => 10,
'q' => 'dag van de fiets',
'regionId' => 'gem-leuven',
'labels' => ['foo', 'bar'],
]
);

Expand All @@ -78,6 +80,10 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
$this->regionIndexName,
$this->regionDocumentType
)
->withLabels(
new LabelName('foo'),
new LabelName('bar')
)
->withStart(new Natural(30))
->withLimit(new Natural(10));

Expand Down Expand Up @@ -139,4 +145,32 @@ public function it_uses_the_default_limit_of_30_if_a_limit_of_0_is_given()

$this->controller->search($request);
}

/**
* @test
*/
public function it_can_handle_a_single_string_value_as_query_parameter_for_labels()
{
$request = new Request(
[
'start' => 30,
'limit' => 10,
'labels' => 'foo',
]
);

$expectedSearchParameters = (new OfferSearchParameters())
->withStart(new Natural(30))
->withLimit(new Natural(10))
->withLabels(new LabelName('foo'));

$expectedResultSet = new PagedResultSet(new Natural(30), new Natural(0), []);

$this->searchService->expects($this->once())
->method('search')
->with($expectedSearchParameters)
->willReturn($expectedResultSet);

$this->controller->search($request);
}
}

0 comments on commit 8253626

Please sign in to comment.