PHP code example of ashc / laravel-json-asserter

1. Go to this page and download the library: Download ashc/laravel-json-asserter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ashc / laravel-json-asserter example snippets


$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'
                         ]);
                 });
         });
});

$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
                    ]
                ]
            ]
        ]
    ]);
});

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

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

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

[
    '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
                        ]
                    ]
                ]
            ]
        ]
    ]
]