Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ This will prevent any method name conflicts with core, your custom or other trai
'lat' => 53.551085,
'lng' => 9.993682,
]);

$hamburg = ['lat' => 53.551085, 'lng' => 9.993682];
$berlin = ['lat' => 52.520008, 'lng' => 13.404954];

\Astrotomic\PhpunitAssertions\GeographicAssertions::assertDistanceGreaterThanOrEqual(255_000, $hamburg, $berlin);
\Astrotomic\PhpunitAssertions\GeographicAssertions::assertDistanceLessThanOrEqual(256_000, $hamburg, $berlin);
```

### HashID
Expand Down
38 changes: 38 additions & 0 deletions src/GeographicAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

final class GeographicAssertions
{
private const EARTH_RADIUS_METERS = 6_371_000;

public static function assertLatitude($actual): void
{
PHPUnit::assertIsNumeric($actual);
Expand All @@ -28,4 +30,40 @@ public static function assertCoordinates($actual, string $lat = 'lat', string $l
PHPUnit::assertArrayHasKey($lng, $actual);
self::assertLongitude($actual[$lng]);
}

public static function assertDistanceGreaterThanOrEqual(
int $distance,
$from,
$to,
string $lat = 'lat',
string $lng = 'lng'
): void {
PHPUnit::assertGreaterThanOrEqual($distance, self::distance($from, $to, $lat, $lng));
}

public static function assertDistanceLessThanOrEqual(
int $distance,
$from,
$to,
string $lat = 'lat',
string $lng = 'lng'
): void {
PHPUnit::assertLessThanOrEqual($distance, self::distance($from, $to, $lat, $lng));
}

private static function distance($from, $to, string $lat, string $lng): float
{
self::assertCoordinates($from, $lat, $lng);
self::assertCoordinates($to, $lat, $lng);

$fromLatitude = deg2rad((float) $from[$lat]);
$toLatitude = deg2rad((float) $to[$lat]);
$latitudeDelta = $toLatitude - $fromLatitude;
$longitudeDelta = deg2rad((float) $to[$lng] - (float) $from[$lng]);

$a = sin($latitudeDelta / 2) ** 2
+ cos($fromLatitude) * cos($toLatitude) * sin($longitudeDelta / 2) ** 2;

return self::EARTH_RADIUS_METERS * 2 * asin(sqrt(min(1.0, $a)));
}
}
42 changes: 42 additions & 0 deletions tests/GeographicAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,46 @@ public static function it_can_validate_array_of_coordinates_with_custom_keys():

GeographicAssertions::assertCoordinates($coords, $lat, $lng);
}

/** @test */
public static function it_can_validate_distance_greater_than_or_equal(): void
{
$hamburg = ['lat' => 53.551085, 'lng' => 9.993682];
$berlin = ['lat' => 52.520008, 'lng' => 13.404954];

GeographicAssertions::assertDistanceGreaterThanOrEqual(255_000, $hamburg, $berlin);
GeographicAssertions::assertDistanceGreaterThanOrEqual(0, $hamburg, $hamburg);
}

/** @test */
public static function it_can_validate_distance_less_than_or_equal(): void
{
$hamburg = ['lat' => 53.551085, 'lng' => 9.993682];
$berlin = ['lat' => 52.520008, 'lng' => 13.404954];

GeographicAssertions::assertDistanceLessThanOrEqual(256_000, $hamburg, $berlin);
GeographicAssertions::assertDistanceLessThanOrEqual(0, $hamburg, $hamburg);
}

/** @test */
public static function it_can_validate_distance_with_custom_keys(): void
{
$hamburg = ['latitude' => 53.551085, 'longitude' => 9.993682];
$berlin = ['latitude' => 52.520008, 'longitude' => 13.404954];

GeographicAssertions::assertDistanceGreaterThanOrEqual(
255_000,
$hamburg,
$berlin,
'latitude',
'longitude'
);
GeographicAssertions::assertDistanceLessThanOrEqual(
256_000,
$hamburg,
$berlin,
'latitude',
'longitude'
);
}
}
Loading