digitaladapt / codeception-json-schema
此软件包已被放弃,不再维护。作者建议使用 ginside/codeception-json-schema 软件包。
Codeception 测试框架的 Json schema 模块,用于验证响应是否符合 json schema。
v0.1.2
2017-08-11 02:25 UTC
Requires
- php: ^5.4||^7.0
- justinrainbow/json-schema: ^5.2
Requires (Dev)
- codeception/codeception: ^2.2
README
Codeception 模块,用于在 schema 上验证 json。
安装
composer require digitaladapt/codeception-json-schema
用法
<?php class MessageApiCest { public function aTest(ApiTester $I) { /* call api */ $I->wantTo('Ensure API Returns Json which matches schema file.'); $I->sendGET('/path/to/api'); /* check if api matches schema */ $I->seeResponseIsValidOnSchemaFile('/path/to/schema.json'); } public function bTest(ApiTester $I) { /* call api */ $I->wantTo('Ensure API Returns Json which matches schema file.'); $I->sendGET('/path/to/api'); /* alternative syntax, check if api matches schema */ $I->canSeeResponseIsValidOnSchemaFile('/path/to/schema.json'); } public function cTest(ApiTester $I) { /* call api */ $I->wantTo('Ensure API Returns Json which matches inline schema.'); $I->sendGET('/path/to/api'); /* if you don't have a separate schema file, that is alright, you can use inline schema */ /* this schema expects the api to return something like {"message": "SOME_STRING"} */ /* schema as php objects */ $schema = (object)[ 'type' => 'object', 'properties' => (object)[ 'message' => (object)[ 'type' => 'string', ], ], 'required' => ['message'], ]; $I->seeResponseIsValidOnSchema($schema); } public function dTest(ApiTester $I) { /* call api */ $I->wantTo('Ensure API Returns Json which matches inline schema.'); $I->sendGET('/path/to/api'); /* if you don't have a separate schema file, that is alright, you can use inline schema */ /* this schema expects the api to return something like {"message": "SOME_STRING"} */ /* json string, must be decoded before checking if response is valid */ $jsonSchema = '{"type":"object","properties":{"message":{"type":"string"}},"required":["message"]}'; /* alternative syntax, notice we decoded the json string */ $I->canSeeResponseIsValidOnSchema(json_decode($jsonSchema)); } }
也参见
Codeception 有一个用于简单 json 匹配的内建语法 seeResponseMatchesJsonType,然而它与 json schema 不兼容,这就是创建此模块的原因。
<?php class MessageApiCest { public function eTest(ApiTester $I) { /* call api */ $I->wantTo('Ensure API Returns Json which matches type.'); $I->sendGET('/path/to/api'); /* this type expects the api to return something like {"message": "SOME_STRING"} */ $I->seeResponseMatchesJsonType([ 'message' => 'string', ]); } }
贡献
如果您发现任何错误或对如何改进此软件有任何建议,请提交一个 issue。