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

相关内容

热门资讯

详细透视!wepoker辅助器... 详细透视!wepoker辅助器软件下载,微乐陕西麻将破解,教程书教程(确实有挂)-哔哩哔哩wepok...
揭露透视!pokemmo辅助官... 揭露透视!pokemmo辅助官网,微信小程序怎么开挂,攻略教程(有挂实锤)-哔哩哔哩1.pokemm...
科普透视!哈糖大菠萝破解器,手... 科普透视!哈糖大菠萝破解器,手机微乐小程序破解器如何使用,教材教程(今日头条)-哔哩哔哩1.哈糖大菠...
揭露透视!wepoker怎么看... 揭露透视!wepoker怎么看牌型,微信微乐游戏苹果辅助器,总结教程(有挂实锤)-哔哩哔哩1、完成w...
开挂透视!aa poker辅助... 开挂透视!aa poker辅助包,微乐家乡小程序脚本,方案教程(有挂秘籍)-哔哩哔哩1、下载好aa ...
教你透视!wepoker有没有... 教你透视!wepoker有没有透视方法,微信小程序怎么开挂,大纲教程(有挂透视)-哔哩哔哩1、许多玩...
普及透视!wpk透视是真的假的... 普及透视!wpk透视是真的假的,微信小程序游戏破解器,操作教程(果真有挂)-哔哩哔哩1、完成wpk透...
必备透视!德州私人局脚本,微乐... 必备透视!德州私人局脚本,微乐家乡小程序自建房辅助app,练习教程(有挂秘籍)-哔哩哔哩一、德州私人...
辅助透视!wepoker私人定... 辅助透视!wepoker私人定制透视,微乐麻辣自建房辅助视频,手筋教程(有挂攻略)-哔哩哔哩1、起透...
辅助透视!wepoker私人局... 辅助透视!wepoker私人局外卦,微乐小程序黑科技免费知乎,窍门教程(有挂存在)-哔哩哔哩wepo...