PHP code example of ovr / swagger-assert-helper

1. Go to this page and download the library: Download ovr/swagger-assert-helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ovr / swagger-assert-helper example snippets


/**
 * @SWG\Definition(
 *  definition = "UserResponse",
 *  64"),
 *  @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",
     *      

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'
            )
        );
    }
}