Finish upgrade to jms serializer 1.0

Updated deprecated MinLength assertion to Length
Updated array of object parsing
Handled deprecated calls because of using
AbstractType (not sure if it is the best way, though)
This commit is contained in:
Stefano Sala 2013-02-11 14:42:17 +01:00
parent befeeb3e3f
commit 07bb37ac76
7 changed files with 104 additions and 24 deletions

View File

@ -73,11 +73,10 @@ class JmsMetadataParser implements ParserInterface
//iterate over property metadata //iterate over property metadata
foreach ($meta->propertyMetadata as $item) { foreach ($meta->propertyMetadata as $item) {
if (!is_null($item->type)) { if (!is_null($item->type)) {
$name = isset($item->serializedName) ? $item->serializedName : $item->name; $name = isset($item->serializedName) ? $item->serializedName : $item->name;
$dataType = $this->processDataType(is_string($item->type) ? $item->type : $item->type['name']); $dataType = $this->processDataType($item);
$params[$name] = array( $params[$name] = array(
'dataType' => $dataType['normalized'], 'dataType' => $dataType['normalized'],
@ -107,21 +106,15 @@ class JmsMetadataParser implements ParserInterface
* Figure out a normalized data type (for documentation), and get a * Figure out a normalized data type (for documentation), and get a
* nested class name, if available. * nested class name, if available.
* *
* @param string $type * @param array|string $type
* @return array * @return array
*/ */
protected function processDataType($type) protected function processDataType(PropertyMetadata $item)
{ {
//could be basic type $type = is_string($item->type) ? $item->type : $item->type['name'];
if ($this->isPrimitive($type)) {
return array(
'normalized' => $type,
'class' => null
);
}
//check for a type inside something that could be treated as an array //check for a type inside something that could be treated as an array
if ($nestedType = $this->getNestedTypeInArray($type)) { if ($nestedType = $this->getNestedTypeInArray($item)) {
if ($this->isPrimitive($nestedType)) { if ($this->isPrimitive($nestedType)) {
return array( return array(
'normalized' => sprintf("array of %ss", $nestedType), 'normalized' => sprintf("array of %ss", $nestedType),
@ -137,6 +130,14 @@ class JmsMetadataParser implements ParserInterface
); );
} }
//could be basic type
if ($this->isPrimitive($type)) {
return array(
'normalized' => $type,
'class' => null
);
}
//if we got this far, it's a general class name //if we got this far, it's a general class name
$exp = explode("\\", $type); $exp = explode("\\", $type);
@ -155,15 +156,17 @@ class JmsMetadataParser implements ParserInterface
* Check the various ways JMS describes values in arrays, and * Check the various ways JMS describes values in arrays, and
* get the value type in the array * get the value type in the array
* *
* @param string $type * @param array|string $item
* @return string|null * @return string|null
*/ */
protected function getNestedTypeInArray($type) protected function getNestedTypeInArray($item)
{ {
//could be some type of array with <V>, or <K,V> if (is_array($item->type)
$regEx = "/\<([A-Za-z0-9\\\]*)(\,?\s?(.*))?\>/"; && in_array($item->type['name'], array('array'))
if (preg_match($regEx, $type, $matches)) { && isset($item->type['params'])
return (!empty($matches[3])) ? $matches[3] : $matches[1]; && 1 === count($item->type['params'])
&& isset($item->type['params'][0]['name'])) {
return $item->type['params'][0]['name'];
} }
return null; return null;

View File

@ -12,6 +12,7 @@
namespace Nelmio\ApiDocBundle\Tests\Extractor; namespace Nelmio\ApiDocBundle\Tests\Extractor;
use Nelmio\ApiDocBundle\Tests\WebTestCase; use Nelmio\ApiDocBundle\Tests\WebTestCase;
use Symfony\Component\Form\Test\DeprecationErrorHandler;
class ApiDocExtractorTest extends WebTestCase class ApiDocExtractorTest extends WebTestCase
{ {
@ -19,7 +20,9 @@ class ApiDocExtractorTest extends WebTestCase
{ {
$container = $this->getContainer(); $container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle'));
$data = $extractor->all(); $data = $extractor->all();
restore_error_handler();
$this->assertTrue(is_array($data)); $this->assertTrue(is_array($data));
$this->assertCount(13, $data); $this->assertCount(13, $data);

View File

@ -16,7 +16,7 @@ use Symfony\Component\Validator\Constraints as Assert;
class Test class Test
{ {
/** /**
* @Assert\MinLength("foo"); * @Assert\Length(min="foo");
* @Assert\NotBlank * @Assert\NotBlank
*/ */
public $a; public $a;

View File

@ -30,13 +30,9 @@ services:
#JMS Serializer config for testing JmsMetadataParser #JMS Serializer config for testing JmsMetadataParser
jms_serializer: jms_serializer:
handlers: handlers:
object_based: false
datetime: datetime:
format: "Y-m-dTH:i:s" # ISO8601 default_format: "Y-m-dTH:i:s" # ISO8601
default_timezone: "UTC" # defaults to whatever timezone set in php.ini or via date_default_timezone_set default_timezone: "UTC" # defaults to whatever timezone set in php.ini or via date_default_timezone_set
array_collection: true
form_error: true
constraint_violation: true
property_naming: property_naming:
separator: _ separator: _

View File

@ -20,7 +20,9 @@ class MarkdownFormatterTest extends WebTestCase
$container = $this->getContainer(); $container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle'));
$data = $extractor->all(); $data = $extractor->all();
restore_error_handler();
$result = $container->get('nelmio_api_doc.formatter.markdown_formatter')->format($data); $result = $container->get('nelmio_api_doc.formatter.markdown_formatter')->format($data);
$expected = <<<MARKDOWN $expected = <<<MARKDOWN

View File

@ -20,7 +20,9 @@ class SimpleFormatterTest extends WebTestCase
$container = $this->getContainer(); $container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle'));
$data = $extractor->all(); $data = $extractor->all();
restore_error_handler();
$result = $container->get('nelmio_api_doc.formatter.simple_formatter')->format($data); $result = $container->get('nelmio_api_doc.formatter.simple_formatter')->format($data);
$expected = array( $expected = array(

View File

@ -0,0 +1,74 @@
<?php
namespace NelmioApiDocBundle\Tests\Parser;
use Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested;
use Nelmio\ApiDocBundle\Parser\JmsMetadataParser;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
{
public function testParserWithNestedType()
{
$metadataFactory = $this->getMock('Metadata\MetadataFactoryInterface');
$docCommentExtractor = $this->getMockBuilder('Nelmio\ApiDocBundle\Util\DocCommentExtractor')
->disableOriginalConstructor()
->getMock();
$propertyMetadataFoo = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'foo');
$propertyMetadataFoo->type = 'DateTime';
$propertyMetadataBar = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'bar');
$propertyMetadataBar->type = array(
'name' => 'string'
);
$propertyMetadataBaz = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'baz');
$propertyMetadataBaz->type = array(
'name' => 'array',
'params' => array(
array(
'name' => 'integer',
'params' => array()
)
)
);
$metadata = new ClassMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested');
$metadata->addPropertyMetadata($propertyMetadataFoo);
$metadata->addPropertyMetadata($propertyMetadataBar);
$metadata->addPropertyMetadata($propertyMetadataBaz);
$input = new JmsNested();
$metadataFactory->expects($this->once())
->method('getMetadataForClass')
->with($input)
->will($this->returnValue($metadata));
$jmsMetadataParser = new JmsMetadataParser($metadataFactory, $docCommentExtractor);
$output = $jmsMetadataParser->parse($input);
$this->assertEquals(array(
'foo' => array(
'dataType' => 'DateTime',
'required' => false,
'description' => 'No description.',
'readonly' => false
),
'bar' => array(
'dataType' => 'string',
'required' => false,
'description' => 'No description.',
'readonly' => false
),
'baz' => array(
'dataType' => 'array of integers',
'required' => false,
'description' => 'No description.',
'readonly' => false
)
), $output);
}
}