3

I have created a Service where JSON is retrieved from an external source and queried. I want to be able to test functions that use this service, independent of the service. How do I mock the JSON file in PHP Unit Tests in order to test these functions? The JSON file formatted like this:

{
    "data": [
        {
            "title": "Development",
            "children": [
                {
                    "title": "Settings",
                    "channel_types": [
                        {
                            "title": "Network"
                        }
                    ]
                },
                {
                    "title": "Testing",
                    "channel_types": [
                        {
                            "title": "Social"
                        }
                    ]
                }
            ],
            "created_at": 1523464038,
            "updated_at": 1523464038,
            "id": "5ace37664e1d4400a04ffaf2"
        }
    ]
}

1 Answer 1

5

You could save the json as a testing file, ie. storage/testing/json/test-data.json

And then retrieve it while setting up your test:

$path = storage_path("testing/json/test-data.json") ;

$json = file_get_contents($path); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.