2015-01-04 13:47:41 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Serializer\Tests\Normalizer ;
use Doctrine\Common\Annotations\AnnotationReader ;
2016-08-17 11:41:54 +01:00
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor ;
2016-02-02 23:02:19 +00:00
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor ;
2016-08-17 11:41:54 +01:00
use Symfony\Component\PropertyInfo\PropertyInfoExtractor ;
2015-04-20 08:28:27 +01:00
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter ;
2016-08-17 11:41:54 +01:00
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer ;
2016-02-02 23:02:19 +00:00
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer ;
2015-01-04 13:47:41 +00:00
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
use Symfony\Component\Serializer\Serializer ;
use Symfony\Component\Serializer\SerializerInterface ;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface ;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy ;
2015-12-22 18:41:48 +00:00
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy ;
2015-01-04 13:47:41 +00:00
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder ;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader ;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory ;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy ;
/**
* @ author Kévin Dunglas < dunglas @ gmail . com >
*/
class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
{
/**
2015-11-14 12:20:58 +00:00
* @ var ObjectNormalizer
2015-01-04 13:47:41 +00:00
*/
private $normalizer ;
/**
* @ var SerializerInterface
*/
private $serializer ;
protected function setUp ()
{
2016-12-19 09:02:29 +00:00
$this -> serializer = $this -> getMockBuilder ( __NAMESPACE__ . '\ObjectSerializerNormalizer' ) -> getMock ();
2015-01-04 13:47:41 +00:00
$this -> normalizer = new ObjectNormalizer ();
$this -> normalizer -> setSerializer ( $this -> serializer );
}
public function testNormalize ()
{
$obj = new ObjectDummy ();
$object = new \stdClass ();
$obj -> setFoo ( 'foo' );
$obj -> bar = 'bar' ;
$obj -> setBaz ( true );
$obj -> setCamelCase ( 'camelcase' );
$obj -> setObject ( $object );
$this -> serializer
-> expects ( $this -> once ())
-> method ( 'normalize' )
-> with ( $object , 'any' )
-> will ( $this -> returnValue ( 'string_object' ))
;
$this -> assertEquals (
array (
'foo' => 'foo' ,
'bar' => 'bar' ,
'baz' => true ,
'fooBar' => 'foobar' ,
'camelCase' => 'camelcase' ,
'object' => 'string_object' ,
),
$this -> normalizer -> normalize ( $obj , 'any' )
);
}
public function testDenormalize ()
{
$obj = $this -> normalizer -> denormalize (
array ( 'foo' => 'foo' , 'bar' => 'bar' , 'baz' => true , 'fooBar' => 'foobar' ),
__NAMESPACE__ . '\ObjectDummy' ,
'any'
);
$this -> assertEquals ( 'foo' , $obj -> getFoo ());
$this -> assertEquals ( 'bar' , $obj -> bar );
$this -> assertTrue ( $obj -> isBaz ());
}
public function testDenormalizeWithObject ()
{
$data = new \stdClass ();
$data -> foo = 'foo' ;
$data -> bar = 'bar' ;
$data -> fooBar = 'foobar' ;
$obj = $this -> normalizer -> denormalize ( $data , __NAMESPACE__ . '\ObjectDummy' , 'any' );
$this -> assertEquals ( 'foo' , $obj -> getFoo ());
$this -> assertEquals ( 'bar' , $obj -> bar );
}
public function testDenormalizeNull ()
{
$this -> assertEquals ( new ObjectDummy (), $this -> normalizer -> denormalize ( null , __NAMESPACE__ . '\ObjectDummy' ));
}
public function testConstructorDenormalize ()
{
$obj = $this -> normalizer -> denormalize (
array ( 'foo' => 'foo' , 'bar' => 'bar' , 'baz' => true , 'fooBar' => 'foobar' ),
__NAMESPACE__ . '\ObjectConstructorDummy' , 'any' );
$this -> assertEquals ( 'foo' , $obj -> getFoo ());
$this -> assertEquals ( 'bar' , $obj -> bar );
$this -> assertTrue ( $obj -> isBaz ());
}
2015-05-21 07:43:39 +01:00
public function testConstructorDenormalizeWithNullArgument ()
{
$obj = $this -> normalizer -> denormalize (
array ( 'foo' => 'foo' , 'bar' => null , 'baz' => true ),
__NAMESPACE__ . '\ObjectConstructorDummy' , 'any' );
$this -> assertEquals ( 'foo' , $obj -> getFoo ());
$this -> assertNull ( $obj -> bar );
$this -> assertTrue ( $obj -> isBaz ());
}
2015-01-04 13:47:41 +00:00
public function testConstructorDenormalizeWithMissingOptionalArgument ()
{
$obj = $this -> normalizer -> denormalize (
array ( 'foo' => 'test' , 'baz' => array ( 1 , 2 , 3 )),
__NAMESPACE__ . '\ObjectConstructorOptionalArgsDummy' , 'any' );
$this -> assertEquals ( 'test' , $obj -> getFoo ());
$this -> assertEquals ( array (), $obj -> bar );
$this -> assertEquals ( array ( 1 , 2 , 3 ), $obj -> getBaz ());
}
2015-05-25 00:36:47 +01:00
public function testConstructorDenormalizeWithOptionalDefaultArgument ()
{
$obj = $this -> normalizer -> denormalize (
array ( 'bar' => 'test' ),
__NAMESPACE__ . '\ObjectConstructorArgsWithDefaultValueDummy' , 'any' );
$this -> assertEquals ( array (), $obj -> getFoo ());
$this -> assertEquals ( 'test' , $obj -> getBar ());
}
2015-01-04 13:47:41 +00:00
public function testConstructorWithObjectDenormalize ()
{
$data = new \stdClass ();
$data -> foo = 'foo' ;
$data -> bar = 'bar' ;
$data -> baz = true ;
$data -> fooBar = 'foobar' ;
$obj = $this -> normalizer -> denormalize ( $data , __NAMESPACE__ . '\ObjectConstructorDummy' , 'any' );
$this -> assertEquals ( 'foo' , $obj -> getFoo ());
$this -> assertEquals ( 'bar' , $obj -> bar );
}
2016-07-03 12:09:45 +01:00
public function testConstructorWithObjectTypeHintDenormalize ()
{
2016-07-03 12:55:57 +01:00
$data = array (
2016-07-03 12:09:45 +01:00
'id' => 10 ,
2016-07-03 12:55:57 +01:00
'inner' => array (
2016-07-03 12:09:45 +01:00
'foo' => 'oof' ,
'bar' => 'rab' ,
2016-07-03 12:55:57 +01:00
),
);
2016-07-03 12:09:45 +01:00
2016-07-04 21:56:52 +01:00
$normalizer = new ObjectNormalizer ();
2016-11-29 19:46:44 +00:00
$serializer = new Serializer ( array ( $normalizer ));
2016-07-04 21:56:52 +01:00
$normalizer -> setSerializer ( $serializer );
$obj = $normalizer -> denormalize ( $data , DummyWithConstructorObject :: class );
2016-07-03 12:09:45 +01:00
$this -> assertInstanceOf ( DummyWithConstructorObject :: class , $obj );
2016-07-04 21:56:52 +01:00
$this -> assertEquals ( 10 , $obj -> getId ());
2016-07-03 12:09:45 +01:00
$this -> assertInstanceOf ( ObjectInner :: class , $obj -> getInner ());
2016-07-04 21:56:52 +01:00
$this -> assertEquals ( 'oof' , $obj -> getInner () -> foo );
$this -> assertEquals ( 'rab' , $obj -> getInner () -> bar );
2016-07-03 12:09:45 +01:00
}
2016-07-06 17:52:20 +01:00
/**
* @ expectedException \Symfony\Component\Serializer\Exception\RuntimeException
* @ expectedExceptionMessage Could not determine the class of the parameter " unknown " .
*/
public function testConstructorWithUnknownObjectTypeHintDenormalize ()
{
$data = array (
'id' => 10 ,
'unknown' => array (
'foo' => 'oof' ,
'bar' => 'rab' ,
),
);
$normalizer = new ObjectNormalizer ();
2016-11-29 19:46:44 +00:00
$serializer = new Serializer ( array ( $normalizer ));
2016-07-06 17:52:20 +01:00
$normalizer -> setSerializer ( $serializer );
$normalizer -> denormalize ( $data , DummyWithConstructorInexistingObject :: class );
}
2015-01-04 13:47:41 +00:00
public function testGroupsNormalize ()
{
$classMetadataFactory = new ClassMetadataFactory ( new AnnotationLoader ( new AnnotationReader ()));
$this -> normalizer = new ObjectNormalizer ( $classMetadataFactory );
$this -> normalizer -> setSerializer ( $this -> serializer );
$obj = new GroupDummy ();
$obj -> setFoo ( 'foo' );
$obj -> setBar ( 'bar' );
$obj -> setFooBar ( 'fooBar' );
$obj -> setSymfony ( 'symfony' );
$obj -> setKevin ( 'kevin' );
$obj -> setCoopTilleuls ( 'coopTilleuls' );
$this -> assertEquals ( array (
'bar' => 'bar' ,
2016-01-18 16:42:55 +00:00
), $this -> normalizer -> normalize ( $obj , null , array ( ObjectNormalizer :: GROUPS => array ( 'c' ))));
2015-01-04 13:47:41 +00:00
$this -> assertEquals ( array (
'symfony' => 'symfony' ,
'foo' => 'foo' ,
'fooBar' => 'fooBar' ,
'bar' => 'bar' ,
'kevin' => 'kevin' ,
'coopTilleuls' => 'coopTilleuls' ,
2016-01-18 16:42:55 +00:00
), $this -> normalizer -> normalize ( $obj , null , array ( ObjectNormalizer :: GROUPS => array ( 'a' , 'c' ))));
2015-01-04 13:47:41 +00:00
}
public function testGroupsDenormalize ()
{
$classMetadataFactory = new ClassMetadataFactory ( new AnnotationLoader ( new AnnotationReader ()));
$this -> normalizer = new ObjectNormalizer ( $classMetadataFactory );
$this -> normalizer -> setSerializer ( $this -> serializer );
$obj = new GroupDummy ();
$obj -> setFoo ( 'foo' );
$toNormalize = array ( 'foo' => 'foo' , 'bar' => 'bar' );
$normalized = $this -> normalizer -> denormalize (
$toNormalize ,
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy' ,
null ,
2016-01-18 16:42:55 +00:00
array ( ObjectNormalizer :: GROUPS => array ( 'a' ))
2015-01-04 13:47:41 +00:00
);
$this -> assertEquals ( $obj , $normalized );
$obj -> setBar ( 'bar' );
$normalized = $this -> normalizer -> denormalize (
$toNormalize ,
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy' ,
null ,
2016-01-18 16:42:55 +00:00
array ( ObjectNormalizer :: GROUPS => array ( 'a' , 'b' ))
2015-01-04 13:47:41 +00:00
);
$this -> assertEquals ( $obj , $normalized );
}
2015-11-14 12:20:58 +00:00
public function testNormalizeNoPropertyInGroup ()
{
$classMetadataFactory = new ClassMetadataFactory ( new AnnotationLoader ( new AnnotationReader ()));
$this -> normalizer = new ObjectNormalizer ( $classMetadataFactory );
$this -> normalizer -> setSerializer ( $this -> serializer );
$obj = new GroupDummy ();
$obj -> setFoo ( 'foo' );
$this -> assertEquals ( array (), $this -> normalizer -> normalize ( $obj , null , array ( 'groups' => array ( 'notExist' ))));
}
2015-04-20 08:28:27 +01:00
public function testGroupsNormalizeWithNameConverter ()
{
$classMetadataFactory = new ClassMetadataFactory ( new AnnotationLoader ( new AnnotationReader ()));
$this -> normalizer = new ObjectNormalizer ( $classMetadataFactory , new CamelCaseToSnakeCaseNameConverter ());
$this -> normalizer -> setSerializer ( $this -> serializer );
$obj = new GroupDummy ();
$obj -> setFooBar ( '@dunglas' );
$obj -> setSymfony ( '@coopTilleuls' );
$obj -> setCoopTilleuls ( 'les-tilleuls.coop' );
$this -> assertEquals (
array (
'bar' => null ,
'foo_bar' => '@dunglas' ,
'symfony' => '@coopTilleuls' ,
),
2016-01-18 16:42:55 +00:00
$this -> normalizer -> normalize ( $obj , null , array ( ObjectNormalizer :: GROUPS => array ( 'name_converter' )))
2015-04-20 08:28:27 +01:00
);
}
public function testGroupsDenormalizeWithNameConverter ()
{
$classMetadataFactory = new ClassMetadataFactory ( new AnnotationLoader ( new AnnotationReader ()));
$this -> normalizer = new ObjectNormalizer ( $classMetadataFactory , new CamelCaseToSnakeCaseNameConverter ());
$this -> normalizer -> setSerializer ( $this -> serializer );
$obj = new GroupDummy ();
$obj -> setFooBar ( '@dunglas' );
$obj -> setSymfony ( '@coopTilleuls' );
$this -> assertEquals (
$obj ,
$this -> normalizer -> denormalize ( array (
'bar' => null ,
'foo_bar' => '@dunglas' ,
'symfony' => '@coopTilleuls' ,
'coop_tilleuls' => 'les-tilleuls.coop' ,
2016-01-18 16:42:55 +00:00
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy' , null , array ( ObjectNormalizer :: GROUPS => array ( 'name_converter' )))
2015-04-20 08:28:27 +01:00
);
}
2015-01-04 13:47:41 +00:00
/**
* @ dataProvider provideCallbacks
*/
public function testCallbacks ( $callbacks , $value , $result , $message )
{
$this -> normalizer -> setCallbacks ( $callbacks );
$obj = new ObjectConstructorDummy ( '' , $value , true );
$this -> assertEquals (
$result ,
$this -> normalizer -> normalize ( $obj , 'any' ),
$message
);
}
/**
* @ expectedException \InvalidArgumentException
*/
public function testUncallableCallbacks ()
{
$this -> normalizer -> setCallbacks ( array ( 'bar' => null ));
$obj = new ObjectConstructorDummy ( 'baz' , 'quux' , true );
$this -> normalizer -> normalize ( $obj , 'any' );
}
public function testIgnoredAttributes ()
{
$this -> normalizer -> setIgnoredAttributes ( array ( 'foo' , 'bar' , 'baz' , 'camelCase' , 'object' ));
$obj = new ObjectDummy ();
$obj -> setFoo ( 'foo' );
$obj -> bar = 'bar' ;
$obj -> setBaz ( true );
$this -> assertEquals (
array ( 'fooBar' => 'foobar' ),
$this -> normalizer -> normalize ( $obj , 'any' )
);
}
2016-05-22 12:47:44 +01:00
public function testIgnoredAttributesDenormalize ()
{
$this -> normalizer -> setIgnoredAttributes ( array ( 'fooBar' , 'bar' , 'baz' ));
$obj = new ObjectDummy ();
$obj -> setFoo ( 'foo' );
$this -> assertEquals (
$obj ,
$this -> normalizer -> denormalize ( array ( 'fooBar' => 'fooBar' , 'foo' => 'foo' , 'baz' => 'baz' ), __NAMESPACE__ . '\ObjectDummy' )
);
}
2015-01-04 13:47:41 +00:00
public function provideCallbacks ()
{
return array (
array (
array (
'bar' => function ( $bar ) {
2016-10-30 09:34:06 +00:00
return 'baz' ;
},
2015-01-04 13:47:41 +00:00
),
'baz' ,
array ( 'foo' => '' , 'bar' => 'baz' , 'baz' => true ),
'Change a string' ,
),
array (
array (
'bar' => function ( $bar ) {
2016-10-30 09:34:06 +00:00
return ;
},
2015-01-04 13:47:41 +00:00
),
'baz' ,
array ( 'foo' => '' , 'bar' => null , 'baz' => true ),
'Null an item' ,
),
array (
array (
'bar' => function ( $bar ) {
2016-10-30 09:34:06 +00:00
return $bar -> format ( 'd-m-Y H:i:s' );
},
2015-01-04 13:47:41 +00:00
),
new \DateTime ( '2011-09-10 06:30:00' ),
array ( 'foo' => '' , 'bar' => '10-09-2011 06:30:00' , 'baz' => true ),
'Format a date' ,
),
array (
array (
'bar' => function ( $bars ) {
2016-10-30 09:34:06 +00:00
$foos = '' ;
foreach ( $bars as $bar ) {
$foos .= $bar -> getFoo ();
}
2015-01-04 13:47:41 +00:00
2016-10-30 09:34:06 +00:00
return $foos ;
},
2015-01-04 13:47:41 +00:00
),
array ( new ObjectConstructorDummy ( 'baz' , '' , false ), new ObjectConstructorDummy ( 'quux' , '' , false )),
array ( 'foo' => '' , 'bar' => 'bazquux' , 'baz' => true ),
'Collect a property' ,
),
array (
array (
'bar' => function ( $bars ) {
2016-10-30 09:34:06 +00:00
return count ( $bars );
},
2015-01-04 13:47:41 +00:00
),
array ( new ObjectConstructorDummy ( 'baz' , '' , false ), new ObjectConstructorDummy ( 'quux' , '' , false )),
array ( 'foo' => '' , 'bar' => 2 , 'baz' => true ),
'Count a property' ,
),
);
}
/**
* @ expectedException \Symfony\Component\Serializer\Exception\LogicException
2016-02-02 23:02:19 +00:00
* @ expectedExceptionMessage Cannot normalize attribute " object " because the injected serializer is not a normalizer
2015-01-04 13:47:41 +00:00
*/
public function testUnableToNormalizeObjectAttribute ()
{
2016-12-19 09:02:29 +00:00
$serializer = $this -> getMockBuilder ( 'Symfony\Component\Serializer\SerializerInterface' ) -> getMock ();
2015-01-04 13:47:41 +00:00
$this -> normalizer -> setSerializer ( $serializer );
$obj = new ObjectDummy ();
$object = new \stdClass ();
$obj -> setObject ( $object );
$this -> normalizer -> normalize ( $obj , 'any' );
}
/**
* @ expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
*/
public function testUnableToNormalizeCircularReference ()
{
$serializer = new Serializer ( array ( $this -> normalizer ));
$this -> normalizer -> setSerializer ( $serializer );
$this -> normalizer -> setCircularReferenceLimit ( 2 );
$obj = new CircularReferenceDummy ();
$this -> normalizer -> normalize ( $obj );
}
public function testSiblingReference ()
{
$serializer = new Serializer ( array ( $this -> normalizer ));
$this -> normalizer -> setSerializer ( $serializer );
$siblingHolder = new SiblingHolder ();
$expected = array (
'sibling0' => array ( 'coopTilleuls' => 'Les-Tilleuls.coop' ),
'sibling1' => array ( 'coopTilleuls' => 'Les-Tilleuls.coop' ),
'sibling2' => array ( 'coopTilleuls' => 'Les-Tilleuls.coop' ),
);
$this -> assertEquals ( $expected , $this -> normalizer -> normalize ( $siblingHolder ));
}
public function testCircularReferenceHandler ()
{
$serializer = new Serializer ( array ( $this -> normalizer ));
$this -> normalizer -> setSerializer ( $serializer );
$this -> normalizer -> setCircularReferenceHandler ( function ( $obj ) {
return get_class ( $obj );
});
$obj = new CircularReferenceDummy ();
$expected = array ( 'me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy' );
$this -> assertEquals ( $expected , $this -> normalizer -> normalize ( $obj ));
}
public function testDenormalizeNonExistingAttribute ()
{
$this -> assertEquals (
new ObjectDummy (),
$this -> normalizer -> denormalize ( array ( 'non_existing' => true ), __NAMESPACE__ . '\ObjectDummy' )
);
}
2015-05-04 07:35:51 +01:00
public function testNoTraversableSupport ()
{
$this -> assertFalse ( $this -> normalizer -> supportsNormalization ( new \ArrayObject ()));
}
2015-11-14 11:27:50 +00:00
public function testNormalizeStatic ()
{
$this -> assertEquals ( array ( 'foo' => 'K' ), $this -> normalizer -> normalize ( new ObjectWithStaticPropertiesAndMethods ()));
}
2015-12-31 10:21:22 +00:00
public function testNormalizeNotSerializableContext ()
{
$objectDummy = new ObjectDummy ();
$expected = array (
'foo' => null ,
'baz' => null ,
'fooBar' => '' ,
'camelCase' => null ,
'object' => null ,
'bar' => null ,
);
2015-12-22 18:41:48 +00:00
$this -> assertEquals ( $expected , $this -> normalizer -> normalize ( $objectDummy , null , array ( 'not_serializable' => function () {
})));
}
public function testMaxDepth ()
{
$classMetadataFactory = new ClassMetadataFactory ( new AnnotationLoader ( new AnnotationReader ()));
$this -> normalizer = new ObjectNormalizer ( $classMetadataFactory );
$serializer = new Serializer ( array ( $this -> normalizer ));
$this -> normalizer -> setSerializer ( $serializer );
$level1 = new MaxDepthDummy ();
$level1 -> foo = 'level1' ;
$level2 = new MaxDepthDummy ();
$level2 -> foo = 'level2' ;
$level1 -> child = $level2 ;
$level3 = new MaxDepthDummy ();
$level3 -> foo = 'level3' ;
$level2 -> child = $level3 ;
$result = $serializer -> normalize ( $level1 , null , array ( ObjectNormalizer :: ENABLE_MAX_DEPTH => true ));
$expected = array (
'bar' => null ,
'foo' => 'level1' ,
'child' => array (
'bar' => null ,
'foo' => 'level2' ,
'child' => array (
'bar' => null ,
'child' => null ,
),
),
);
$this -> assertEquals ( $expected , $result );
2015-12-31 10:21:22 +00:00
}
2016-02-29 12:59:26 +00:00
/**
* @ expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testThrowUnexpectedValueException ()
{
$this -> normalizer -> denormalize ( array ( 'foo' => 'bar' ), ObjectTypeHinted :: class );
}
2016-02-02 23:02:19 +00:00
public function testDenomalizeRecursive ()
{
2016-08-17 11:41:54 +01:00
$extractor = new PropertyInfoExtractor ( array (), array ( new PhpDocExtractor (), new ReflectionExtractor ()));
$normalizer = new ObjectNormalizer ( null , null , null , $extractor );
$serializer = new Serializer ( array ( new ArrayDenormalizer (), new DateTimeNormalizer (), $normalizer ));
$obj = $serializer -> denormalize ( array (
'inner' => array ( 'foo' => 'foo' , 'bar' => 'bar' ),
'date' => '1988/01/21' ,
'inners' => array ( array ( 'foo' => 1 ), array ( 'foo' => 2 )),
), ObjectOuter :: class );
2016-02-02 23:02:19 +00:00
$this -> assertEquals ( 'foo' , $obj -> getInner () -> foo );
$this -> assertEquals ( 'bar' , $obj -> getInner () -> bar );
$this -> assertEquals ( '1988-01-21' , $obj -> getDate () -> format ( 'Y-m-d' ));
2016-08-17 11:41:54 +01:00
$this -> assertEquals ( 1 , $obj -> getInners ()[ 0 ] -> foo );
$this -> assertEquals ( 2 , $obj -> getInners ()[ 1 ] -> foo );
2016-02-02 23:02:19 +00:00
}
/**
2016-09-25 09:27:07 +01:00
* @ expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
2016-02-02 23:02:19 +00:00
* @ expectedExceptionMessage The type of the " date " attribute for class " Symfony\Component\Serializer\Tests\Normalizer\ObjectOuter " must be one of " DateTimeInterface " ( " string " given).
*/
public function testRejectInvalidType ()
{
$normalizer = new ObjectNormalizer ( null , null , null , new ReflectionExtractor ());
$serializer = new Serializer ( array ( $normalizer ));
$serializer -> denormalize ( array ( 'date' => 'foo' ), ObjectOuter :: class );
}
2016-05-30 09:20:49 +01:00
2016-08-17 11:41:54 +01:00
/**
2016-09-25 09:27:07 +01:00
* @ expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
2016-08-17 11:41:54 +01:00
* @ expectedExceptionMessage The type of the key " a " must be " int " ( " string " given ) .
*/
public function testRejectInvalidKey ()
{
$extractor = new PropertyInfoExtractor ( array (), array ( new PhpDocExtractor (), new ReflectionExtractor ()));
$normalizer = new ObjectNormalizer ( null , null , null , $extractor );
$serializer = new Serializer ( array ( new ArrayDenormalizer (), new DateTimeNormalizer (), $normalizer ));
$serializer -> denormalize ( array ( 'inners' => array ( 'a' => array ( 'foo' => 1 ))), ObjectOuter :: class );
}
2016-05-30 09:20:49 +01:00
public function testExtractAttributesRespectsFormat ()
{
$normalizer = new FormatAndContextAwareNormalizer ();
$data = new ObjectDummy ();
$data -> setFoo ( 'bar' );
$data -> bar = 'foo' ;
$this -> assertSame ( array ( 'foo' => 'bar' , 'bar' => 'foo' ), $normalizer -> normalize ( $data , 'foo_and_bar_included' ));
}
public function testExtractAttributesRespectsContext ()
{
$normalizer = new FormatAndContextAwareNormalizer ();
$data = new ObjectDummy ();
$data -> setFoo ( 'bar' );
$data -> bar = 'foo' ;
$this -> assertSame ( array ( 'foo' => 'bar' , 'bar' => 'foo' ), $normalizer -> normalize ( $data , null , array ( 'include_foo_and_bar' => true )));
}
2015-01-04 13:47:41 +00:00
}
class ObjectDummy
{
protected $foo ;
public $bar ;
private $baz ;
protected $camelCase ;
protected $object ;
public function getFoo ()
{
return $this -> foo ;
}
public function setFoo ( $foo )
{
$this -> foo = $foo ;
}
public function isBaz ()
{
return $this -> baz ;
}
public function setBaz ( $baz )
{
$this -> baz = $baz ;
}
public function getFooBar ()
{
return $this -> foo . $this -> bar ;
}
public function getCamelCase ()
{
return $this -> camelCase ;
}
public function setCamelCase ( $camelCase )
{
$this -> camelCase = $camelCase ;
}
public function otherMethod ()
{
2015-04-20 08:28:27 +01:00
throw new \RuntimeException ( 'Dummy::otherMethod() should not be called' );
2015-01-04 13:47:41 +00:00
}
public function setObject ( $object )
{
$this -> object = $object ;
}
public function getObject ()
{
return $this -> object ;
}
}
class ObjectConstructorDummy
{
protected $foo ;
public $bar ;
private $baz ;
public function __construct ( $foo , $bar , $baz )
{
$this -> foo = $foo ;
$this -> bar = $bar ;
$this -> baz = $baz ;
}
public function getFoo ()
{
return $this -> foo ;
}
public function isBaz ()
{
return $this -> baz ;
}
public function otherMethod ()
{
2015-04-20 08:28:27 +01:00
throw new \RuntimeException ( 'Dummy::otherMethod() should not be called' );
2015-01-04 13:47:41 +00:00
}
}
abstract class ObjectSerializerNormalizer implements SerializerInterface , NormalizerInterface
{
}
class ObjectConstructorOptionalArgsDummy
{
protected $foo ;
public $bar ;
private $baz ;
public function __construct ( $foo , $bar = array (), $baz = array ())
{
$this -> foo = $foo ;
$this -> bar = $bar ;
$this -> baz = $baz ;
}
public function getFoo ()
{
return $this -> foo ;
}
public function getBaz ()
{
return $this -> baz ;
}
public function otherMethod ()
{
2015-04-20 08:28:27 +01:00
throw new \RuntimeException ( 'Dummy::otherMethod() should not be called' );
2015-01-04 13:47:41 +00:00
}
}
2015-05-25 00:36:47 +01:00
class ObjectConstructorArgsWithDefaultValueDummy
{
protected $foo ;
protected $bar ;
public function __construct ( $foo = array (), $bar )
{
$this -> foo = $foo ;
$this -> bar = $bar ;
}
public function getFoo ()
{
return $this -> foo ;
}
public function getBar ()
{
return $this -> bar ;
}
public function otherMethod ()
{
throw new \RuntimeException ( 'Dummy::otherMethod() should not be called' );
}
}
2015-11-14 11:27:50 +00:00
class ObjectWithStaticPropertiesAndMethods
{
public $foo = 'K' ;
public static $bar = 'A' ;
public static function getBaz ()
{
return 'L' ;
}
}
2016-02-29 12:59:26 +00:00
class ObjectTypeHinted
{
public function setFoo ( array $f )
{
}
}
2016-02-02 23:02:19 +00:00
class ObjectOuter
{
private $inner ;
private $date ;
2016-08-17 11:41:54 +01:00
/**
* @ var ObjectInner []
*/
private $inners ;
2016-02-02 23:02:19 +00:00
public function getInner ()
{
return $this -> inner ;
}
public function setInner ( ObjectInner $inner )
{
$this -> inner = $inner ;
}
public function setDate ( \DateTimeInterface $date )
{
$this -> date = $date ;
}
public function getDate ()
{
return $this -> date ;
}
2016-08-17 11:41:54 +01:00
public function setInners ( array $inners )
{
$this -> inners = $inners ;
}
public function getInners ()
{
return $this -> inners ;
}
2016-02-02 23:02:19 +00:00
}
class ObjectInner
{
public $foo ;
public $bar ;
}
2016-05-30 09:20:49 +01:00
class FormatAndContextAwareNormalizer extends ObjectNormalizer
{
protected function isAllowedAttribute ( $classOrObject , $attribute , $format = null , array $context = array ())
{
if ( in_array ( $attribute , array ( 'foo' , 'bar' )) && 'foo_and_bar_included' === $format ) {
return true ;
}
if ( in_array ( $attribute , array ( 'foo' , 'bar' )) && isset ( $context [ 'include_foo_and_bar' ]) && true === $context [ 'include_foo_and_bar' ]) {
return true ;
}
return false ;
}
}
2016-07-03 12:09:45 +01:00
class DummyWithConstructorObject
{
private $id ;
private $inner ;
public function __construct ( $id , ObjectInner $inner )
{
$this -> id = $id ;
$this -> inner = $inner ;
}
public function getId ()
{
return $this -> id ;
}
public function getInner ()
{
return $this -> inner ;
}
}
2016-07-06 17:52:20 +01:00
class DummyWithConstructorInexistingObject
{
public function __construct ( $id , Unknown $unknown )
{
}
}