NelmioApiDocBundle/Tests/Formatter/SimpleFormatterTest.php

364 lines
14 KiB
PHP
Raw Normal View History

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\Formatter;
use Nelmio\ApiDocBundle\Tests\WebTestCase;
2012-04-13 14:11:54 +02:00
class SimpleFormatterTest extends WebTestCase
{
public function testFormat()
{
$container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
2012-04-13 15:00:46 +02:00
$data = $extractor->all();
$result = $container->get('nelmio_api_doc.formatter.simple_formatter')->format($data);
2012-04-13 14:11:54 +02:00
$expected = array(
'/tests' =>
array(
0 =>
2012-04-13 15:00:46 +02:00
array(
'method' => 'GET',
'uri' => '/tests.{_format}',
'filters' =>
array(
'a' =>
array(
'dataType' => 'integer',
),
'b' =>
array(
'dataType' => 'string',
'arbitrary' =>
array(
0 => 'arg1',
1 => 'arg2',
),
),
),
'description' => 'index action',
'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''),
),
),
1 =>
2012-04-13 14:11:54 +02:00
array(
'method' => 'GET',
'uri' => '/tests.{_format}',
'filters' =>
array(
'a' =>
array(
2012-04-13 14:11:54 +02:00
'dataType' => 'integer',
),
'b' =>
array(
2012-04-13 14:11:54 +02:00
'dataType' => 'string',
'arbitrary' =>
array(
0 => 'arg1',
1 => 'arg2',
2012-04-13 14:11:54 +02:00
),
),
),
'description' => 'index action',
'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''),
),
2012-04-13 14:11:54 +02:00
),
2 =>
array(
'method' => 'POST',
'uri' => '/tests.{_format}',
'parameters' =>
array(
'a' =>
array(
'dataType' => 'string',
'required' => true,
'description' => 'A nice description',
2012-07-24 14:39:43 -04:00
'readonly' => false
),
'b' =>
array(
'dataType' => 'string',
2012-06-20 23:19:34 +02:00
'required' => false,
'description' => '',
2012-07-24 14:39:43 -04:00
'readonly' => false
),
'c' =>
array(
'dataType' => 'boolean',
'required' => true,
'description' => '',
2012-07-24 14:39:43 -04:00
'readonly' => false
),
),
'description' => 'create test',
'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''),
),
),
3 =>
2012-04-13 14:11:54 +02:00
array(
'method' => 'POST',
'uri' => '/tests.{_format}',
'parameters' =>
array(
'a' =>
array(
2012-04-13 14:11:54 +02:00
'dataType' => 'string',
'required' => true,
2012-04-13 14:20:12 +02:00
'description' => 'A nice description',
2012-07-24 14:39:43 -04:00
'readonly' => false
2012-04-13 14:11:54 +02:00
),
'b' =>
array(
2012-04-13 14:11:54 +02:00
'dataType' => 'string',
2012-06-20 23:19:34 +02:00
'required' => false,
2012-04-13 14:20:12 +02:00
'description' => '',
2012-07-24 14:39:43 -04:00
'readonly' => false
2012-04-13 14:11:54 +02:00
),
'c' =>
array(
'dataType' => 'boolean',
'required' => true,
'description' => '',
2012-07-24 14:39:43 -04:00
'readonly' => false
),
2012-04-13 14:11:54 +02:00
),
'description' => 'create test',
'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''),
),
2012-04-13 14:11:54 +02:00
),
),
'others' =>
array(
0 =>
array(
'method' => 'POST',
'uri' => '/another-post',
'parameters' =>
array(
'a' =>
array(
'dataType' => 'string',
'required' => true,
'description' => 'A nice description',
2012-07-24 14:39:43 -04:00
'readonly' => false
),
),
'description' => 'create another test',
),
1 =>
array(
'method' => 'ANY',
'uri' => '/any',
'description' => 'Action without HTTP verb',
),
2 =>
array(
'method' => 'ANY',
'uri' => '/any/{foo}',
'requirements' =>
array(
2012-07-18 12:46:22 +02:00
'foo' => array('type' => '', 'description' => '', 'requirement' => ''),
),
'description' => 'Action without HTTP verb',
),
3 =>
array(
'method' => 'POST',
'uri' => '/jms-input-test',
'parameters' =>
array(
'foo' =>
array(
'dataType' => 'string',
'required' => false,
'description' => 'No description.',
'readonly' => false
),
'bar' =>
array(
'dataType' => 'DateTime',
'required' => false,
'description' => 'No description.',
'readonly' => true
),
'number' =>
array(
'dataType' => 'double',
'required' => false,
'description' => 'No description.',
'readonly' => false
),
'arr' =>
array(
'dataType' => 'array',
'required' => false,
'description' => 'No description.',
'readonly' => false
),
'nested' => array(
'dataType' => 'object (JmsNested)',
'required' => false,
'description' => 'No description.',
'readonly' => false,
'children' => array(
'foo' => array(
'dataType' => 'DateTime',
'required' => false,
'description' => 'No description.',
'readonly' => true,
),
'bar' => array(
'dataType' => 'string',
'required' => false,
'description' => 'No description.',
'readonly' => false,
),
'baz' => array(
'dataType' => 'array of integers',
'required' => false,
'description' => 'Epic description.
With multiple lines.',
'readonly' => false,
)
)
),
'nestedArray' => array(
'dataType' => 'array of objects (JmsNested)',
'required' => false,
'description' => 'No description.',
'readonly' => false,
'children' => array(
'foo' => array(
'dataType' => 'DateTime',
'required' => false,
'description' => 'No description.',
'readonly' => true,
),
'bar' => array(
'dataType' => 'string',
'required' => false,
'description' => 'No description.',
'readonly' => false,
),
'baz' => array(
'dataType' => 'array of integers',
'required' => false,
'description' => 'Epic description.
With multiple lines.',
'readonly' => false,
)
)
),
),
'description' => 'Testing JMS'
),
4 =>
2012-08-27 12:56:19 -04:00
array(
'method' => 'GET',
'uri' => '/jms-return-test',
'description' => 'Testing return',
'response' => array(
2012-08-27 13:25:03 -04:00
'a' => array(
'dataType' => 'string',
'required' => true,
'description' => 'A nice description',
'readonly' => false
)
2012-08-27 12:56:19 -04:00
)
),
5 =>
2012-04-19 17:05:59 +02:00
array(
'method' => 'ANY',
'uri' => '/my-commented/{id}/{page}',
2012-04-19 17:05:59 +02:00
'requirements' =>
array(
2012-07-18 12:46:22 +02:00
'id' => array('type' => 'int', 'description' => 'A nice comment', 'requirement' => ''),
'page' => array('type' => 'int', 'description' => '', 'requirement' => ''),
2012-04-19 17:05:59 +02:00
),
2012-07-13 16:04:29 +02:00
'description' => 'This method is useful to test if the getDocComment works.',
2012-07-13 14:07:40 +01:00
'documentation' => "This method is useful to test if the getDocComment works.\nAnd, it supports multilines until the first '@' char."
2012-04-19 17:05:59 +02:00
),
2012-08-27 12:56:19 -04:00
6 =>
array(
'method' => 'ANY',
'uri' => '/yet-another/{id}',
'requirements' =>
array(
2012-07-18 12:46:22 +02:00
'id' => array('type' => '', 'description' => '', 'requirement' => '\d+')
),
),
2012-08-27 12:56:19 -04:00
7 =>
array(
'method' => 'GET',
'uri' => '/z-action-with-query-param',
'filters' =>
array(
'page' => array('description' => 'Page of the overview.', 'requirement' => '\d+')
),
),
8 =>
array(
'method' => 'POST',
'uri' => '/z-action-with-request-param',
'parameters' =>
array(
'param1' => array('description' => 'Param1 description.', 'required' => true, 'dataType' => 'string', 'readonly' => false)
),
),
),
2012-04-13 14:11:54 +02:00
);
$this->assertEquals($expected, $result);
}
2012-04-13 15:00:46 +02:00
public function testFormatOne()
{
$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');
$result = $container->get('nelmio_api_doc.formatter.simple_formatter')->formatOne($annotation);
2012-04-13 15:00:46 +02:00
$expected = array(
'method' => 'GET',
'uri' => '/tests.{_format}',
2012-04-13 15:00:46 +02:00
'filters' => array(
'a' => array(
'dataType' => 'integer',
),
'b' => array(
'dataType' => 'string',
'arbitrary' => array(
'arg1',
'arg2',
),
),
),
'description' => 'index action',
'requirements' => array(
'_format' => array('type' => '', 'description' => '', 'requirement' => ''),
),
2012-04-13 15:00:46 +02:00
);
$this->assertEquals($expected, $result);
}
2012-04-13 14:11:54 +02:00
}