macpaw/behat-nelmio-describer

Bundle for adding sample responses behat test nelmio to api doc

Installs: 100 464

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 1

Type:symfony-bundle

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

This package is auto-updated.

Last update: 2024-11-04 11:53:47 UTC


README

Installation

Step 1: Install Bundle

Open a command console, enter your project directory and execute:

$ composer require macpaw/behat-nelmio-describer

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

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

        // ...
    }

    // ...
}

Step 3: Create Behat Nelmio Describer Config:

config/packages/behat_nelmio_describer.yaml

Configurating behat nelmio describer

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

Step 4: Add annotation to controller [OPTIONAL]

<?php

use BehatNelmioDescriber\Attributes\BehatFeaturesPath;

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

Step 5: Add annotation to route

<?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() {
        // ...
    }
}

For each anchor path from config, path from BehatFeaturesPath annotation (optional) and path/filename from BehatFeature annotation are concatenated to find the right feature file.

Additionally, each BehatFeature annotation represents folder in api doc which contains all sample responses defined by anchors.

An example of usage

If your feature file is located in src/tests/Behat/Features/api/version/route/example.feature

Configuration

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

Used in Controller:

<?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(
        // ...
    ) {
        // ...
    }
}

Update your feature file:

Contains following snippets:

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

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

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

Now in Api Doc see response payload your test: Screenshot 2022-10-26 at 12 35 44