I do a lot with fixtures when testing. This means I put files in the tests/fixtures folder that represet the JSON of what ever I am testing. For example maybe I hit an API that returns

{
 "foo": "bar"
}

And when I mock that API response I want to have that ready for me to use.

I then save it to the folder tests/fixtures/foo.json and can just use it.

To do that I just do the following.

Setup a helpers.php file in your app

Add the file app/helpers.php for now with <?php on top of it.

<?php

Not to exciting yet!

Then add it to composer.json

File: composer.json
38:     "autoload-dev": {
39:         "psr-4": {
40:             "Tests\\": "tests/"
41:         },
42:         "files": [
43:             "app/helpers.php"
44:         ]
45:     },

Now run composer dump and you are ready to add helpers to your app.

Add get and put helpers

To that file I add the getter and putter(?):

<?php 

use Illuminate\Support\Facades\File;

if (!function_exists("put_fixture")) {
    function put_fixture($file_name, $content = [], $json = true)
    {
        if ($json) {
            $content = json_encode($content, 128);
        }
        File::put(
            base_path(sprintf("tests/fixtures/%s", $file_name)),
            $content
        );
        return true;
    }
}

if (!function_exists("get_fixture")) {
    function get_fixture($file_name)
    {
        $results = File::get(base_path(sprintf(
            "tests/fixtures/%s",
            $file_name
        )));
        return json_decode($results, true);
    }
}

and I make the folder tests/fixtures

And now in my tests I can do this:

put_fixture('meta_data.json', $response_from_api_as_array);

NOTE: Many apis have good docs that sometimes show a JSON response I can save. But somtimes I want a real response to save it so I hit the api ONCE from my test and mock it from then on

and I can use that later just like this

get_fixture('meta_data.json', $response_from_api_as_array);

This will simply put a file and get that file tests/fixtures/meta_data.json

And the file will be JSON for example:

{
 "foo": "bar"
}