GetSetMethodNormalizerTest.php 2.6 KB

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