>我创建了一个服务,其中从外部源检索并查询 JSON。我希望能够独立于服务测试使用此服务的函数。如何在 PHP 单元测试中模拟 JSON 文件以测试这些函数?格式如下的 JSON 文件:
{
"data": [
{
"title": "Development",
"children": [
{
"title": "Settings",
"channel_types": [
{
"title": "Network"
}
]
},
{
"title": "Testing",
"channel_types": [
{
"title": "Social"
}
]
}
],
"created_at": 1523464038,
"updated_at": 1523464038,
"id": "5ace37664e1d4400a04ffaf2"
}
]
}
您可以将 json 保存为测试文件,即 storage/testing/json/test-data.json
然后在设置测试时检索它:
$path = storage_path("testing/json/test-data.json") ;
$json = file_get_contents($path);