GetSetMethodNormalizerTest.php 2.6 KB

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