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

Commit

Permalink
Merge e9a8af0 into 4240e2b
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Apr 24, 2017
2 parents 4240e2b + e9a8af0 commit 93e49f4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"cultuurnet/hydra": "~0.1",
"cultuurnet/udb3-search": "~0.1",
"cultuurnet/udb3-search": "dev-feature/III-2042 as 0.1",
"cultuurnet/geocoding": "~0.1",
"cultuurnet/valueobjects": "~3.0",
"symfony/http-foundation": "~2.7.2"
Expand Down
17 changes: 17 additions & 0 deletions src/OfferSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use ValueObjects\Geography\Country;
use ValueObjects\Geography\CountryCode;
use ValueObjects\Number\Natural;
use ValueObjects\StringLiteral\StringLiteral;

Expand Down Expand Up @@ -188,6 +190,21 @@ public function search(Request $request)
);
}

if (!empty($request->query->get('addressCountry'))) {
$requestedCountry = $request->query->get('addressCountry');
$upperCasedCountry = strtoupper((string) $requestedCountry);

try {
$countryCode = CountryCode::fromNative($upperCasedCountry);
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException("Unknown country code '{$requestedCountry}'.");
}

$parameters = $parameters->withAddressCountry(
new Country($countryCode)
);
}

// Do strict comparison to make sure 0 gets included.
if ($request->query->get('minAge', false) !== false) {
$parameters = $parameters->withMinimumAge(
Expand Down
35 changes: 35 additions & 0 deletions tests/OfferSearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use CultuurNet\UDB3\Search\Region\RegionId;
use CultuurNet\UDB3\ValueObject\MultilingualString;
use Symfony\Component\HttpFoundation\Request;
use ValueObjects\Geography\Country;
use ValueObjects\Geography\CountryCode;
use ValueObjects\Number\Natural;
use ValueObjects\StringLiteral\StringLiteral;

Expand Down Expand Up @@ -106,6 +108,7 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
'coordinates' => '-40,70',
'distance' => '30km',
'postalCode' => 3000,
'addressCountry' => 'BE',
'minAge' => 3,
'maxAge' => 7,
'price' => 1.55,
Expand Down Expand Up @@ -158,6 +161,7 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
)
)
->withPostalCode(new PostalCode("3000"))
->withAddressCountry(new Country(CountryCode::fromNative('BE')))
->withMinimumAge(new Natural(3))
->withMaximumAge(new Natural(7))
->withPrice(Price::fromFloat(1.55))
Expand Down Expand Up @@ -506,4 +510,35 @@ public function it_throws_an_exception_when_an_unknown_facet_name_is_given()
$request = new Request(['facets' => ['regions', 'bla']]);
$this->controller->search($request);
}

/**
* @test
*/
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']);
$this->controller->search($request);
}

/**
* @test
*/
public function it_transforms_the_request_address_country_to_uppercase()
{
$request = new Request(['addressCountry' => 'nl']);

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

$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 93e49f4

Please sign in to comment.