Skip to content

Bundle for adding sample responses behat test nelmio to api doc

License

Notifications You must be signed in to change notification settings

MacPaw/behat-nelmio-describer

Repository files navigation

Behat Nelmio Describer

Version Build Status Code Coverage
master CI Coverage Status
develop CI Coverage Status

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