Skip to content

Stop GeocoderTest calling Google - #904

Merged
ngm merged 1 commit into
developfrom
fix-geocoder-test
Sep 3, 2026
Merged

Stop GeocoderTest calling Google#904
ngm merged 1 commit into
developfrom
fix-geocoder-test

Conversation

@edwh

@edwh edwh commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

tests/Unit/GeocoderTest.php built a Geocoder directly and geocoded a real address against the live Google Maps API. When that call fails — network, key, or quota — geocode() returns false and the test errors:

ErrorException : Trying to access array offset on value of type bool
  /var/www/tests/Unit/GeocoderTest.php:11

That is what failed CI on #900 (feat/ords-repairs-export), which has nothing to do with geocoding. PHPUnit passed on develop's later run, so it comes and goes with the API — a unit test depending on a third party being reachable.

Change

Move the HTTP call behind a protected fetch(), the same idea as the existing googleKey() seam ("We have this so that we can change the key in testing"), and have the test subclass it with a canned response. The assertions on parsed latitude, longitude and country code are unchanged, so what the test was actually checking — that a Google response is parsed correctly — is still checked.

Three paths that had no coverage are now tested: no results, a failed request (exactly the case that produced the error above), and the ForceGeocodeFailure short circuit.

GeocoderMock overrides geocode() outright, so tests resolving the geocoder from the container are unaffected — GroupEditGeocodeFallbackTest still passes.

Verification

phpunit --filter GeocoderTest            OK (4 tests, 6 assertions)
phpunit GroupEditGeocodeFallbackTest     OK (2 tests, 7 assertions)

testGeocode built a Geocoder directly and geocoded a real address against
the live Maps API.  When that call failed - network, key or quota - the
helper returned false and the test errored with "Trying to access array
offset on value of type bool", as it did on the #900 run.  Nothing was
wrong with the branch: a unit test simply depended on a third party being
reachable.

Move the HTTP call behind a protected fetch(), the same idea as the
existing googleKey() seam, so the parsing can be tested with a canned
response.  The assertions on the parsed latitude, longitude and country
code are unchanged.

Also covers the paths that had no test: no results, a failed request
(the one that produced the error above), and the ForceGeocodeFailure
short circuit.

GeocoderMock overrides geocode() outright, so tests resolving the
geocoder from the container are unaffected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented Sep 2, 2026

Copy link
Copy Markdown

@ngm ngm self-assigned this Sep 3, 2026
@ngm
ngm merged commit 2387095 into develop Sep 3, 2026
3 checks passed
edwh added a commit that referenced this pull request Sep 5, 2026
develop gained #904 (a fetch() seam so GeocoderTest could exercise the
parsing without calling Google) while this branch was replacing the whole
method with the geocoder package. Same method, incompatible approaches.

Taking this branch's: with the provider chain resolved from the
container there is nothing to seam - a test binds its own provider
instead, which is what the code comment here already promised. That also
drops googleKey(), and reverseGeocode(), which was defined and never
called anywhere in the codebase.

GeocoderTest is rewritten onto that mechanism: it builds real
Geocoder\Model\Address objects and binds a provider returning them.

Both container keys have to be replaced, not just one. geocoder-laravel
binds the string 'geocoder' through to ProviderAndDumperAggregator and
registers that as a singleton, so replacing only the string leaves the
real Mapbox chain reachable - and a test that passes then is quietly
talking to the network.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants