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\ChainDecoder;
17 class ChainDecoderTest extends TestCase
19 const FORMAT_1 = 'format1';
20 const FORMAT_2 = 'format2';
21 const FORMAT_3 = 'format3';
23 private $chainDecoder;
27 protected function setUp()
29 $this->decoder1 = $this
30 ->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
34 ->method('supportsDecoding')
35 ->will($this->returnValueMap(array(
36 array(self::FORMAT_1, true),
37 array(self::FORMAT_2, false),
38 array(self::FORMAT_3, false),
41 $this->decoder2 = $this
42 ->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
46 ->method('supportsDecoding')
47 ->will($this->returnValueMap(array(
48 array(self::FORMAT_1, false),
49 array(self::FORMAT_2, true),
50 array(self::FORMAT_3, false),
53 $this->chainDecoder = new ChainDecoder(array($this->decoder1, $this->decoder2));
56 public function testSupportsDecoding()
58 $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
59 $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
60 $this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));
63 public function testDecode()
65 $this->decoder1->expects($this->never())->method('decode');
66 $this->decoder2->expects($this->once())->method('decode');
68 $this->chainDecoder->decode('string_to_decode', self::FORMAT_2);
72 * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
74 public function testDecodeUnsupportedFormat()
76 $this->chainDecoder->decode('string_to_decode', self::FORMAT_3);