wheesnoza/assertable-json-enhancer is a package that extends Laravel's AssertableJson class. This package enables more flexible and powerful assertions when testing JSON responses. It leverages the Macroable trait to provide several useful assertion methods.
You can install this package via Composer:
composer require wheesnoza/assertable-json-enhancerAfter installation, this package automatically registers the necessary macros to the AssertableJson class. No additional setup is required.
This package is fully compatible with Laravel Inertia's AssertableInertia. You can use the methods provided by wheesnoza/assertable-json-enhancer when writing assertions for Inertia responses.
- whereStringContains
- whereLessThan
- whereLessThanOrEqual
- whereGreaterThan
- whereGreaterThanOrEqual
- whereIsArray
- whereArrayHasAtLeast
- whereArrayHasSize
- whereStringStartsWith
- whereStringEndsWith
- whereExactLength
- whereMatchesPattern
- whereIsString
- whereIsInteger
- whereIsBoolean
- whereIsFloat
- whereIsEmpty
- whereIsNotEmpty
- whereStringEquals
- whereStringNotEquals
- whereResultsAreOrderedBy
- whereResultsContain
- whereResultsMatchCriteria
Asserts that the string at the given key contains the specified substring.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereStringContains('data.name', 'John')
);Asserts that the value at the given key is less than the specified value.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereLessThan('data.age', 18)
);Asserts that the value at the given key is less than or equal to the specified value.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereLessThanOrEqual('data.age', 18)
);Asserts that the value at the given key is greater than the specified value.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereGreaterThan('data.age', 18)
);Asserts that the value at the given key is greater than or equal to the specified value.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereGreaterThanOrEqual('data.age', 18)
);Asserts that the value at the given key is an array.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereIsArray('data.items')
);Asserts that the array at the given key has at least the specified number of elements.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereArrayHasAtLeast('data.items', 3)
);Asserts that the array at the given key has exactly the specified number of elements.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereArrayHasSize('data.items', 3)
);Asserts that the string at the given key starts with the specified prefix.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereStringStartsWith('data.title', 'Laravel')
);Asserts that the string at the given key ends with the specified suffix.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereStringEndsWith('data.title', 'Framework')
);Asserts that the string at the given key has exactly the specified length.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereExactLength('data.code', 10)
);Asserts that the string at the given key matches the specified regular expression pattern.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereMatchesPattern('data.email', '/^[\w\-\.]+@([\w\-]+\.)+[\w\-]{2,4}$/')
);Asserts that the value at the given key is a string.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereIsString('data.name')
);Asserts that the value at the given key is an integer.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereIsInteger('data.age')
);Asserts that the value at the given key is a boolean.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereIsBoolean('data.active')
);Asserts that the value at the given key is a float.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereIsFloat('data.price')
);Asserts that the value at the given key is empty (null, empty string, empty array, etc.).
$response->assertJson(fn (AssertableJson $json) =>
$json->whereIsEmpty('data.name')
);Asserts that the value at the given key is not empty.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereIsNotEmpty('data.name')
);Asserts that the string at the given key is exactly equal to the specified value.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereStringEquals('data.status', 'active')
);Asserts that the string at the given key is not equal to the specified value.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereStringNotEquals('data.status', 'inactive')
);Asserts that the array at the given key is ordered by the specified key in the given direction.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereResultsAreOrderedBy('data.items', 'id', 'asc')
);Asserts that the array at the given key contains the specified value.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereResultsContain('data.items', 2)
);Asserts that the array at the given key matches the specified criteria. If partialMatch is true, it allows partial matching of the criteria values.
$response->assertJson(fn (AssertableJson $json) =>
$json->whereResultsMatchCriteria('data.items', ['name' => 'Laravel'], true)
);Contributions are welcome! Feel free to submit bug reports or pull requests. If you have any suggestions or improvements, please share them on GitHub.
This package is open-sourced software licensed under the MIT license.