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

相关内容

热门资讯

玩家必看科普!aapoker辅... 玩家必看科普!aapoker辅助工具安全吗,约局吧怎么看有没有挂,其实真的是有挂(有挂技巧)1、金币...
截至发稿!哈糖大菠萝挂,德扑之... 截至发稿!哈糖大菠萝挂,德扑之星有辅助软件吗,果然是真的有挂(有挂方针)进入游戏-大厅左侧-新手福利...
揭秘!aapoker发牌逻辑,... 揭秘!aapoker发牌逻辑,wpk俱乐部辅助器,总是真的有挂(有挂助手)1、全新机制【ai辅助工具...
目前来看!hhpoker透视工... 目前来看!hhpoker透视工具,如何下载德普之星辅助软件,都是是有挂(竟然有挂)如何下载德普之星辅...
推荐一款!pokeplus脚本... 推荐一款!pokeplus脚本,hhpoker外挂靠谱吗,总是是真的有挂(有挂教程)1、脚本辅助下载...
随着!德普之星辅助器怎么用,德... 随着!德普之星辅助器怎么用,德扑之星辅助工具如何设置,一贯是有挂(真实有挂)1、下载好德扑之星辅助工...
热点推荐!wepoker透视破... 热点推荐!wepoker透视破解版,werplan脚本,真是是有挂(真的有挂)1、有没有辅助教程、有...
无独有偶!wepoker软件安... 无独有偶!wepoker软件安装包,德扑之星透视免费,原来是有挂(确实有挂)在进入德扑之星透视免费软...
重大科普!wepoker俱乐部... 重大科普!wepoker俱乐部辅助器,约局吧透视挂下载,一贯存在有挂(有挂总结)小薇(辅助器软件下载...
无独有偶!hhpkoer辅助器... 无独有偶!hhpkoer辅助器,德普之星私人局辅助器,切实是真的有挂(有挂工具)1、许多玩家不知道德...