2012-04-13 14:11:54 +02:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2012-04-13 14:22:07 +02:00
|
|
|
namespace Nelmio\ApiDocBundle\Tests\Extractor;
|
|
|
|
|
2014-09-29 16:18:11 +02:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
|
|
|
use Nelmio\ApiDocBundle\Extractor\ApiDocExtractor;
|
2012-04-13 14:22:07 +02:00
|
|
|
use Nelmio\ApiDocBundle\Tests\WebTestCase;
|
2012-04-13 14:11:54 +02:00
|
|
|
|
|
|
|
class ApiDocExtractorTest extends WebTestCase
|
|
|
|
{
|
2014-11-22 22:27:13 +01:00
|
|
|
const ROUTES_QUANTITY_DEFAULT = 33; // Routes in the default api
|
|
|
|
const ROUTES_QUANTITY_PREMIUM = 6; // Routes tagged with premium api
|
|
|
|
const ROUTES_QUANTITY_TEST = 2; // Routes tagged with test api
|
|
|
|
|
2012-04-13 14:11:54 +02:00
|
|
|
public function testAll()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
2013-02-15 13:33:05 +01:00
|
|
|
set_error_handler(array($this, 'handleDeprecation'));
|
2012-04-13 14:11:54 +02:00
|
|
|
$data = $extractor->all();
|
2013-02-11 14:42:17 +01:00
|
|
|
restore_error_handler();
|
2012-04-13 14:11:54 +02:00
|
|
|
|
2015-05-15 17:42:03 +02:00
|
|
|
if (class_exists('Dunglas\ApiBundle\DunglasApiBundle')) {
|
2015-03-15 23:48:57 +01:00
|
|
|
$routesQuantity = 38;
|
|
|
|
$httpsKey = 25;
|
|
|
|
} else {
|
2014-11-22 22:27:13 +01:00
|
|
|
$routesQuantity = self::ROUTES_QUANTITY_DEFAULT;
|
2015-03-15 23:48:57 +01:00
|
|
|
$httpsKey = 20;
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:42:28 +02:00
|
|
|
$this->assertTrue(is_array($data));
|
2015-03-15 23:48:57 +01:00
|
|
|
$this->assertCount($routesQuantity, $data);
|
2012-04-13 14:42:28 +02:00
|
|
|
|
2014-07-21 10:26:06 -07:00
|
|
|
$cacheFile = $container->getParameter('kernel.cache_dir') . '/api-doc.cache';
|
|
|
|
$this->assertFileExists($cacheFile);
|
|
|
|
$this->assertEquals(file_get_contents($cacheFile), serialize($data));
|
|
|
|
|
2015-03-15 23:48:57 +01:00
|
|
|
foreach ($data as $key => $d) {
|
2012-04-13 14:42:28 +02:00
|
|
|
$this->assertTrue(is_array($d));
|
|
|
|
$this->assertArrayHasKey('annotation', $d);
|
|
|
|
$this->assertArrayHasKey('resource', $d);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Nelmio\ApiDocBundle\Annotation\ApiDoc', $d['annotation']);
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertInstanceOf('Symfony\Component\Routing\Route', $d['annotation']->getRoute());
|
2012-04-13 14:42:28 +02:00
|
|
|
$this->assertNotNull($d['resource']);
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$a1 = $data[7]['annotation'];
|
2012-07-20 00:58:58 +02:00
|
|
|
$array1 = $a1->toArray();
|
2012-04-13 14:42:28 +02:00
|
|
|
$this->assertTrue($a1->isResource());
|
|
|
|
$this->assertEquals('index action', $a1->getDescription());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertTrue(is_array($array1['filters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertNull($a1->getInput());
|
2012-04-13 14:42:28 +02:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$a1 = $data[7]['annotation'];
|
2012-07-20 00:58:58 +02:00
|
|
|
$array1 = $a1->toArray();
|
2012-04-14 10:11:52 +02:00
|
|
|
$this->assertTrue($a1->isResource());
|
|
|
|
$this->assertEquals('index action', $a1->getDescription());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertTrue(is_array($array1['filters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertNull($a1->getInput());
|
2012-04-14 10:11:52 +02:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$a2 = $data[8]['annotation'];
|
2012-07-20 00:58:58 +02:00
|
|
|
$array2 = $a2->toArray();
|
2012-04-14 10:11:52 +02:00
|
|
|
$this->assertFalse($a2->isResource());
|
|
|
|
$this->assertEquals('create test', $a2->getDescription());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertFalse(isset($array2['filters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInput());
|
2012-04-14 10:11:52 +02:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$a2 = $data[9]['annotation'];
|
2012-07-20 00:58:58 +02:00
|
|
|
$array2 = $a2->toArray();
|
2012-04-13 14:42:28 +02:00
|
|
|
$this->assertFalse($a2->isResource());
|
|
|
|
$this->assertEquals('create test', $a2->getDescription());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertFalse(isset($array2['filters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInput());
|
2012-12-10 10:21:04 -08:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$a4 = $data[11]['annotation'];
|
2013-10-11 16:18:02 +02:00
|
|
|
$this->assertTrue($a4->isResource());
|
|
|
|
$this->assertEquals('TestResource', $a4->getResource());
|
|
|
|
|
2015-03-15 23:48:57 +01:00
|
|
|
$a3 = $data[$httpsKey]['annotation'];
|
2012-12-10 10:21:04 -08:00
|
|
|
$this->assertTrue($a3->getHttps());
|
2012-04-13 14:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGet()
|
|
|
|
{
|
2012-07-20 00:58:58 +02:00
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::indexAction', 'test_route_1');
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Nelmio\ApiDocBundle\Annotation\ApiDoc', $annotation);
|
2012-04-13 14:11:54 +02:00
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertTrue($annotation->isResource());
|
|
|
|
$this->assertEquals('index action', $annotation->getDescription());
|
2012-04-13 14:42:28 +02:00
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annotation->toArray();
|
|
|
|
$this->assertTrue(is_array($array['filters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertNull($annotation->getInput());
|
2012-04-14 10:11:52 +02:00
|
|
|
|
2014-03-27 19:49:59 +00:00
|
|
|
$annotation2 = $extractor->get('nelmio.test.controller:indexAction', 'test_service_route_1');
|
2012-07-20 00:58:58 +02:00
|
|
|
$annotation2->getRoute()
|
|
|
|
->setDefault('_controller', $annotation->getRoute()->getDefault('_controller'))
|
|
|
|
->compile(); // compile as we changed a default value
|
|
|
|
$this->assertEquals($annotation, $annotation2);
|
2012-04-13 14:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithBadController()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$data = $extractor->get('Undefined\Controller::indexAction', 'test_route_1');
|
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-14 10:11:52 +02:00
|
|
|
|
|
|
|
$data = $extractor->get('undefined_service:index', 'test_service_route_1');
|
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-13 14:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithBadRoute()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$data = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::indexAction', 'invalid_route');
|
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-14 10:11:52 +02:00
|
|
|
|
2014-03-27 19:49:59 +00:00
|
|
|
$data = $extractor->get('nelmio.test.controller:indexAction', 'invalid_route');
|
2012-04-14 10:11:52 +02:00
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-13 14:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithInvalidPattern()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$data = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController', 'test_route_1');
|
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-14 10:11:52 +02:00
|
|
|
|
2014-03-27 19:49:59 +00:00
|
|
|
$data = $extractor->get('nelmio.test.controller', 'test_service_route_1');
|
2012-04-14 10:11:52 +02:00
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-13 14:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithMethodWithoutApiDocAnnotation()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$data = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::anotherAction', 'test_route_3');
|
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-14 10:11:52 +02:00
|
|
|
|
2014-03-27 19:49:59 +00:00
|
|
|
$data = $extractor->get('nelmio.test.controller:anotherAction', 'test_service_route_1');
|
2012-04-14 10:11:52 +02:00
|
|
|
|
|
|
|
$this->assertNull($data);
|
2012-04-13 14:11:54 +02:00
|
|
|
}
|
2012-04-19 16:23:26 +02:00
|
|
|
|
|
|
|
public function testGetWithDocComment()
|
|
|
|
{
|
2012-07-20 00:58:58 +02:00
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::myCommentedAction', 'test_route_5');
|
2012-04-19 16:23:26 +02:00
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertNotNull($annotation);
|
2012-04-19 17:17:36 +02:00
|
|
|
$this->assertEquals(
|
2012-07-13 16:04:29 +02:00
|
|
|
"This method is useful to test if the getDocComment works.",
|
2012-07-20 00:58:58 +02:00
|
|
|
$annotation->getDescription()
|
2012-04-19 17:17:36 +02:00
|
|
|
);
|
2013-04-24 22:28:46 +02:00
|
|
|
|
2013-05-03 14:37:52 +01:00
|
|
|
$data = $annotation->toArray();
|
|
|
|
$this->assertEquals(
|
|
|
|
4,
|
|
|
|
count($data['requirements'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'The param type',
|
|
|
|
$data['requirements']['paramType']['description']
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'The param id',
|
|
|
|
$data['requirements']['param']['description']
|
|
|
|
);
|
2012-04-19 16:23:26 +02:00
|
|
|
}
|
2012-12-26 12:23:28 +01:00
|
|
|
|
|
|
|
public function testGetWithAuthentication()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::AuthenticatedAction', 'test_route_13');
|
|
|
|
|
|
|
|
$this->assertNotNull($annotation);
|
|
|
|
$this->assertTrue(
|
|
|
|
$annotation->getAuthentication()
|
|
|
|
);
|
2013-06-24 15:57:54 +02:00
|
|
|
$this->assertContains('ROLE_USER', $annotation->getAuthenticationRoles());
|
|
|
|
$this->assertContains('ROLE_FOOBAR', $annotation->getAuthenticationRoles());
|
|
|
|
$this->assertCount(2, $annotation->getAuthenticationRoles());
|
2012-12-26 12:23:28 +01:00
|
|
|
}
|
2013-03-27 16:20:42 +01:00
|
|
|
|
|
|
|
public function testGetWithCache()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
2014-08-28 00:12:34 +02:00
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zCachedAction', 'test_route_23');
|
2013-03-27 16:20:42 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($annotation);
|
|
|
|
$this->assertEquals(
|
|
|
|
60,
|
|
|
|
$annotation->getCache()
|
|
|
|
);
|
|
|
|
}
|
2013-04-30 16:21:12 +04:00
|
|
|
|
2013-04-16 19:40:29 +01:00
|
|
|
public function testGetWithDeprecated()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
2013-04-16 19:59:54 +01:00
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::DeprecatedAction', 'test_route_14');
|
2013-04-16 19:40:29 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($annotation);
|
|
|
|
$this->assertTrue(
|
|
|
|
$annotation->getDeprecated()
|
|
|
|
);
|
|
|
|
}
|
2013-12-05 00:05:47 +01:00
|
|
|
|
|
|
|
public function testOutputWithSelectedParsers()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zReturnSelectedParsersOutputAction', 'test_route_19');
|
|
|
|
|
|
|
|
$this->assertNotNull($annotation);
|
|
|
|
$output = $annotation->getOutput();
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2013-12-05 00:05:47 +01:00
|
|
|
$parsers = $output['parsers'];
|
|
|
|
$this->assertEquals(
|
|
|
|
"Nelmio\\ApiDocBundle\\Parser\\JmsMetadataParser",
|
|
|
|
$parsers[0]
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
"Nelmio\\ApiDocBundle\\Parser\\ValidationParser",
|
|
|
|
$parsers[1]
|
|
|
|
);
|
|
|
|
$this->assertCount(2, $parsers);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInputWithSelectedParsers()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zReturnSelectedParsersInputAction', 'test_route_20');
|
|
|
|
|
|
|
|
$this->assertNotNull($annotation);
|
|
|
|
$input = $annotation->getInput();
|
|
|
|
$parsers = $input['parsers'];
|
|
|
|
$this->assertEquals(
|
|
|
|
"Nelmio\\ApiDocBundle\\Parser\\FormTypeParser",
|
|
|
|
$parsers[0]
|
|
|
|
);
|
|
|
|
$this->assertCount(1, $parsers);
|
|
|
|
}
|
2014-09-29 16:18:11 +02:00
|
|
|
|
|
|
|
public function testPostRequestDoesRequireParametersWhenMarkedAsSuch()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
/** @var ApiDocExtractor $extractor */
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
/** @var ApiDoc $annotation */
|
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::requiredParametersAction', 'test_required_parameters');
|
|
|
|
|
|
|
|
$parameters = $annotation->getParameters();
|
|
|
|
$this->assertTrue($parameters['required_field']['required']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPutRequestDoesNeverRequireParameters()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
/** @var ApiDocExtractor $extractor */
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
/** @var ApiDoc $annotation */
|
|
|
|
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::requiredParametersAction', 'test_put_disables_required_parameters');
|
|
|
|
|
|
|
|
$parameters = $annotation->getParameters();
|
|
|
|
$this->assertFalse($parameters['required_field']['required']);
|
|
|
|
}
|
2014-11-22 22:27:13 +01:00
|
|
|
|
|
|
|
public function multiDocProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('default', self::ROUTES_QUANTITY_DEFAULT),
|
|
|
|
array('premium', self::ROUTES_QUANTITY_PREMIUM),
|
|
|
|
array('test', self::ROUTES_QUANTITY_TEST),
|
|
|
|
array('foobar', 0),
|
|
|
|
array("", 0),
|
|
|
|
array(null, 0),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllMultiDocsForTest()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
set_error_handler(array($this, 'handleDeprecation'));
|
|
|
|
$data = $extractor->all('test');
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($data));
|
|
|
|
$this->assertCount(self::ROUTES_QUANTITY_TEST, $data);
|
|
|
|
|
|
|
|
$a1 = $data[0]['annotation'];
|
|
|
|
$this->assertCount(3, $a1->getApis());
|
|
|
|
$this->assertEquals('List resources.', $a1->getDescription());
|
|
|
|
|
|
|
|
$a2 = $data[1]['annotation'];
|
|
|
|
$this->assertCount(2, $a2->getApis());
|
|
|
|
$this->assertEquals('create another test', $a2->getDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllMultiDocsForPremium()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
set_error_handler(array($this, 'handleDeprecation'));
|
|
|
|
$data = $extractor->all('premium');
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($data));
|
|
|
|
$this->assertCount(self::ROUTES_QUANTITY_PREMIUM, $data);
|
|
|
|
|
|
|
|
$a1 = $data[0]['annotation'];
|
|
|
|
$this->assertCount(2, $a1->getApis());
|
|
|
|
$this->assertEquals('List another resource.', $a1->getDescription());
|
|
|
|
|
|
|
|
$a2 = $data[1]['annotation'];
|
|
|
|
$this->assertCount(3, $a2->getApis());
|
|
|
|
$this->assertEquals('List resources.', $a2->getDescription());
|
|
|
|
|
|
|
|
$a3 = $data[4]['annotation'];
|
|
|
|
$this->assertCount(2, $a3->getApis());
|
|
|
|
$this->assertEquals('create test', $a3->getDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider multiDocProvider
|
|
|
|
*/
|
|
|
|
public function testAllMultiDocs($api, $count)
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
|
|
|
set_error_handler(array($this, 'handleDeprecation'));
|
|
|
|
$data = $extractor->all($api);
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($data));
|
|
|
|
$this->assertCount($count, $data);
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:11:54 +02:00
|
|
|
}
|