Added new tests for Swagger doc controllers. Also some CS fixes.

This commit is contained in:
Bez Hermoso 2014-06-28 00:20:12 +00:00
parent ee0496af65
commit bb723bdb40
9 changed files with 156 additions and 16 deletions

View File

@ -29,8 +29,13 @@ class ApiDocController extends Controller
public function swaggerAction(Request $request, $resource = null)
{
$docs = $this->get('nelmio_api_doc.extractor.api_doc_extractor')->all();
$formatter = $this->get('nelmio_api_doc.formatter.swagger_formatter');
$formatter = $this->get('nelmio_api_doc.formatter.request_aware_swagger_formatter');
$spec = $formatter->format($docs, $resource ? '/' . $resource : null);
if (count($spec['apis']) === 0) {
throw $this->createNotFoundException(sprintf('Cannot find resource "%s"', $resource));
}
return new JsonResponse($spec);
}
}

View File

@ -39,5 +39,12 @@ class SwaggerConfigCompilerPass implements CompilerPassInterface
$formatter->addMethodCall('setSwaggerVersion', array($container->getParameter('nelmio_api_doc.swagger.swagger_version')));
$formatter->addMethodCall('setInfo', array($container->getParameter('nelmio_api_doc.swagger.info')));
$formatter = $container->getDefinition('nelmio_api_doc.formatter.request_aware_swagger_formatter');
$formatter->addMethodCall('setBasePath', array($container->getParameter('nelmio_api_doc.swagger.base_path')));
$formatter->addMethodCall('setApiVersion', array($container->getParameter('nelmio_api_doc.swagger.api_version')));
$formatter->addMethodCall('setSwaggerVersion', array($container->getParameter('nelmio_api_doc.swagger.swagger_version')));
$formatter->addMethodCall('setInfo', array($container->getParameter('nelmio_api_doc.swagger.info')));
}
}

View File

@ -485,8 +485,8 @@ class ApiDocExtractor
/**
* Creates a human-readable version of the `actualType`. `subType` is taken into account.
*
* @param string $actualType
* @param string $subType
* @param string $actualType
* @param string $subType
* @return string
*/
protected function generateHumanReadableType($actualType, $subType)
@ -509,6 +509,7 @@ class ApiDocExtractor
if (class_exists($subType)) {
$parts = explode('\\', $subType);
return sprintf('array of objects (%s)', end($parts));
}

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Formatter;
use Symfony\Component\HttpFoundation\Request;
/**
* Extends SwaggerFormatter which takes into account the request's base URL when generating the documents for direct swagger-ui consumption.
*
* @author Bezalel Hermoso <bezalelhermoso@gmail.com>
*/
class RequestAwareSwaggerFormatter extends SwaggerFormatter
{
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* @param array $collection
* @param string $resource
* @return array
*/
protected function produceApiDeclaration(array $collection, $resource)
{
$data = parent::produceApiDeclaration($collection, $resource);
$data['basePath'] = $this->request->getBaseUrl() . $data['basePath'];
return $data;
}
}

View File

@ -14,6 +14,9 @@ namespace Nelmio\ApiDocBundle\Formatter;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Nelmio\ApiDocBundle\DataTypes;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\RouterInterface;
@ -54,6 +57,11 @@ class SwaggerFormatter implements FormatterInterface
DataTypes::DATETIME => 'date-time',
);
/**
* @var Request
*/
protected $request;
/**
* Format a collection of documentation data.
*
@ -144,7 +152,7 @@ class SwaggerFormatter implements FormatterInterface
* @param string $resource
* @return array
*/
private function produceApiDeclaration(array $collection, $resource)
protected function produceApiDeclaration(array $collection, $resource)
{
$apiDeclaration = array(

View File

@ -9,6 +9,7 @@
<parameter key="nelmio_api_doc.formatter.simple_formatter.class">Nelmio\ApiDocBundle\Formatter\SimpleFormatter</parameter>
<parameter key="nelmio_api_doc.formatter.html_formatter.class">Nelmio\ApiDocBundle\Formatter\HtmlFormatter</parameter>
<parameter key="nelmio_api_doc.formatter.swagger_formatter.class">Nelmio\ApiDocBundle\Formatter\SwaggerFormatter</parameter>
<parameter key="nelmio_api_doc.formatter.request_aware_swagger_formatter.class">Nelmio\ApiDocBundle\Formatter\RequestAwareSwaggerFormatter</parameter>
<parameter key="nelmio_api_doc.sandbox.authentication">null</parameter>
</parameters>
@ -58,6 +59,9 @@
</call>
</service>
<service id="nelmio_api_doc.formatter.swagger_formatter" class="%nelmio_api_doc.formatter.swagger_formatter.class%" />
<service id="nelmio_api_doc.formatter.request_aware_swagger_formatter" class="%nelmio_api_doc.formatter.request_aware_swagger_formatter.class%" scope="request">
<argument type="service" id="request" />
</service>
</services>
</container>

View File

@ -0,0 +1,66 @@
<?php
/**
* Created by PhpStorm.
* User: bezalelhermoso
* Date: 6/27/14
* Time: 4:35 PM
*/
namespace NelmioApiDocBundle\Tests\Controller;
use Nelmio\ApiDocBundle\Tests\WebTestCase;
/**
* Class ApiDocControllerTest
*
* @package NelmioApiDocBundle\Tests\Controller
* @author Bez Hermoso <bez@activelamp.com>
*/
class ApiDocControllerTest extends WebTestCase
{
public function testSwaggerDocResourceListRoute()
{
$client = static::createClient();
$client->request('GET', '/api-docs/');
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->headers->get('Content-type'));
}
public function dataTestApiDeclarations()
{
return array(
array('resources'),
array('tests'),
array('tests2'),
array('TestResource'),
);
}
/**
* @dataProvider dataTestApiDeclarations
*/
public function testApiDeclarationRoutes($resource)
{
$client = static::createClient();
$client->request('GET', '/api-docs/' . $resource);
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->headers->get('Content-type'));
}
public function testNonExistingApiDeclaration()
{
$client = static::createClient();
$client->request('GET', '/api-docs/santa');
$response = $client->getResponse();
$this->assertEquals(404, $response->getStatusCode());
}
}

View File

@ -1,21 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: bezalelhermoso
* Date: 6/20/14
* Time: 3:21 PM
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Controller;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
/**
* Class ResourceController
*
* @package Nelmio\ApiDocBundle\Tests\Fixtures\Controller
* @author Bez Hermoso <bez@activelamp.com>
*/
class ResourceController
{
/**

View File

@ -203,3 +203,7 @@ test_route_update_another_resource:
requirements:
_method: PUT|PATCH
_format: json|xml|html
swagger_doc:
resource: @NelmioApiDocBundle/Resources/config/swagger_routing.yml
prefix: /api-docs