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

Commit

Permalink
III-2043: Add availableFrom and availableTo to offer search endpoint,…
Browse files Browse the repository at this point in the history
… with default values
  • Loading branch information
bertramakers committed Apr 26, 2017
1 parent 1fcc80b commit 35416e5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 16 deletions.
41 changes: 41 additions & 0 deletions src/OfferSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ public function search(Request $request)
);
}

$availableFrom = $this->getAvailabilityFromQuery($request, 'availableFrom');
if ($availableFrom instanceof \DateTimeImmutable) {
$parameters = $parameters->withAvailableFrom($availableFrom);
}

$availableTo = $this->getAvailabilityFromQuery($request, 'availableTo');
if ($availableTo instanceof \DateTimeImmutable) {
$parameters = $parameters->withAvailableTo($availableTo);
}

if (!empty($request->query->get('workflowStatus'))) {
$parameters = $parameters->withWorkflowStatus(
new WorkflowStatus($request->query->get('workflowStatus'))
Expand Down Expand Up @@ -309,6 +319,37 @@ public function search(Request $request)
->setTtl(60 * 5);
}

/**
* @param Request $request
* @param string $queryParameter
* @return \DateTimeImmutable|null
*/
private function getAvailabilityFromQuery(Request $request, $queryParameter)
{
$availability = $request->query->get($queryParameter, false);

if (!$availability) {
// Default value is the time the request was made.
return \DateTimeImmutable::createFromFormat('U', $request->server->get('REQUEST_TIME'));
}

if ($availability === '*') {
// A wildcard means the consumer wants to disable the filter
// completely instead of changing the default value.
return null;
}

$availabilityAsDateTime = \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $availability);

if (!$availabilityAsDateTime) {
throw new \InvalidArgumentException(
"{$queryParameter} should be an ISO-8601 datetime, for example 2017-04-26T12:20:05+01:00"
);
}

return $availabilityAsDateTime;
}

/**
* @param Request $request
* @param string $queryParameter
Expand Down
94 changes: 78 additions & 16 deletions tests/OfferSearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,18 @@ public function setUp()
*/
public function it_returns_a_paged_collection_of_search_results_based_on_request_query_parameters()
{
$request = new Request(
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
[
'start' => 30,
'limit' => 10,
'q' => 'dag van de fiets',
'id' => '42926044-09f4-4bd5-bc35-427b2fc1a525',
'locationId' => '652ab95e-fdff-41ce-8894-1b29dce0d230',
'organizerId' => '392168d7-57c9-4488-8e2e-d492c843054b',
'availableFrom' => '2017-04-26T00:00:00+01:00',
'availableTo' => '2017-04-28T15:30:23+01:00',
'workflowStatus' => 'DRAFT',
'regionId' => 'gem-leuven',
'coordinates' => '-40,70',
Expand Down Expand Up @@ -143,6 +147,12 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
->withOrganizerCdbid(
new Cdbid('392168d7-57c9-4488-8e2e-d492c843054b')
)
->withAvailableFrom(
\DateTimeImmutable::createFromFormat(\DateTime::ATOM, '2017-04-26T00:00:00+01:00')
)
->withAvailableTo(
\DateTimeImmutable::createFromFormat(\DateTime::ATOM, '2017-04-28T15:30:23+01:00')
)
->withWorkflowStatus(
new WorkflowStatus('DRAFT')
)
Expand Down Expand Up @@ -285,10 +295,14 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
*/
public function it_uses_the_default_limit_of_30_if_a_limit_of_0_is_given()
{
$request = new Request(
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
[
'start' => 0,
'limit' => 0,
'availableFrom' => '*',
'availableTo' => '*',
]
);

Expand All @@ -306,15 +320,43 @@ public function it_uses_the_default_limit_of_30_if_a_limit_of_0_is_given()
$this->controller->search($request);
}

/**
* @test
*/
public function it_sets_a_default_available_from_and_available_to_if_none_are_given()
{
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
[],
[],
[],
['REQUEST_TIME' => 1493195661]
);

$expectedSearchParameters = (new OfferSearchParameters())
->withAvailableFrom(\DateTimeImmutable::createFromFormat(\DateTime::ATOM, '2017-04-26T08:34:21+00:00'))
->withAvailableTo(\DateTimeImmutable::createFromFormat(\DateTime::ATOM, '2017-04-26T08:34:21+00:00'));

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

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

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

/**
* @test
*/
public function it_throws_an_exception_if_coordinates_is_given_without_distance()
{
$request = new Request(
[
'coordinates' => '-40,70',
]
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
['coordinates' => '-40,70']
);

$this->expectException(\InvalidArgumentException::class);
Expand All @@ -328,10 +370,10 @@ public function it_throws_an_exception_if_coordinates_is_given_without_distance(
*/
public function it_throws_an_exception_if_distance_is_given_without_coordinates()
{
$request = new Request(
[
'distance' => '30km',
]
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
['distance' => '30km']
);

$this->expectException(\InvalidArgumentException::class);
Expand All @@ -345,10 +387,14 @@ public function it_throws_an_exception_if_distance_is_given_without_coordinates(
*/
public function it_works_with_a_min_age_of_zero_and_or_a_max_age_of_zero()
{
$request = new Request(
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
[
'start' => 0,
'limit' => 0,
'availableFrom' => '*',
'availableTo' => '*',
'minAge' => 0,
'maxAge' => 0,
]
Expand Down Expand Up @@ -393,10 +439,14 @@ public function it_converts_the_embed_parameter_to_a_correct_boolean_and_passes_
$pagedCollectionFactory
);

$request = new Request(
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
[
'start' => 0,
'limit' => 30,
'availableFrom' => '*',
'availableTo' => '*',
'embed' => $embedParameter,
]
);
Expand Down Expand Up @@ -463,10 +513,14 @@ public function embedParameterDataProvider()
*/
public function it_can_handle_a_single_string_value_for_parameters_that_are_normally_arrays()
{
$request = new Request(
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
[
'start' => 30,
'limit' => 10,
'availableFrom' => '*',
'availableTo' => '*',
'labels' => 'foo',
'organizerLabels' => 'bar',
'locationLabels' => 'baz',
Expand Down Expand Up @@ -507,7 +561,7 @@ public function it_throws_an_exception_when_an_unknown_facet_name_is_given()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Unknown facet name 'bla'.");
$request = new Request(['facets' => ['regions', 'bla']]);
$request = Request::create('http://search.uitdatabank.be/offers/', 'GET', ['facets' => ['regions', 'bla']]);
$this->controller->search($request);
}

Expand All @@ -518,7 +572,7 @@ public function it_throws_an_exception_when_an_unknown_address_country_is_given(
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Unknown country code 'foobar'.");
$request = new Request(['addressCountry' => 'foobar']);
$request = Request::create('http://search.uitdatabank.be/offers/', 'GET', ['addressCountry' => 'foobar']);
$this->controller->search($request);
}

Expand All @@ -527,7 +581,15 @@ public function it_throws_an_exception_when_an_unknown_address_country_is_given(
*/
public function it_transforms_the_request_address_country_to_uppercase()
{
$request = new Request(['addressCountry' => 'nl']);
$request = Request::create(
'http://search.uitdatabank.be/offers/',
'GET',
[
'availableFrom' => '*',
'availableTo' => '*',
'addressCountry' => 'nl'
]
);

$expectedSearchParameters = (new OfferSearchParameters())
->withAddressCountry(new Country(CountryCode::fromNative('NL')));
Expand Down

0 comments on commit 35416e5

Please sign in to comment.