AbstractNormalizerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Symfony\Component\Serializer\Tests\Normalizer;
  3. use Symfony\Component\Serializer\Mapping\AttributeMetadata;
  4. use Symfony\Component\Serializer\Mapping\ClassMetadata;
  5. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  6. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  7. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  8. use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
  9. use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
  10. /**
  11. * Provides a dummy Normalizer which extends the AbstractNormalizer.
  12. *
  13. * @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
  14. */
  15. class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var AbstractNormalizerDummy
  19. */
  20. private $normalizer;
  21. /**
  22. * @var ClassMetadataFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $classMetadata;
  25. protected function setUp()
  26. {
  27. $loader = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\Loader\LoaderChain')->setConstructorArgs(array(array()))->getMock();
  28. $this->classMetadata = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory')->setConstructorArgs(array($loader))->getMock();
  29. $this->normalizer = new AbstractNormalizerDummy($this->classMetadata);
  30. }
  31. public function testGetAllowedAttributesAsString()
  32. {
  33. $classMetadata = new ClassMetadata('c');
  34. $a1 = new AttributeMetadata('a1');
  35. $classMetadata->addAttributeMetadata($a1);
  36. $a2 = new AttributeMetadata('a2');
  37. $a2->addGroup('test');
  38. $classMetadata->addAttributeMetadata($a2);
  39. $a3 = new AttributeMetadata('a3');
  40. $a3->addGroup('other');
  41. $classMetadata->addAttributeMetadata($a3);
  42. $a4 = new AttributeMetadata('a4');
  43. $a4->addGroup('test');
  44. $a4->addGroup('other');
  45. $classMetadata->addAttributeMetadata($a4);
  46. $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
  47. $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), true);
  48. $this->assertEquals(array('a2', 'a4'), $result);
  49. $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), true);
  50. $this->assertEquals(array('a3', 'a4'), $result);
  51. }
  52. public function testGetAllowedAttributesAsObjects()
  53. {
  54. $classMetadata = new ClassMetadata('c');
  55. $a1 = new AttributeMetadata('a1');
  56. $classMetadata->addAttributeMetadata($a1);
  57. $a2 = new AttributeMetadata('a2');
  58. $a2->addGroup('test');
  59. $classMetadata->addAttributeMetadata($a2);
  60. $a3 = new AttributeMetadata('a3');
  61. $a3->addGroup('other');
  62. $classMetadata->addAttributeMetadata($a3);
  63. $a4 = new AttributeMetadata('a4');
  64. $a4->addGroup('test');
  65. $a4->addGroup('other');
  66. $classMetadata->addAttributeMetadata($a4);
  67. $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
  68. $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
  69. $this->assertEquals(array($a2, $a4), $result);
  70. $result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
  71. $this->assertEquals(array($a3, $a4), $result);
  72. }
  73. public function testObjectToPopulateWithProxy()
  74. {
  75. $proxyDummy = new ProxyDummy();
  76. $context = array(AbstractNormalizer::OBJECT_TO_POPULATE => $proxyDummy);
  77. $normalizer = new ObjectNormalizer();
  78. $normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);
  79. $this->assertSame('bar', $proxyDummy->getFoo());
  80. }
  81. }