interpalsnet/swagger-assert-helper

此包最新版本(1.1.8)没有可用的许可证信息。

该包的官方仓库似乎已不存在,因此该包已被冻结。

1.1.8 2019-04-19 09:55 UTC

README

Build Status

这个库支持以下功能

  • 通过 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() {}
}

更多定义示例,您可以在以下找到

  • 示例/API - PHP 注释的小型 HTTP REST 服务示例
  • 示例/GitHub - 使用 PHP 注释编写的 api.github.com 定义示例

2. 为您的控制器编写测试

功能测试

功能测试 - 当您在服务(PHP 代码)内部执行 Request 时,有以下支持

示例

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 时,有以下支持

常见问题解答

Q: 这个库可以验证我的 Swagger 定义吗?
A: 不可以。这个库验证您的 API 请求和响应是否符合 Swagger 定义。
Q: 支持哪些内容类型?
A: 目前支持 JSON,如果您在问题中提出需要 XML 或其他内容类型,我可以为您实现。

许可证

本项目是开源软件,许可协议为 MIT 许可。

有关更多信息,请参阅 LICENSE 文件。