jsonpath-standard/jsonpath-compliance-test-suite
Compliance test suite for the JSONPath Standard. Provides a shared set of test cases and expected results to validate JSONPath implementations, track spec adherence, and compare behavior across languages and libraries.
Installation Add the package via Composer:
composer require --dev jsonpath-standard/jsonpath-compliance-test-suite
Ensure your project uses PHP 8.0+ (check composer.json constraints).
First Use Case: Validate JSONPath Implementation
Run the test suite against your JSONPath library (e.g., league/json-query, mtdowling/jsonpath, or a custom implementation):
./vendor/bin/jsonpath-compliance-test-suite
Quick Check for Laravel Projects If using a JSONPath library in Laravel (e.g., for API responses or Eloquent queries), test it with:
use JsonPath\JsonPath; // Example library
$json = json_decode('{"store": {"book": [{"title": "Foo"}, {"title": "Bar"}]}}');
$result = JsonPath::search($json, '$..book[?(@.title=="Foo")]');
Run the compliance suite to ensure $..book[?(@.title=="Foo")] behaves as expected.
Integration with Testing
phpunit.xml as a pre-test hook:
<phpunit>
<listeners>
<listener class="JsonPathComplianceTestSuite\Listener" file="vendor/jsonpath-standard/jsonpath-compliance-test-suite/src/Listener.php"/>
</listeners>
</phpunit>
- name: Run JSONPath Compliance Tests
run: ./vendor/bin/jsonpath-compliance-test-suite --format=json > compliance.json
Debugging JSONPath Queries
--verbose flag to see step-by-step execution:
./vendor/bin/jsonpath-compliance-test-suite --verbose
./vendor/bin/jsonpath-compliance-test-suite --filter="filter"
Custom JSONPath Libraries
use JsonPathComplianceTestSuite\JsonPathInterface;
class LaravelJsonPathAdapter implements JsonPathInterface {
public function search($json, $expression) {
return json_decode(json_encode(\JsonPath::search($json, $expression)));
}
}
composer.json autoload or via a service provider.Laravel-Specific Use Cases
routes/api.php or controllers comply:
$response = Http::get('https://api.example.com/data');
$filtered = jsonpath($response->json(), '$..active'); // Test with suite
Model::whereJsonContains() or custom accessors.False Positives in Wildcard Tests
$..* (deep search) as "non-compliant" if your library doesn’t handle circular references. Ignore these if your use case doesn’t require them.--ignore="circular" to skip irrelevant tests.PHP Type Mismatches
null vs. false). Libraries like league/json-query may return false for missing paths, while the draft expects null.return is_false($result) ? null : $result;
Performance Over Compliance
$..) are computationally expensive. Disable them if not needed:
./vendor/bin/jsonpath-compliance-test-suite --exclude="recursive-descent"
Laravel Caching Quirks
JsonPath::search() in cached views or jobs, ensure the compliance suite tests the same environment (e.g., same PHP version, extensions like json).--filter="expression" to narrow down issues:
./vendor/bin/jsonpath-compliance-test-suite --filter="$.store.book[0].title"
./vendor/bin/jsonpath-compliance-test-suite --format=json > my-output.json
./vendor/bin/jsonpath-compliance-test-suite --format=json --library=league > league-output.json
diff my-output.json league-output.json
./vendor/bin/jsonpath-compliance-test-suite --draft-version=02
Custom Test Cases
Add project-specific JSONPath queries to tests/JsonPathComplianceTest.php:
public function testCustomQuery() {
$json = ['user' => ['roles' => ['admin', 'editor']]];
$this->assertEquals(['admin'], $this->jsonPath->search($json, '$..roles[0]'));
}
Hook into Laravel Events
Run compliance checks on Model::saved or Job::handled:
use JsonPathComplianceTestSuite\Runner;
Event::listen('eloquent.saved', function ($model) {
$runner = new Runner();
$runner->runTests(); // Validate any JSONPath queries used in model logic
});
Visual Studio Code Integration
Add a task to .vscode/tasks.json for quick compliance checks:
{
"label": "Run JSONPath Compliance",
"type": "shell",
"command": "./vendor/bin/jsonpath-compliance-test-suite --format=compact",
"problemMatcher": []
}
How can I help you explore Laravel packages today?