CustomNormalizerTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. $this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer'));
  20. }
  21. public function testSerialize()
  22. {
  23. $obj = new ScalarDummy;
  24. $obj->foo = 'foo';
  25. $obj->xmlFoo = 'xml';
  26. $this->assertEquals('foo', $this->normalizer->normalize($obj, 'json'));
  27. $this->assertEquals('xml', $this->normalizer->normalize($obj, 'xml'));
  28. }
  29. public function testDeserialize()
  30. {
  31. $obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy), 'xml');
  32. $this->assertEquals('foo', $obj->xmlFoo);
  33. $this->assertNull($obj->foo);
  34. $obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy), 'json');
  35. $this->assertEquals('foo', $obj->foo);
  36. $this->assertNull($obj->xmlFoo);
  37. }
  38. public function testSupports()
  39. {
  40. $this->assertTrue($this->normalizer->supports(new \ReflectionClass(get_class(new ScalarDummy))));
  41. $this->assertFalse($this->normalizer->supports(new \ReflectionClass('stdClass')));
  42. }
  43. }