/** * Returns api docs for given API name. * @param string $name * @return array */ function getDoc($name) { $result = $this->swagger->getResource($name); foreach ($result['apis'] as &$api) { $api['path'] = $this->pathPrefix . $api['path']; } return $result; }
public function testGenerate() { $generator = new Swagger(1, 'http://api.phpsx.org', 'http://foo.phpsx.org'); $json = $generator->generate($this->getResource()); $expect = <<<'JSON' { "swaggerVersion": "1.2", "apiVersion": 1, "basePath": "http:\/\/api.phpsx.org", "resourcePath": "\/foo\/bar", "apis": [ { "path": "\/foo\/bar", "description": "lorem ipsum", "operations": [ { "method": "GET", "nickname": "getCollection", "parameters": [ { "paramType": "path", "name": "name", "type": "string", "description": "Name parameter", "required": false, "minimum": 0, "maximum": 16 }, { "paramType": "path", "name": "type", "type": "string", "enum": ["foo", "bar"] }, { "paramType": "query", "name": "startIndex", "type": "integer", "description": "startIndex parameter", "required": false, "minimum": 0, "maximum": 32 }, { "paramType": "query", "name": "float", "type": "number" }, { "paramType": "query", "name": "boolean", "type": "boolean" }, { "paramType": "query", "name": "date", "type": "string", "format": "date" }, { "paramType": "query", "name": "datetime", "type": "string", "format": "date-time" } ], "summary": "Returns a collection", "responseMessages": [ { "code": 200, "message": "Response", "responseModel": "getResponse" } ] }, { "method": "POST", "nickname": "postItem", "parameters": [ { "paramType": "path", "name": "name", "type": "string", "description": "Name parameter", "required": false, "minimum": 0, "maximum": 16 }, { "paramType": "path", "name": "type", "type": "string", "enum": ["foo", "bar"] }, { "paramType": "body", "name": "body", "required": true, "type": "postRequest" } ], "responseMessages": [ { "code": 200, "message": "Response", "responseModel": "postResponse" } ] }, { "method": "PUT", "nickname": "putItem", "parameters": [ { "paramType": "path", "name": "name", "type": "string", "description": "Name parameter", "required": false, "minimum": 0, "maximum": 16 }, { "paramType": "path", "name": "type", "type": "string", "enum": ["foo", "bar"] }, { "paramType": "body", "name": "body", "required": true, "type": "putRequest" } ], "responseMessages": [ { "code": 200, "message": "Response", "responseModel": "putResponse" } ] }, { "method": "DELETE", "nickname": "deleteItem", "parameters": [ { "paramType": "path", "name": "name", "type": "string", "description": "Name parameter", "required": false, "minimum": 0, "maximum": 16 }, { "paramType": "path", "name": "type", "type": "string", "enum": ["foo", "bar"] }, { "paramType": "body", "name": "body", "required": true, "type": "deleteRequest" } ], "responseMessages": [ { "code": 200, "message": "Response", "responseModel": "deleteResponse" } ] } ] } ], "models": { "getResponse": { "id": "getResponse", "properties": { "entry": { "type": "array", "items": { "$ref": "ref993f4bb37f524889fc963fedd6381458" } } } }, "postRequest": { "id": "postRequest", "required": [ "title", "date" ], "properties": { "id": { "type": "integer" }, "userId": { "type": "integer" }, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": { "type": "string" } } }, "postResponse": { "id": "postResponse", "properties": { "success": { "type": "boolean" }, "message": { "type": "string" } } }, "putRequest": { "id": "putRequest", "required": [ "id" ], "properties": { "id": { "type": "integer" }, "userId": { "type": "integer" }, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": { "type": "string" } } }, "putResponse": { "id": "putResponse", "properties": { "success": { "type": "boolean" }, "message": { "type": "string" } } }, "deleteRequest": { "id": "deleteRequest", "required": [ "id" ], "properties": { "id": { "type": "integer" }, "userId": { "type": "integer" }, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": { "type": "string" } } }, "deleteResponse": { "id": "deleteResponse", "properties": { "success": { "type": "boolean" }, "message": { "type": "string" } } }, "ref993f4bb37f524889fc963fedd6381458": { "id": "ref993f4bb37f524889fc963fedd6381458", "properties": { "id": { "type": "integer" }, "userId": { "type": "integer" }, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": { "type": "string" } } } } } JSON; $this->assertJsonStringEqualsJsonString($expect, $json); }