CustomNormalizerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Normalizer;
  11. use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
  12. use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
  13. use Symfony\Component\Serializer\Serializer;
  14. class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var CustomNormalizer
  18. */
  19. private $normalizer;
  20. protected function setUp()
  21. {
  22. $this->normalizer = new CustomNormalizer();
  23. $this->normalizer->setSerializer(new Serializer());
  24. }
  25. public function testInterface()
  26. {
  27. $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer);
  28. $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer);
  29. $this->assertInstanceOf('Symfony\Component\Serializer\SerializerAwareInterface', $this->normalizer);
  30. }
  31. public function testSerialize()
  32. {
  33. $obj = new ScalarDummy();
  34. $obj->foo = 'foo';
  35. $obj->xmlFoo = 'xml';
  36. $this->assertEquals('foo', $this->normalizer->normalize($obj, 'json'));
  37. $this->assertEquals('xml', $this->normalizer->normalize($obj, 'xml'));
  38. }
  39. public function testDeserialize()
  40. {
  41. $obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'xml');
  42. $this->assertEquals('foo', $obj->xmlFoo);
  43. $this->assertNull($obj->foo);
  44. $obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'json');
  45. $this->assertEquals('foo', $obj->foo);
  46. $this->assertNull($obj->xmlFoo);
  47. }
  48. public function testSupportsNormalization()
  49. {
  50. $this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy()));
  51. $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  52. }
  53. public function testSupportsDenormalization()
  54. {
  55. $this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy'));
  56. $this->assertFalse($this->normalizer->supportsDenormalization(array(), 'stdClass'));
  57. $this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\DenormalizableDummy'));
  58. }
  59. }