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, array(), true),
37 array(self::FORMAT_2, array(), false),
38 array(self::FORMAT_3, array(), false),
39 array(self::FORMAT_3, array('foo' => 'bar'), true),
42 $this->decoder2 = $this
43 ->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
47 ->method('supportsDecoding')
48 ->will($this->returnValueMap(array(
49 array(self::FORMAT_1, array(), false),
50 array(self::FORMAT_2, array(), true),
51 array(self::FORMAT_3, array(), false),
54 $this->chainDecoder = new ChainDecoder(array($this->decoder1, $this->decoder2));
57 public function testSupportsDecoding()
59 $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
60 $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
61 $this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));
62 $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, array('foo' => 'bar')));
65 public function testDecode()
67 $this->decoder1->expects($this->never())->method('decode');
68 $this->decoder2->expects($this->once())->method('decode');
70 $this->chainDecoder->decode('string_to_decode', self::FORMAT_2);
74 * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
76 public function testDecodeUnsupportedFormat()
78 $this->chainDecoder->decode('string_to_decode', self::FORMAT_3);