PHPackages                             ashc/laravel-json-asserter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. ashc/laravel-json-asserter

ActiveLibrary[Testing &amp; Quality](/categories/testing)

ashc/laravel-json-asserter
==========================

A trait for simplifying HTTP tests in Laravel

v0.2.0(2y ago)14MITPHPPHP ^8.2

Since Mar 22Pushed 2y ago1 watchersCompare

[ Source](https://github.com/ashleycoles/laravel-json-asserter)[ Packagist](https://packagist.org/packages/ashc/laravel-json-asserter)[ RSS](/packages/ashc-laravel-json-asserter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (5)Used By (0)

laravel-json-asserter
=====================

[](#laravel-json-asserter)

A prototype tool to make writing and reading Laravel JSON assertions less painfull.

Example
-------

[](#example)

Take the following JSON structure

```
{
    "message": "Employees retrieved",
    "data": [
        {
            "id": 1,
            "name": "Carolyn Aufderhar",
            "age": 46,
            "start_date": "2005-10-27",
            "contract": {
                "id": 1,
                "name": "Ullam earum."
            },
            "certifications": [
                {
                    "id": 1,
                    "name": "Error aspernatur.",
                    "description": "Et eos minima ut aliquam est. Odit quia quasi ut suscipit."
                }
            ]
        }
    ]
}
```

Using the fluent JSON assertion system in Laravel to test the structure of the JSON we produce a test that looks like this:

```
$response->assertJson(function (AssertableJson $json) {
     $json->hasAll(['message', 'data'])
         ->whereType('message', 'string')
         ->has('data', 1, function (AssertableJson $json) {
             $json->hasAll(['id', 'name', 'age' , 'start_date', 'contract', 'certifications'])
                 ->whereAllType([
                     'id' => 'integer',
                     'name' => 'string',
                     'age' => 'integer',
                     'start_date' => 'string'
                 ])
                 ->has('contract', function (AssertableJson $json) {
                     $json->hasAll(['id', 'name'])
                         ->whereAllType([
                             'id' => 'integer',
                             'name' => 'string'
                         ]);
                 })
                 ->has('certifications', 1, function (AssertableJson $json) {
                     $json->hasAll(['id', 'name', 'description'])
                         ->whereAllType([
                             'id' => 'integer',
                             'name' => 'string',
                             'description' => 'string'
                         ]);
                 });
         });
});
```

Compared to the prototype assertion helper:

```
$response->assertJson(function (AssertableJson $json) {
    $this->assertJsonHelper($json, [
        'message' => Type::STRING,
        'data' => [
            'count' => 1,
            'values' => [
                'id' => Type::INTEGER,
                'name' => Type::STRING,
                'age' => Type::INTEGER,
                'start_date' => Type::STRING,
                'contract' => [
                    'values' => [
                        'id' => Type::INTEGER,
                        'name' => Type::STRING
                    ]
                ],
                'certifications' => [
                    'count' => 1,
                    'values' => [
                        'id' => Type::INTEGER,
                        'name' => Type::STRING,
                        'description' => Type::STRING
                    ]
                ]
            ]
        ]
    ]);
});
```

Usage
-----

[](#usage)

JsonAsserter uses an array to describe the structure and datatype of the JSON, and then uses the Laravel fluent JSON testing API behind the scenes to generate assertions.

It makes a `JsonAsserter` trait available, just use the trait in your test files (or TestCase.php to automatically apply to all tests).

For JSON fields that are simple data-types, you can use `string`, `integer`, `double`, `boolean` and `null` - the same as with Laravel's `whereType()` and `whereAllType()` methods.

In addition to the standard Laravel types, you can use a type of `missing` to assert that the field is absent from the response.

```
[
    'name' => Type::STRING,
    'age' => Type::INTEGER,
    'likes_fluent_json_testing_syntax' => Type::BOOLEAN,
    'example' => Type::MISSING
]
```

For arrays and objects, you can use a nested array to describe the structure of the array/object.

For an object, the array must have a values subarray.

```
[
    'contract' => [
        'values' => [
            'id' => Type::INTEGER,
            'name' => Type::STRING
        ]
    ]
]
```

For an array, the array must have both values and count. Count representing the number of results expected in the array.

```
[
    'certifications' => [
        'count' => 1,
        'values' => [
            'id' => Type::INTEGER,
            'name' => Type::STRING,
            'description' => Type::STRING
        ]
    ]
]
```

Nesting objects within arrays and vice versa is of course allowed.

```
[
    'friends' => [
        'count' => 10,
        'values' => [
            'id' => Type::INTEGER,
            'name' => Type::STRING,
            'hobbies' => [
                'count' => 3,
                'values' => [
                    'id' => Type::INTEGER,
                    'hobby' => Type::STRING,
                    'difficulty' => [
                        'values' => [
                            'name' => Type::STRING,
                            'score' => Type::DOUBLE
                        ]
                    ]
                ]
            ]
        ]
    ]
]
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

778d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c899cbe9de5439f8bae74f3b52648fbfd266473016961c69a7947b48800b220?d=identicon)[ashc](/maintainers/ashc)

---

Top Contributors

[![ashleycoles](https://avatars.githubusercontent.com/u/19924378?v=4)](https://github.com/ashleycoles "ashleycoles (23 commits)")

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/ashc-laravel-json-asserter/health.svg)

```
[![Health](https://phpackages.com/badges/ashc-laravel-json-asserter/health.svg)](https://phpackages.com/packages/ashc-laravel-json-asserter)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[pdepend/pdepend

Official version of pdepend to be handled with Composer

954110.9M815](/packages/pdepend-pdepend)[jasonmccreary/laravel-test-assertions

A set of helpful assertions when testing Laravel applications.

3513.9M32](/packages/jasonmccreary-laravel-test-assertions)[instaclick/php-webdriver

PHP WebDriver for Selenium 2

43761.8M22](/packages/instaclick-php-webdriver)[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69417.9M510](/packages/spatie-phpunit-snapshot-assertions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
