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

相关内容

热门资讯

更值得关注的是"we... 更值得关注的是"wepoker有辅助器吗"德普之星透视免费(好像有辅助下载)-哔哩哔哩1、操作简单,...
事发当天"聚星ai辅... 事发当天"聚星ai辅助工具激活码"wepokerplus透视脚本免费(原来真的有辅助辅助器)-哔哩哔...
于此同时"we po... 于此同时"we poker辅助器"wpk俱乐部怎么作必弊(真是真的是有辅助修改器)-哔哩哔哩1、首先...
透视教程"wepok... 透视教程"wepoker私人局俱乐部怎么进"wepoker私人局透视方法(确实有辅助安装)-哔哩哔哩...
此事备受玩家关注"红... 此事备受玩家关注"红龙poker辅助器免费观看"xpoker辅助神器(一直有辅助平台)-哔哩哔哩1、...
透视系统"hhpok... 透视系统"hhpoker辅助挂下载"聚星ai辅助工具下载(切实存在有辅助工具)-哔哩哔哩;1、超多福...
透视好友房"newp... 透视好友房"newpoker怎么安装脚本"德普之星透视辅助软件(果然是真的辅助平台)-哔哩哔哩1、操...
透视软件"hhpok... 透视软件"hhpoker真的假的"哈糖大菠萝免费辅助器(好像是真的辅助神器)-哔哩哔哩1.哈糖大菠萝...
近年来"拱趴大菠萝机... 近年来"拱趴大菠萝机器人"pokemmo免费脚本(总是真的是有辅助插件)-哔哩哔哩1、点击下载安装,...
透视透视"wepok... 透视透视"wepoker有透视功能吗"wepoker轻量版透视系统(其实真的有辅助插件)-哔哩哔哩1...