4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests\Encoder;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Encoder\ChainEncoder;
16 use Symfony\Component\Serializer\Encoder\EncoderInterface;
17 use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
19 class ChainEncoderTest extends TestCase
21 const FORMAT_1 = 'format1';
22 const FORMAT_2 = 'format2';
23 const FORMAT_3 = 'format3';
25 private $chainEncoder;
29 protected function setUp()
31 $this->encoder1 = $this
32 ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
36 ->method('supportsEncoding')
37 ->will($this->returnValueMap(array(
38 array(self::FORMAT_1, array(), true),
39 array(self::FORMAT_2, array(), false),
40 array(self::FORMAT_3, array(), false),
41 array(self::FORMAT_3, array('foo' => 'bar'), true),
44 $this->encoder2 = $this
45 ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
49 ->method('supportsEncoding')
50 ->will($this->returnValueMap(array(
51 array(self::FORMAT_1, array(), false),
52 array(self::FORMAT_2, array(), true),
53 array(self::FORMAT_3, array(), false),
56 $this->chainEncoder = new ChainEncoder(array($this->encoder1, $this->encoder2));
59 public function testSupportsEncoding()
61 $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
62 $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
63 $this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));
64 $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, array('foo' => 'bar')));
67 public function testEncode()
69 $this->encoder1->expects($this->never())->method('encode');
70 $this->encoder2->expects($this->once())->method('encode');
72 $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_2);
76 * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
78 public function testEncodeUnsupportedFormat()
80 $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_3);
83 public function testNeedsNormalizationBasic()
85 $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1));
86 $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
90 * @dataProvider booleanProvider
92 public function testNeedsNormalizationChainNormalizationAware($bool)
95 ->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder')
98 $chainEncoder->method('supportsEncoding')->willReturn(true);
99 $chainEncoder->method('needsNormalization')->willReturn($bool);
101 $sut = new ChainEncoder(array($chainEncoder));
103 $this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1));
106 public function testNeedsNormalizationNormalizationAware()
108 $encoder = new NormalizationAwareEncoder();
109 $sut = new ChainEncoder(array($encoder));
111 $this->assertFalse($sut->needsNormalization(self::FORMAT_1));
114 public function booleanProvider()
123 class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface
127 class NormalizationAwareEncoder implements EncoderInterface, NormalizationAwareInterface
129 public function supportsEncoding($format)
134 public function encode($data, $format, array $context = array())