CustomNormalizerTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Symfony\Tests\Component\Serializer\Normalizer;
  3. require_once __DIR__.'/../Fixtures/ScalarDummy.php';
  4. use Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy;
  5. use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
  6. use Symfony\Component\Serializer\Serializer;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien@symfony.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function setUp()
  18. {
  19. $this->normalizer = new CustomNormalizer;
  20. $this->normalizer->setSerializer(new Serializer);
  21. }
  22. public function testSerialize()
  23. {
  24. $obj = new ScalarDummy;
  25. $obj->foo = 'foo';
  26. $obj->xmlFoo = 'xml';
  27. $this->assertEquals('foo', $this->normalizer->normalize($obj, 'json'));
  28. $this->assertEquals('xml', $this->normalizer->normalize($obj, 'xml'));
  29. }
  30. public function testDeserialize()
  31. {
  32. $obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy), 'xml');
  33. $this->assertEquals('foo', $obj->xmlFoo);
  34. $this->assertNull($obj->foo);
  35. $obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy), 'json');
  36. $this->assertEquals('foo', $obj->foo);
  37. $this->assertNull($obj->xmlFoo);
  38. }
  39. public function testSupportsNormalization()
  40. {
  41. $this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy));
  42. $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass));
  43. }
  44. public function testSupportsDenormalization()
  45. {
  46. $this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy'));
  47. $this->assertFalse($this->normalizer->supportsDenormalization(array(), 'stdClass'));
  48. }
  49. }