macpaw/behat-nelmio-describer

为 behat 测试添加样本响应,并将其添加到 api 文档的捆绑包

安装次数: 86,446

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放性问题: 1

类型:symfony-bundle

v2.0.1 2023-12-01 09:22 UTC

This package is auto-updated.

Last update: 2024-09-04 11:34:32 UTC


README

安装

步骤 1: 安装捆绑包

打开命令行控制台,进入您的项目目录并执行

$ composer require macpaw/behat-nelmio-describer

此命令需要您全局安装 Composer,具体请参阅 Composer 文档中的安装章节

步骤 2: 启用捆绑包

然后,通过将其添加到项目 app/AppKernel.php 文件中注册的捆绑包列表来启用捆绑包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            BehatNelmioDescriber\BehatNelmioDescriberBundle::class => ['all' => true]
        );

        // ...
    }

    // ...
}

步骤 3: 创建 Behat Nelmio Describer 配置

config/packages/behat_nelmio_describer.yaml

配置 behat nelmio describer

behat_nelmio_describer:
  behat_test_path: <path to directory with your behat features>

步骤 4: 向控制器添加注释 [可选]

<?php

use BehatNelmioDescriber\Attributes\BehatFeaturesPath;

#[BehatFeaturesPath(path: "<path to folder/file with fixtures regarding base path in config>")]
final class SomeController extends AbstractController{
    // ... 
}

步骤 5: 向路由添加注释

<?php

use BehatNelmioDescriber\Attributes\BehatFeature;
use BehatNelmioDescriber\Enum\Status;

final class SomeController extends AbstractController{
    #[BehatFeature(status: "<string name to group by>", file: '<filename or route to file regarding base path>', anchors: [
       // array of anchors    
    ])]
    public function handleRequestFunction() {
        // ...
    }
}

对于配置中的每个锚点路径、BehatFeaturesPath 注释的路径(可选)和 BehatFeature 注释的路径/文件名,将它们连接起来以找到正确的功能文件。

此外,每个 BehatFeature 注释代表一个文件夹,该文件夹包含所有由锚点定义的样本响应。

使用示例

如果您的功能文件位于 src/tests/Behat/Features/api/version/route/example.feature

配置

behat_nelmio_describer:
  behat_test_path: '%kernel.project_dir%/tests/Behat/Features'

用于控制器

<?php

namespace Some/Namespace;

use BehatNelmioDescriber\Attributes\BehatFeature;
use BehatNelmioDescriber\Attributes\BehatFeaturesPath;
use FOS\RestBundle\Controller\Annotations as Rest;
use Nelmio\ApiDocBundle\Annotation as ApiDoc;
use OpenApi\Annotations as OA;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/api/version/route', name: 'api_version_route_')]
#[BehatFeaturesPath(path: 'api/version/route/')]
final class CustomerController extends AbstractController
{
    /**
     * Title
     *
     * @Route(
     *     path="/example",
     *     name="example",
     *     defaults={"_format": "json"},
     *     methods={"GET"}
     * )
     *
     * @ApiDoc\Operation(tags={"Example"})
     */
    #[BehatFeature(status: Status::SUCCESS, file: 'example.feature', anchors: [
       'success',
       'successWithoutOptionalParams',    
    ])]
    #[BehatFeature(status: Status::FAILURE, file: 'example.feature', anchors: [
       'paramsInvalid',    
    ])]
    public function getCustomerProductPlanListAction(
        // ...
    ) {
        // ...
    }
}

更新您的功能文件

包含以下片段

#! success
"""
{
    "example": "data""
}
"""

#! successWithoutOptionalParams
"""
{
    "example": "data""
}
"""

#! paramsInvalid
"""
{
    "example": "data""
}
"""

现在在 Api Doc 中查看您的测试的响应负载: Screenshot 2022-10-26 at 12 35 44