CustomNormalizerTest.php 1.8 KB

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