DevSymmetryk's avatar

[Laravel 6.20.30] - Issue trying to assert Response structure based on type

  • Laravel Version: 6.20.30
  • PHP Version: 7.2.8
  • Database Driver & Version: Mysql 5.7

Description:

The issue I'm facing is that I need to test the structure of a response in which some properties are look-alike

Steps To Reproduce:

this is the response to test against :

[
  {
    "id": 1,
    "product_id": 1,
    "section_id": 1,
    "image_id": null,
    "html_id": 1,
    "index": 0,
    "name": "zip_2MB",
    "description": "Error ex sed numquam et odit minima quia sunt. Praesentium in ea numquam. Alias ut pariatur voluptates modi velit veniam aspernatur accusantium. Aut facilis minus reprehenderit praesentium eum.",
    "title": "Slide #53",
    "backup": false,
    "required": true,
    "emailable": 0,
    "type": 0,
    "asset_id": null,
    "thumb_id": null,
    "image": null,
    "html": {
      "id": 1,
      "product_id": 1,
      "zip_id": 1,
      "thumb_id": 1,
      "name": "zip_2MB",
      "rank": 0,
      "created_at": "2021-08-11 13:24:42",
      "updated_at": "2021-08-11 13:24:42",
      "deleted_at": null,
      "thumb": {
        "id": 1,
        "user_id": null,
        "key": "archive/zip_2MB.zip",
        "bucket": "symmetryk-assets-testing",
        "region": "eu-west-2",
        "cdnURL": "https://symmetryk-assets-testing.s3.eu-west-2.amazonaws.com/archive/zip_2MB.zip",
        "size": 2036861,
        "size_thumb": 0,
        "size_preview": 0,
        "mime_type": "application/zip",
        "extension": "zip",
        "width": null,
        "height": null,
        "duration": null,
        "viewed": null,
        "created_at": "2021-08-11 13:24:42",
        "updated_at": "2021-08-11 13:24:42",
        "deleted_at": null
      },
      "html": {
        "id": 1,
        "user_id": null,
        "key": "archive/zip_2MB.zip",
        "bucket": "symmetryk-assets-testing",
        "region": "eu-west-2",
        "cdnURL": "https://symmetryk-assets-testing.s3.eu-west-2.amazonaws.com/archive/zip_2MB.zip",
        "size": 2036861,
        "size_thumb": 0,
        "size_preview": 0,
        "mime_type": "application/zip",
        "extension": "zip",
        "width": null,
        "height": null,
        "duration": null,
        "viewed": null,
        "created_at": "2021-08-11 13:24:42",
        "updated_at": "2021-08-11 13:24:42",
        "deleted_at": null
      }
    }
  },
  {
    "id": 2,
    "product_id": 1,
    "section_id": 1,
    "image_id": 2,
    "html_id": null,
    "index": 1,
    "name": "file_example_JPG_500kB",
    "description": "Et voluptatem et corporis in.",
    "title": "Slide #78",
    "backup": false,
    "required": false,
    "emailable": 0,
    "type": 2,
    "asset_id": 2,
    "thumb_id": 2,
    "image": {
      "id": 2,
      "product_id": 1,
      "image_id": 2,
      "thumb_id": 2,
      "name": "file_example_JPG_500kB",
      "rank": 1,
      "created_at": "2021-08-11 13:25:23",
      "updated_at": "2021-08-11 13:25:23",
      "deleted_at": null,
      "thumb": {
        "id": 2,
        "user_id": null,
        "key": "image/file_example_JPG_500kB.jpeg",
        "bucket": "symmetryk-assets-testing",
        "region": "eu-west-2",
        "cdnURL": "https://symmetryk-assets-testing.s3.eu-west-2.amazonaws.com/image/file_example_JPG_500kB.jpeg",
        "size": 555181,
        "size_thumb": 0,
        "size_preview": 0,
        "mime_type": "image/jpeg",
        "extension": "jpeg",
        "width": null,
        "height": null,
        "duration": null,
        "viewed": null,
        "created_at": "2021-08-11 13:25:23",
        "updated_at": "2021-08-11 13:25:23",
        "deleted_at": null
      },
      "image": {
        "id": 2,
        "user_id": null,
        "key": "image/file_example_JPG_500kB.jpeg",
        "bucket": "symmetryk-assets-testing",
        "region": "eu-west-2",
        "cdnURL": "https://symmetryk-assets-testing.s3.eu-west-2.amazonaws.com/image/file_example_JPG_500kB.jpeg",
        "size": 555181,
        "size_thumb": 0,
        "size_preview": 0,
        "mime_type": "image/jpeg",
        "extension": "jpeg",
        "width": null,
        "height": null,
        "duration": null,
        "viewed": null,
        "created_at": "2021-08-11 13:25:23",
        "updated_at": "2021-08-11 13:25:23",
        "deleted_at": null
      }
    },
    "html": null
  }
]

this is can be returned from a route for easy testing.

$request = $this->get('/dummy-url-to-return-the-response');

$request
->assertJsonStructure(
[
            '*' => [
                'id',
                'product_id',
                'section_id',
                'image_id',
                'html_id',
                'index',
                'name',
                'description',
                'title',
                'backup',
                'required',
                'emailable',
                'type',
                'asset_id',
                'thumb_id',
                'image' => [
                    '*' => [
                        'id',
                        'product_id',
                        'image',
                        'thumb'
                    ]
                ],
                'html' => [
                    '*' => [
                        'id',
                        'product_id',
                        'image',
                        'thumb'
                    ]
                ]
            ]
        ]
)

If an object have an html object, the image object is null and vis versa.

Keep in mind that the structure was truncated for easier follow so assuming that I am writing this correctly, this is the message I keep getting.

Argument #2 of PHPUnit\Framework\Assert::assertArrayHasKey() must be an array or ArrayAccess
0 likes
0 replies

Please or to participate in this conversation.