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\NormalizationAwareInterface;
18 class ChainEncoderTest extends TestCase
20 const FORMAT_1 = 'format1';
21 const FORMAT_2 = 'format2';
22 const FORMAT_3 = 'format3';
24 private $chainEncoder;
28 protected function setUp()
30 $this->encoder1 = $this
31 ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
35 ->method('supportsEncoding')
36 ->will($this->returnValueMap(array(
37 array(self::FORMAT_1, true),
38 array(self::FORMAT_2, false),
39 array(self::FORMAT_3, false),
42 $this->encoder2 = $this
43 ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
47 ->method('supportsEncoding')
48 ->will($this->returnValueMap(array(
49 array(self::FORMAT_1, false),
50 array(self::FORMAT_2, true),
51 array(self::FORMAT_3, false),
54 $this->chainEncoder = new ChainEncoder(array($this->encoder1, $this->encoder2));
57 public function testSupportsEncoding()
59 $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
60 $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
61 $this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));
64 public function testEncode()
66 $this->encoder1->expects($this->never())->method('encode');
67 $this->encoder2->expects($this->once())->method('encode');
69 $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_2);
73 * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
75 public function testEncodeUnsupportedFormat()
77 $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_3);
80 public function testNeedsNormalizationBasic()
82 $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1));
83 $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
87 * @dataProvider booleanProvider
89 public function testNeedsNormalizationChainNormalizationAware($bool)
92 ->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder')
95 $chainEncoder->method('supportsEncoding')->willReturn(true);
96 $chainEncoder->method('needsNormalization')->willReturn($bool);
98 $sut = new ChainEncoder(array($chainEncoder));
100 $this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1));
103 public function testNeedsNormalizationNormalizationAware()
105 $encoder = new NormalizationAwareEncoder();
106 $sut = new ChainEncoder(array($encoder));
108 $this->assertFalse($sut->needsNormalization(self::FORMAT_1));
111 public function booleanProvider()
120 class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface
124 class NormalizationAwareEncoder implements NormalizationAwareInterface
126 public function supportsEncoding($format)