Unit testing of json encoding/decoding against errors

微笑、不失礼 提交于 2019-12-25 02:50:38

问题


So basically I have the code which is giving me the message from json_last_error():

$msg = 'Unknown error';
switch (json_last_error()) {
    case JSON_ERROR_NONE:
        $msg = null;
        break;
    case JSON_ERROR_DEPTH:
        $msg = 'Maximum stack depth exceeded';
        break;
    case JSON_ERROR_STATE_MISMATCH:
        $msg = 'Underflow or the modes mismatch';
        break;
    case JSON_ERROR_CTRL_CHAR:
        $msg = 'Unexpected control character found';
        break;
    case JSON_ERROR_SYNTAX:
        $msg = 'Syntax error, malformed JSON';
        break;
    case JSON_ERROR_UTF8:
        $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
}
return $msg;

For testing purposes I want to raise all the errors from this list to have 100% coverage, but I can't raise JSON_ERROR_STATE_MISMATCH.

Can anyone help giving me the example with either encoding or decoding, with any parameters, which can produce this error?


回答1:


$j = '{"j": 1 ] }';

json_decode($j);

var_dump(json_last_error() === JSON_ERROR_STATE_MISMATCH); // true

How I found it: just checked the source code :-)



来源:https://stackoverflow.com/questions/24543489/unit-testing-of-json-encoding-decoding-against-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!