GetSetMethodNormalizerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. }
  30. public function testNormalizeRestricted()
  31. {
  32. $obj = new GetSetDummy;
  33. $obj->setFoo('foo');
  34. $obj->setBar('bar');
  35. $this->assertEquals(
  36. array('foo' => 'foo'),
  37. $this->normalizer->normalize($obj, 'any', array('foo'))
  38. );
  39. }
  40. public function testDenormalize()
  41. {
  42. $obj = $this->normalizer->denormalize(
  43. array('foo' => 'foo', 'bar' => 'bar', 'foobar' => 'foobar'),
  44. __NAMESPACE__.'\GetSetDummy',
  45. 'any'
  46. );
  47. $this->assertEquals('foo', $obj->getFoo());
  48. $this->assertEquals('bar', $obj->getBar());
  49. }
  50. public function testConstructorDenormalize()
  51. {
  52. $obj = $this->normalizer->denormalize(
  53. array('foo' => 'foo', 'bar' => 'bar', 'foobar' => 'foobar'),
  54. __NAMESPACE__.'\GetConstructorDummy', 'any');
  55. $this->assertEquals('foo', $obj->getFoo());
  56. $this->assertEquals('bar', $obj->getBar());
  57. }
  58. }
  59. class GetSetDummy
  60. {
  61. protected $foo;
  62. private $bar;
  63. public function getFoo()
  64. {
  65. return $this->foo;
  66. }
  67. public function setFoo($foo)
  68. {
  69. $this->foo = $foo;
  70. }
  71. public function getBar()
  72. {
  73. return $this->bar;
  74. }
  75. public function setBar($bar)
  76. {
  77. $this->bar = $bar;
  78. }
  79. public function getFooBar()
  80. {
  81. return $this->foo . $this->bar;
  82. }
  83. public function otherMethod()
  84. {
  85. throw new \RuntimeException("Dummy::otherMethod() should not be called");
  86. }
  87. }
  88. class GetConstructorDummy
  89. {
  90. protected $foo;
  91. private $bar;
  92. public function __construct($foo, $bar)
  93. {
  94. $this->foo = $foo;
  95. $this->bar = $bar;
  96. }
  97. public function getFoo()
  98. {
  99. return $this->foo;
  100. }
  101. public function getBar()
  102. {
  103. return $this->bar;
  104. }
  105. public function otherMethod()
  106. {
  107. throw new \RuntimeException("Dummy::otherMethod() should not be called");
  108. }
  109. }