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;
|
|
|
|
|
|
|
|
use Nelmio\ApiDocBundle\Tests\WebTestCase;
|
2012-04-13 14:11:54 +02:00
|
|
|
|
|
|
|
class ApiDocExtractorTest extends WebTestCase
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2012-04-13 14:42:28 +02:00
|
|
|
$this->assertTrue(is_array($data));
|
2013-03-21 12:15:44 +01:00
|
|
|
$this->assertCount(16, $data);
|
2012-04-13 14:42:28 +02:00
|
|
|
|
|
|
|
foreach ($data as $d) {
|
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$a1 = $data[0]['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
|
|
|
|
2012-04-14 10:11:52 +02:00
|
|
|
$a1 = $data[1]['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
|
|
|
|
|
|
|
$a2 = $data[2]['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
|
|
|
|
|
|
|
$a2 = $data[3]['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
|
|
|
|
2013-03-21 12:15:44 +01:00
|
|
|
$a3 = $data['12']['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
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$annotation2 = $extractor->get('nemlio.test.controller:indexAction', 'test_service_route_1');
|
|
|
|
$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
|
|
|
|
|
|
|
$data = $extractor->get('nemlio.test.controller:indexAction', 'invalid_route');
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$data = $extractor->get('nemlio.test.controller', 'test_service_route_1');
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$data = $extractor->get('nemlio.test.controller:anotherAction', 'test_service_route_1');
|
|
|
|
|
|
|
|
$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
|
|
|
);
|
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()
|
|
|
|
);
|
|
|
|
}
|
2012-04-13 14:11:54 +02:00
|
|
|
}
|