Added more tests for the Extractor

This commit is contained in:
William DURAND 2012-04-13 14:42:28 +02:00
parent 9eedb8e643
commit 2c1e100094
4 changed files with 82 additions and 2 deletions

View File

@ -114,15 +114,21 @@ class ApiDocExtractor
public function get($controller, $route)
{
if (!preg_match('#(.+)::([\w]+)#', $controller, $matches)) {
return;
return null;
}
$method = new \ReflectionMethod($matches[1], $matches[2]);
try {
$method = new \ReflectionMethod($matches[1], $matches[2]);
} catch (\ReflectionException $e) {
return null;
}
if ($annot = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) {
if ($route = $this->router->getRouteCollection()->get($route)) {
return array('annotation' => $annot, 'route' => $route);
}
}
return null;
}
}

View File

@ -21,7 +21,31 @@ class ApiDocExtractorTest extends WebTestCase
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$data = $extractor->all();
$this->assertTrue(is_array($data));
$this->assertCount(2, $data);
foreach ($data as $d) {
$this->assertTrue(is_array($d));
$this->assertArrayHasKey('annotation', $d);
$this->assertArrayHasKey('route', $d);
$this->assertArrayHasKey('resource', $d);
$this->assertInstanceOf('Nelmio\ApiDocBundle\Annotation\ApiDoc', $d['annotation']);
$this->assertInstanceOf('Symfony\Component\Routing\Route', $d['route']);
$this->assertNotNull($d['resource']);
}
$a1 = $data[0]['annotation'];
$this->assertTrue($a1->isResource());
$this->assertEquals('index action', $a1->getDescription());
$this->assertTrue(is_array($a1->getFilters()));
$this->assertNull($a1->getFormType());
$a2 = $data[1]['annotation'];
$this->assertFalse($a2->isResource());
$this->assertEquals('create test', $a2->getDescription());
$this->assertTrue(is_array($a2->getFilters()));
$this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getFormType());
}
public function testGet()
@ -32,5 +56,47 @@ class ApiDocExtractorTest extends WebTestCase
$this->assertTrue(isset($data['route']));
$this->assertTrue(isset($data['annotation']));
$a = $data['annotation'];
$this->assertTrue($a->isResource());
$this->assertEquals('index action', $a->getDescription());
$this->assertTrue(is_array($a->getFilters()));
$this->assertNull($a->getFormType());
}
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);
}
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);
}
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);
}
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);
}
}

View File

@ -38,4 +38,8 @@ class TestController
public function postTestAction()
{
}
public function anotherAction()
{
}
}

View File

@ -10,6 +10,10 @@ test_route_2:
requirements:
_method: POST
test_route_3:
pattern: /another
defaults: { _controller: NelmioApiDocTestBundle:Test:another }
NelmioApiDocBundle:
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /