murilo's avatar
Level 10

Does have any how or any script that I can transform my json into pest assertJsonStructure format ?

hello , I have been working with Pest , and sometimes I have to check ( assertJsonStructure ) of a list . and sometimes , this list have many items to check . I wold like to know if in laravel or Pest , it has any script that could transform that list , with json format , something from this -

{"data":[{"id":"5687dd60-f0dd-46cc-a2f4-9f555c26d408","email":"[email protected]","status":"active","presentation":{"title":"Dana Pouros Nader ( repudiandae ) ","url_title":"dana-pouros-nader-repudiandae-6981377","description":"Neque et est ea deleniti rerum. Dolores quis et quia voluptatem non esse neque non.","url_website":"www.website.com","image_profile":null}}],"pagination":{"total":1,"per_page":20,"current_page":1,"last_page":1,"from":1,"to":1}}

to this (into Pest assertJsonStructure ) -

['data' => ['*' => ['id','email','status','presentation' => ['title','url_title','description','url_website','image_profile']]],
                                     'pagination' => ['total', 'per_page', 'current_page', 'last_page', 'from', 'to']]

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To transform your JSON structure into a format suitable for Pest's assertJsonStructure, you can write a PHP script that recursively parses the JSON and generates the corresponding structure array. Below is a solution that demonstrates how to achieve this:

  1. Decode the JSON string into a PHP array.
  2. Recursively traverse the array to build the structure array.
  3. Print the resulting structure array.

Here's a PHP script to do that:

<?php

$json = '{"data":[{"id":"5687dd60-f0dd-46cc-a2f4-9f555c26d408","email":"[email protected]","status":"active","presentation":{"title":"Dana Pouros Nader ( repudiandae ) ","url_title":"dana-pouros-nader-repudiandae-6981377","description":"Neque et est ea deleniti rerum. Dolores quis et quia voluptatem non esse neque non.","url_website":"www.website.com","image_profile":null}}],"pagination":{"total":1,"per_page":20,"current_page":1,"last_page":1,"from":1,"to":1}}';

$array = json_decode($json, true);

function buildStructure(array $array) {
    $structure = [];
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            if (array_keys($value) === range(0, count($value) - 1)) {
                // Handle list of items
                $structure[$key] = ['*' => buildStructure($value[0])];
            } else {
                // Handle associative array
                $structure[$key] = buildStructure($value);
            }
        } else {
            $structure[] = $key;
        }
    }
    return $structure;
}

$structure = buildStructure($array);

print_r($structure);

This script will output the structure array in a format that you can use with Pest's assertJsonStructure method. The output will look like this:

Array
(
    [data] => Array
        (
            [*] => Array
                (
                    [0] => id
                    [1] => email
                    [2] => status
                    [presentation] => Array
                        (
                            [0] => title
                            [1] => url_title
                            [2] => description
                            [3] => url_website
                            [4] => image_profile
                        )

                )

        )

    [pagination] => Array
        (
            [0] => total
            [1] => per_page
            [2] => current_page
            [3] => last_page
            [4] => from
            [5] => to
        )

)

You can then convert this array to the format required by Pest's assertJsonStructure:

$expectedStructure = [
    'data' => [
        '*' => [
            'id',
            'email',
            'status',
            'presentation' => [
                'title',
                'url_title',
                'description',
                'url_website',
                'image_profile'
            ]
        ]
    ],
    'pagination' => [
        'total',
        'per_page',
        'current_page',
        'last_page',
        'from',
        'to'
    ]
];

You can use this $expectedStructure in your Pest test like this:

test('json structure', function () {
    $response = $this->getJson('/your-endpoint');
    $response->assertJsonStructure($expectedStructure);
});

This approach will help you automate the conversion of your JSON structure into the format required for Pest's assertJsonStructure.

Please or to participate in this conversation.