ChainEncoderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Encoder;
  11. use Symfony\Component\Serializer\Encoder\ChainEncoder;
  12. use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
  13. class ChainEncoderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const FORMAT_1 = 'format1';
  16. const FORMAT_2 = 'format2';
  17. const FORMAT_3 = 'format3';
  18. private $chainEncoder;
  19. private $encoder1;
  20. private $encoder2;
  21. protected function setUp()
  22. {
  23. $this->encoder1 = $this
  24. ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
  25. ->getMock();
  26. $this->encoder1
  27. ->method('supportsEncoding')
  28. ->will($this->returnValueMap(array(
  29. array(self::FORMAT_1, true),
  30. array(self::FORMAT_2, false),
  31. array(self::FORMAT_3, false),
  32. )));
  33. $this->encoder2 = $this
  34. ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
  35. ->getMock();
  36. $this->encoder2
  37. ->method('supportsEncoding')
  38. ->will($this->returnValueMap(array(
  39. array(self::FORMAT_1, false),
  40. array(self::FORMAT_2, true),
  41. array(self::FORMAT_3, false),
  42. )));
  43. $this->chainEncoder = new ChainEncoder(array($this->encoder1, $this->encoder2));
  44. }
  45. public function testSupportsEncoding()
  46. {
  47. $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
  48. $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
  49. $this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));
  50. }
  51. public function testEncode()
  52. {
  53. $this->encoder1->expects($this->never())->method('encode');
  54. $this->encoder2->expects($this->once())->method('encode');
  55. $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_2);
  56. }
  57. /**
  58. * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
  59. */
  60. public function testEncodeUnsupportedFormat()
  61. {
  62. $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_3);
  63. }
  64. public function testNeedsNormalizationBasic()
  65. {
  66. $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1));
  67. $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
  68. }
  69. /**
  70. * @dataProvider booleanProvider
  71. */
  72. public function testNeedsNormalizationChainNormalizationAware($bool)
  73. {
  74. $chainEncoder = $this
  75. ->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder')
  76. ->getMock();
  77. $chainEncoder->method('supportsEncoding')->willReturn(true);
  78. $chainEncoder->method('needsNormalization')->willReturn($bool);
  79. $sut = new ChainEncoder(array($chainEncoder));
  80. $this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1));
  81. }
  82. public function testNeedsNormalizationNormalizationAware()
  83. {
  84. $encoder = new NormalizationAwareEncoder();
  85. $sut = new ChainEncoder(array($encoder));
  86. $this->assertFalse($sut->needsNormalization(self::FORMAT_1));
  87. }
  88. public function booleanProvider()
  89. {
  90. return array(
  91. array(true),
  92. array(false),
  93. );
  94. }
  95. }
  96. class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface
  97. {
  98. }
  99. class NormalizationAwareEncoder implements NormalizationAwareInterface
  100. {
  101. public function supportsEncoding($format)
  102. {
  103. return true;
  104. }
  105. }