GetSetMethodNormalizerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Symfony\Tests\Component\Serializer\Normalizer;
  3. use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  4. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  5. /*
  6. * This file is part of the Symfony framework.
  7. *
  8. * (c) Fabien Potencier <fabien@symfony.com>
  9. *
  10. * This source file is subject to the MIT license that is bundled
  11. * with this source code in the file LICENSE.
  12. */
  13. class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function setUp()
  16. {
  17. $this->normalizer = new GetSetMethodNormalizer;
  18. $this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer'));
  19. }
  20. public function testNormalize()
  21. {
  22. $obj = new GetSetDummy;
  23. $obj->setFoo('foo');
  24. $obj->setBar('bar');
  25. $this->assertEquals(
  26. array('foo' => 'foo', 'bar' => 'bar', 'foobar' => 'foobar'),
  27. $this->normalizer->normalize($obj, 'any'));
  28. }
  29. public function testNormalizeRestricted()
  30. {
  31. $obj = new GetSetDummy;
  32. $obj->setFoo('foo');
  33. $obj->setBar('bar');
  34. $this->assertEquals(
  35. array('foo' => 'foo'),
  36. $this->normalizer->normalize($obj, 'any', array('foo')));
  37. }
  38. public function testDenormalize()
  39. {
  40. $obj = $this->normalizer->denormalize(
  41. array('foo' => 'foo', 'bar' => 'bar', 'foobar' => 'foobar'),
  42. __NAMESPACE__.'\GetSetDummy', 'any');
  43. $this->assertEquals('foo', $obj->getFoo());
  44. $this->assertEquals('bar', $obj->getBar());
  45. }
  46. public function testConstructorDenormalize()
  47. {
  48. $obj = $this->normalizer->denormalize(
  49. array('foo' => 'foo', 'bar' => 'bar', 'foobar' => 'foobar'),
  50. __NAMESPACE__.'\GetConstructorDummy', 'any');
  51. $this->assertEquals('foo', $obj->getFoo());
  52. $this->assertEquals('bar', $obj->getBar());
  53. }
  54. }
  55. class GetSetDummy
  56. {
  57. protected $foo;
  58. private $bar;
  59. public function getFoo()
  60. {
  61. return $this->foo;
  62. }
  63. public function setFoo($foo)
  64. {
  65. $this->foo = $foo;
  66. }
  67. public function getBar()
  68. {
  69. return $this->bar;
  70. }
  71. public function setBar($bar)
  72. {
  73. $this->bar = $bar;
  74. }
  75. public function getFooBar()
  76. {
  77. return $this->foo . $this->bar;
  78. }
  79. public function otherMethod()
  80. {
  81. throw new \RuntimeException("Dummy::otherMethod() should not be called");
  82. }
  83. }
  84. class GetConstructorDummy
  85. {
  86. protected $foo;
  87. private $bar;
  88. public function __construct($foo, $bar)
  89. {
  90. $this->foo = $foo;
  91. $this->bar = $bar;
  92. }
  93. public function getFoo()
  94. {
  95. return $this->foo;
  96. }
  97. public function getBar()
  98. {
  99. return $this->bar;
  100. }
  101. public function otherMethod()
  102. {
  103. throw new \RuntimeException("Dummy::otherMethod() should not be called");
  104. }
  105. }