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;
|
|
|
|
|
Unified data types [actualType and subType]
This is the result of https://github.com/nelmio/NelmioApiDocBundle/issues/410.
This PR aims to provide a uniform way of declaring data-types of parameters for
parsers and handlers to follow. In turn, this would allow formatters to
determine data-types in a cleaner and less volatile manner. (See use-case that
can be improved with this PR:
https://github.com/nelmio/NelmioApiDocBundle/blob/master/Formatter/AbstractFormatter.php#L103)
This is possible by the addition two properties to each property item in
`response`, and `parameters` fields in each API endpoint produced by the
`ApiDocExtractor`:
* `actualType` Contains a value from one of the `DataTypes` class constants.
* `subType` Can contain either `null`, or any other `DataTypes` class constant.
This is relevant when the `actualType` is a `DataTypes::COLLECTION`, wherein
`subType` would specify the type of the collection items. It is also relevant
when `actualType` is a `DataTypes::MODEL`, wherein `subType` would contain an
identifier of the model (the FQCN or anything the parser would wish to specify)
Examples:
```php
array(
'id' => array(
'dataType' => 'integer',
'actualType' => DataTypes::INTEGER,
'subType' => null,
),
'profile' => array(
'dataType' => 'object (Profile)',
'actualType' => DataTypes::MODEL,
'subType' => 'Foo\Entity\Profile',
'children' => array(
'name' => array(
'dataType' => 'string',
'actualType' => DataTypes::STRING,
'subType' => null,
),
'birthDate' => array(
'dataType' => 'date',
'actualType' => DataTypes::DATE,
'subType' => null,
),
)
),
'languages' => array(
'dataType' => 'array of strings',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::STRING,
),
'roles' => array(
'dataType' => 'array of choices',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::ENUM,
),
'groups' => array(
'dataType' => 'array of objects (Group)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Group',
),
'profileRevisions' => array(
'dataType' => 'array of objects (Profile)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Profile',
),
'address' => array(
'dataType' => 'object (a_type_a_custom_JMS_serializer_handler_handles)',
'actualType' => DataTypes::MODEL,
'subType' => 'a_type_a_custom_JMS_serializer_handler_handles',
),
);
```
When a formatter omits the `dataType` property or leaves it blank, it is
inferred within `ApiDocExtractor` before everything is passed to formatters.
2014-06-17 17:05:00 -07:00
|
|
|
use Nelmio\ApiDocBundle\DataTypes;
|
2012-04-13 14:22:07 +02:00
|
|
|
use Nelmio\ApiDocBundle\Tests\WebTestCase;
|
2012-04-13 14:11:54 +02:00
|
|
|
|
|
|
|
class SimpleFormatterTest extends WebTestCase
|
|
|
|
{
|
2012-04-13 15:00:46 +02:00
|
|
|
public function testFormatOne()
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$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',
|
2012-08-10 11:38:01 +02:00
|
|
|
'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',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2012-08-10 11:38:01 +02:00
|
|
|
'description' => 'index action',
|
|
|
|
'requirements' => array(
|
2012-11-17 18:05:05 +01:00
|
|
|
'_format' => array('dataType' => '', 'description' => '', 'requirement' => ''),
|
2012-08-10 11:38:01 +02:00
|
|
|
),
|
2012-12-10 10:21:04 -08:00
|
|
|
'https' => false,
|
2012-12-26 12:23:28 +01:00
|
|
|
'authentication' => false,
|
2013-06-24 14:27:22 +02:00
|
|
|
'authenticationRoles' => array(),
|
|
|
|
'deprecated' => false,
|
2012-04-13 15:00:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2012-04-13 14:11:54 +02:00
|
|
|
}
|