astrotomic/phpunit-assertions
Adds expressive, reusable PHPUnit assertions to PHP and Laravel test suites, covering arrays, strings, objects, and common framework values. Drop-in helpers make tests clearer and reduce custom assertion boilerplate without changing your existing setup.
Install the package as a dev dependency:
composer require --dev astrotomic/phpunit-assertions
The package is framework-agnostic and is consumed as static assertion helpers inside your existing PHPUnit/Pest tests. You do not need to register a service provider — just import the assertion class you need:
use Astrotomic\PhpunitAssertions\GeographicAssertions;
For a first use case, start with the 0.15.0 addition: geographic distance assertions. They are useful anywhere you store coordinates (store locators, delivery zones, check-in features, geo search results) and want to assert that two points are the expected distance apart without hard-coding a full geo calculation in every test. The new logic lives in src/GeographicAssertions.php — read that file first to confirm the exact parameter order and units for your version.
GeographicAssertions::assertDistance(
expected: 1000.0, // metres
fromLatitude: 52.5200,
fromLongitude: 13.4050,
toLatitude: 52.5290,
toLongitude: 13.4100,
delta: 50.0 // tolerance in metres
);
Assert distances, not raw maths. Instead of recalculating Haversine inside a test (and duplicating the production bug), express the expectation declaratively:
public function test_order_is_deliverable_from_warehouse(): void
{
$warehouse = Warehouse::factory()->create(['lat' => 52.5200, 'lng' => 13.4050]);
$order = Order::factory()->create(['lat' => 52.5290, 'lng' => 13.4100]);
GeographicAssertions::assertDistance(
expected: 1000.0,
fromLatitude: $warehouse->lat,
fromLongitude: $warehouse->lng,
toLatitude: $order->lat,
toLongitude: $order->lng,
delta: 50.0
);
}
Use a delta for every real-world assertion. Coordinates coming from APIs, geocoders, or factories are rarely exact. A tolerance of tens of metres keeps tests stable while still catching genuine regressions (e.g. swapped latitude/longitude).
Pair with database/query assertions. Use the distance assertion to validate a specific pair of records, and use Laravel's own assertDatabaseHas/assertCount to prove that a radius query returned the right set of rows. The package asserts values; it does not replace spatial SQL.
Wrap in your own test helper. If several tests assert geo distances, add a small helper on your base TestCase so the coordinate argument order is enforced in one place:
protected function assertGeoDistance(float $expected, array $from, array $to, float $delta = 50.0): void
{
GeographicAssertions::assertDistance(
expected: $expected,
fromLatitude: $from[0],
fromLongitude: $from[1],
toLatitude: $to[0],
toLongitude: $to[1],
delta: $delta
);
}
Integrate with factories and fixtures. Add named states such as ->atCoordinates(52.52, 13.405) to your model factories so geo tests read consistently and you avoid scattering literal floats.
$delta tolerance; do not mix kilometres into expected without converting. Confirm units against the source before upgrading existing tests."astrotomic/phpunit-assertions": "^0.15") and read the changelog on each minor bump. Static helper signatures can change between minors.dump() on the models when investigating.How can I help you explore Laravel packages today?