ovr / swagger-assert-helper
此包的最新版本(1.1.6)没有提供许可信息。
1.1.6
2018-02-04 12:55 UTC
Requires
- php: >=5.6
- flow/jsonpath: ~0.3.1
- phpunit/phpunit: >=4.8 <6.0
- zircote/swagger-php: ^2.0.13
Requires (Dev)
- fzaninotto/faker: ~1.6.0
- illuminate/http: 5.2.37
- phpunit/phpunit: ~5.4.6
- slim/slim: 3.0.0
- symfony/http-foundation: 3.0.0
- symfony/var-dumper: ~3.3.0
- zendframework/zend-http: 2.5.4
README
此库提供了以下支持:
- 通过 Swagger 路径进行 HTTP 请求
- 使用 Swagger 响应模式断言 HTTP 响应
- 在以下框架之上进行功能测试:
Zend
、Laravel
、Slim
、Symfony
- 在以下之上进行集成测试:
Guzzle
通过 Composer 安装
您可以使用 Composer .
composer require ovr/swagger-assert-helper
如何使用?
1. 为您的 API 编写 Swagger 定义
我喜欢使用 PHP 注释,例如
/** * @SWG\Definition( * definition = "UserResponse", * required={"id", "name"}, * @SWG\Property(property="id", type="integer", format="int64"), * @SWG\Property(property="name", type="string"), * ); */ class UserController extends AbstractController { /** * @SWG\Get( * tags={"User"}, * path="/user/{id}", * operationId="getUserById", * summary="Find user by $id", * @SWG\Parameter( * name="id", * description="$id of the specified", * in="path", * required=true, * type="string" * ), * @SWG\Response( * response=200, * description="success", * @SWG\Schema(ref="#/definitions/UserResponse") * ), * @SWG\Response( * response=404, * description="Not found" * ) * ) */ public function getAction() {} }
更多定义示例可以在以下找到
2. 为您的控制器编写测试
功能测试
功能测试 - 当你在你的服务(PHP
代码)中执行 Request
时,支持以下
- SymfonyTrait - Symfony
- LaravelTrait - Laravel
- ZendTrait - Zend
- SlimTrait - Slim
示例
class UserControllerTest extends \PHPUnit\Framework\TestCase { // You should use trait for your framework, review supported and use what you need use \Ovr\Swagger\SymfonyTrait; public function testGetUserById() { // We define operation called getUserById in first step! $operation = $this->getSwaggerWrapper()->getOperationByName('getUserById'); // Call makeRequestByOperation from our framework Trait, SymfonyTrait for us $request = $this->makeRequestByOperation( $operation, [ 'id' => 1 ] ); // This will be \Symfony\Component\HttpFoundation\Request var_dump($request); // You should execute your API module by Request and get Response $response = $this->getApi()->handle($request); $this->getSwaggerWrapper()->assertHttpResponseForOperation( // Call makeRequestByOperation from our framework Trait, SymfonyTrait for us $this->extractResponseData($response), // getUserById $operation, // Operation can response by codes that your defined, lets assert that it will be 200 (HTTP_OK) Response::HTTP_OK ); } /** * Return API module/service/bundle, that handle request and return Response for it */ abstract public function getApi(); /** * SwaggerWrapper store all information about API and help us with assertHttpResponseForOperation * * @return \Ovr\Swagger\SwaggerWrapper */ protected function getSwaggerWrapper() { return new \Ovr\Swagger\SwaggerWrapper( \Swagger\scan( // Path to your API __DIR__ . '/../../examples/api' ) ); } }
集成测试
集成测试 - 当你通过真实传输执行 Request
时,支持以下
- GuzzleTrait - Guzzle
常见问题解答(FAQ)
- 问题:这个库可以验证我的 Swagger 定义吗?
- 答案:不可以。这个库可以验证您的 API 请求和响应是否与 Swagger 定义匹配。
- 问题:支持哪些内容类型?
- 答案:目前支持 JSON,如果需要 XML 或其他类型,请在问题中提出。
许可证
本项目是开源软件,遵循 MIT 许可证。
有关更多信息,请参阅 LICENSE 文件。