@@ -17,10 +17,10 @@ class Test_Events extends TestCase {
1717 * Asserts that an HTTP response is valid and contains an event.
1818 */
1919 public function assertResponseHasEvent ( $ response ) {
20- $ body = json_decode ( $ response ->body );
21- $ event = $ body ->events [0 ];
20+ $ this ->assertSame ( 200 , $ response ->status_code , 'Non-200 response body: ' . substr ( $ response ->body , 0 , 200 ) );
2221
23- $ this ->assertSame ( 200 , $ response ->status_code );
22+ $ body = json_decode ( $ response ->body );
23+ $ this ->assertIsObject ( $ body , 'Response body was not valid JSON: ' . substr ( $ response ->body , 0 , 200 ) );
2424 $ this ->assertNull ( $ body ->error );
2525
2626 $ this ->assertIsObject ( $ body ->location );
@@ -29,6 +29,8 @@ public function assertResponseHasEvent( $response ) {
2929 ( isset ( $ body ->location ->latitude ) && is_numeric ( $ body ->location ->latitude ) )
3030 );
3131
32+ $ this ->assertNotEmpty ( $ body ->events , 'Response contained no events. ' );
33+ $ event = $ body ->events [0 ];
3234 $ this ->assertContains ( $ event ->type , array ( 'wordcamp ' , 'meetup ' ) );
3335 $ this ->assertIsString ( $ event ->title );
3436 $ this ->assertIsNumeric ( $ event ->start_unix_timestamp );
@@ -471,20 +473,31 @@ public function test_get_location( array $input, $expected ) : void {
471473 $ actual_result ['description ' ] = strtolower ( $ actual_result ['description ' ] );
472474 }
473475
474- /*
475- * Normalize coordinates to account for minor differences in the databases.
476- *
477- * Rounding to three decimal places means that we're still accurate within about 110 meters, which is
478- * good enough for our purposes.
479- *
480- * @link https://gis.stackexchange.com/a/8674/49125.
481- */
482- if ( isset ( $ actual_result ['latitude ' ], $ actual_result ['longitude ' ] ) ) {
483- $ actual_result ['latitude ' ] = number_format ( round ( $ actual_result ['latitude ' ], 3 ), 3 );
484- $ actual_result ['longitude ' ] = number_format ( round ( $ actual_result ['longitude ' ], 3 ), 3 );
476+ $ this ->assertSameWithLatLonDelta ( $ expected , $ actual_result );
477+ }
478+
479+ /**
480+ * Assert that two location arrays match, allowing small drift in latitude/longitude.
481+ *
482+ * Compares lat/lon with a tolerance to absorb routine drift in the geo databases, and
483+ * compares the remaining fields exactly. A delta of 0.01 degrees is roughly 1km at the
484+ * equator — tight enough to catch a wrong-city match, loose enough to survive geonames
485+ * updates.
486+ *
487+ * @link https://gis.stackexchange.com/a/8674/49125.
488+ */
489+ public function assertSameWithLatLonDelta ( $ expected , $ actual , float $ delta = 0.01 ) : void {
490+ if ( is_array ( $ expected ) && is_array ( $ actual )
491+ && isset ( $ expected ['latitude ' ], $ expected ['longitude ' ], $ actual ['latitude ' ], $ actual ['longitude ' ] )
492+ ) {
493+ $ this ->assertEqualsWithDelta ( (float ) $ expected ['latitude ' ], (float ) $ actual ['latitude ' ], $ delta , 'latitude drifted beyond tolerance ' );
494+ $ this ->assertEqualsWithDelta ( (float ) $ expected ['longitude ' ], (float ) $ actual ['longitude ' ], $ delta , 'longitude drifted beyond tolerance ' );
495+
496+ // Strip the coordinates so the remaining fields can be compared exactly.
497+ unset( $ expected ['latitude ' ], $ expected ['longitude ' ], $ actual ['latitude ' ], $ actual ['longitude ' ] );
485498 }
486499
487- $ this ->assertSame ( $ expected , $ actual_result );
500+ $ this ->assertSame ( $ expected , $ actual );
488501 }
489502
490503 public static function data_get_location () : array {
@@ -813,12 +826,12 @@ public static function data_get_location() : array {
813826
814827 'city-endonym-ideographic-asia3 ' => array (
815828 'input ' => array (
816- 'location_name ' => 'كراچى ' ,
829+ 'location_name ' => 'کراچی ' ,
817830 'locale ' => 'ur ' ,
818831 'timezone ' => 'Asia/Karachi ' ,
819832 ),
820833 'expected ' => array (
821- 'description ' => 'كراچى ' ,
834+ 'description ' => 'کراچی ' ,
822835 'latitude ' => '24.861 ' ,
823836 'longitude ' => '67.010 ' ,
824837 'country ' => 'PK ' ,
@@ -1464,7 +1477,8 @@ public static function data_build_response() : array {
14641477
14651478 'throttled ' => array (
14661479 'input ' => array (
1467- 'location ' => 'temp-request-throttled ' ,
1480+ 'location ' => 'temp-request-throttled ' ,
1481+ 'location_args ' => array (),
14681482 ),
14691483 'expected ' => array (
14701484 'location ' => array (),
@@ -1475,7 +1489,8 @@ public static function data_build_response() : array {
14751489
14761490 'no-location ' => array (
14771491 'input ' => array (
1478- 'location ' => array (),
1492+ 'location ' => array (),
1493+ 'location_args ' => array (),
14791494 ),
14801495 'expected ' => array (
14811496 'location ' => array (),
0 commit comments