helmich/phpunit-json-assert
Adds concise JSON assertions to PHPUnit using JSONPath expressions and JSON Schema validation. Use the JsonAssertions trait to verify complex JSON/data structures with readable assert* helpers. Install via Composer and choose the branch matching your PHPUnit/PHP version.
HttpTests, XmlHttpRequest) and can enhance existing test suites without disrupting core functionality. Works alongside Laravel’s Illuminate\Support\Facades\Route and Illuminate\Testing\TestCase for end-to-end validation.assertJsonEquals(), assertJsonContains(), assertJsonFragment()) allows for granular test assertions, reducing boilerplate and improving maintainability. Can be extended via PHPUnit’s extensibility mechanisms (e.g., custom assertions).use statements and extending test cases. No database migrations, service providers, or route changes needed.php artisan test command and IDEs (e.g., PHPStorm’s PHPUnit integration). Can be combined with Laravel’s Http facade for API testing.assertJson() or supplement it? Are there gaps in Laravel’s native assertions (e.g., deep object traversal, custom validators)?Validator facade be integrated for reusable rules?spatie/laravel-test-factory) or general-purpose libraries (e.g., php-json-schema-validator).HttpTests in Laravel, where JSON responses from routes (e.g., Route::get('/api/data')) can be validated with methods like:
$response = $this->getJson('/api/data');
$response->assertJsonEquals(['key' => 'value']);
App\Services\JsonGenerator) or third-party API clients.assertJsonStructure(), assertJsonFragment()).composer.json:
composer require --dev martin-helmich/phpunit-json-assert
composer.json and update phpunit.xml if needed (e.g., for custom assertion paths).## JSON Assertion Patterns
- **Deep Validation**: `assertJsonEquals()` for exact matches.
- **Partial Validation**: `assertJsonContains()` for dynamic fields.
- **Arrays**: `assertJsonCount()` for array length.
Failed asserting that two JSON strings are equal.
--- Expected
+++ Actual
@@ -1,2 +1,2 @@
{
- "status": "success"
+ "status": "failed"
}
--parallel flag.@slow tags).assertJsonSimilar() for approximate matches.v1_api_tests.php).HttpTests + Pact).--filter to run JSON-specific tests.How can I help you explore Laravel packages today?