assertMatchesResourceCollectionJsonSchema出现“NULLvaluefound,butanobjectisrequired”错误
创始人
2024-09-20 16:00:54
0

这个错误通常出现在使用PHPUnit测试框架时,检查JSON响应是否符合所期望的架构。通常是因为响应JSON为空或者响应格式不正确导致的。

解决方法为先检查JSON响应是否为空,然后检查响应格式是否正确。可以尝试使用var_dump函数打印结果来调试该问题。

以下示例演示了如何使用PHPUnit测试框架和纯PHP来进行JSON格式检验的示例:

// 在测试框架中使用PHPUnit和assertMatchesResourceCollectionJsonSchema public function testApiEndpointReturnsSomeData() { $client = new GuzzleHttp\Client(); $response = $client->get('http://example.com/api/endpoint');

$data = json_decode($response->getBody()->getContents(), true);
$schema = file_get_contents(__DIR__ . '/schemas/endpoint-collection.json');

$this->assertArrayHasKey('data', $data, 'Response does not have a "data" key.');

$this->assertMatchesResourceCollectionJsonSchema($schema, $data);

}

// 使用纯PHP进行JSON格式检验示例 $jsonResponse = '{ "data": [ { "id": 1, "name": "John" }, { "id": 2, "name": "Jane" } ] }';

$schema = '{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "required" : [ "data" ], "properties" : { "data": { "type": "array", "items": { "type": "object", "required": [ "id", "name" ], "properties": { "id": { "type": "integer" }, "name": { "type": "string" } } } } } }';

$data = json_decode($jsonResponse, true); $schema = json_decode($schema, true);

$validator

相关内容

热门资讯

透视科技!hhpoker德州透... 透视科技!hhpoker德州透视挂(透视)都是是真的辅助辅助器(了解有挂)-哔哩哔哩1、每一步都需要...
今天下午!菠萝辅助器免费版的功... 今天下午!菠萝辅助器免费版的功能介绍(透视)好像真的有辅助修改器(有挂方法)-哔哩哔哩在进入菠萝辅助...
透视辅助!wepoker有辅助... 透视辅助!wepoker有辅助吗(透视)一直真的是有辅助神器(果真有挂)-哔哩哔哩1、下载好wepo...
为了进一步!hhpoker有后... 为了进一步!hhpoker有后台操控吗(透视)原来有辅助下载(有挂规律)-哔哩哔哩1、超多福利:超高...
更值得关注的是!aapoker... 更值得关注的是!aapoker脚本怎么用(透视)果然是真的辅助安装(今日头条)-哔哩哔哩1、金币登录...
透视最新!wepoker游戏的... 透视最新!wepoker游戏的安装教程(透视)其实真的有辅助安装(有挂规律)-哔哩哔哩一、wepok...
透视脚本!wepoker作必弊... 透视脚本!wepoker作必弊方法(透视)好像是真的辅助辅助器(有挂教程)-哔哩哔哩1、起透看视 w...
透视辅助!werplan透视挂... 透视辅助!werplan透视挂(透视)其实真的是有辅助修改器(有挂分享)-哔哩哔哩1、游戏颠覆性的策...
反观!wepoker破解工具(... 反观!wepoker破解工具(透视)总是是有辅助app(真的有挂)-哔哩哔哩小薇(辅助器软件下载)致...
今日!wepoker好友房开挂... 今日!wepoker好友房开挂(透视)都是真的有辅助修改器(证实有挂)-哔哩哔哩;wepoker好友...