diff options
Diffstat (limited to 't')
-rw-r--r-- | t/Mock/GoogleGeocoder.pm | 36 | ||||
-rw-r--r-- | t/geocode/google.t | 22 |
2 files changed, 58 insertions, 0 deletions
diff --git a/t/Mock/GoogleGeocoder.pm b/t/Mock/GoogleGeocoder.pm new file mode 100644 index 000000000..fd741c00e --- /dev/null +++ b/t/Mock/GoogleGeocoder.pm @@ -0,0 +1,36 @@ +package t::Mock::GoogleGeocoder; + +use JSON::MaybeXS; +use Web::Simple; +use LWP::Protocol::PSGI; + +has json => ( + is => 'lazy', + default => sub { + JSON->new->utf8->pretty->allow_blessed->convert_blessed; + }, +); + +sub dispatch_request { + my $self = shift; + + sub (GET + /maps/api/geocode/json + ?*) { + my ($self, $args) = @_; + my $response = {}; + if ($args->{address} =~ /result/) { + $response->{status} = 'OK'; + push @{$response->{results}}, { formatted_address => 'High Street, Old Town, City of Edinburgh, Scotland', geometry => { location => { lng => -3.1858425, lat => 55.9504009 } } }; + } + if ($args->{address} eq 'two results') { + push @{$response->{results}}, { geometry => { location => { lat => "55.8596449", "lng" => "-4.240377" } }, formatted_address => "High Street, Collegelands, Merchant City, Glasgow, Scotland" }; + } + my $json = mySociety::Locale::in_gb_locale { + $self->json->encode($response); + }; + return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]; + }, +} + +LWP::Protocol::PSGI->register(t::Mock::GoogleGeocoder->to_psgi_app, host => 'maps.googleapis.com'); + +__PACKAGE__->run_if_script; diff --git a/t/geocode/google.t b/t/geocode/google.t new file mode 100644 index 000000000..ee3c15ea8 --- /dev/null +++ b/t/geocode/google.t @@ -0,0 +1,22 @@ +package FixMyStreet::Cobrand::Tester; +use parent 'FixMyStreet::Cobrand::Default'; +sub country { 'GB' } +sub disambiguate_location { { components => 'country:IE' } } + +package main; + +use FixMyStreet::Test; +use FixMyStreet::Geocode::Google; +use Catalyst::Test 'FixMyStreet::App'; +use t::Mock::GoogleGeocoder; + +my $c = ctx_request('/'); +my $r = FixMyStreet::Geocode::Google::string("one result", $c); +ok $r->{latitude}; +ok $r->{longitude}; + +$c->stash->{cobrand} = FixMyStreet::Cobrand::Tester->new; +$r = FixMyStreet::Geocode::Google::string("two results", $c); +is scalar @{$r->{error}}, 2; + +done_testing; |