7d860f65f27f33f799973a005d5f099f13553ee7
[yaffs-website] / serializer / Tests / Encoder / ChainDecoderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Tests\Encoder;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Encoder\ChainDecoder;
16
17 class ChainDecoderTest extends TestCase
18 {
19     const FORMAT_1 = 'format1';
20     const FORMAT_2 = 'format2';
21     const FORMAT_3 = 'format3';
22
23     private $chainDecoder;
24     private $decoder1;
25     private $decoder2;
26
27     protected function setUp()
28     {
29         $this->decoder1 = $this
30             ->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
31             ->getMock();
32
33         $this->decoder1
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),
40             )));
41
42         $this->decoder2 = $this
43             ->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
44             ->getMock();
45
46         $this->decoder2
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),
52             )));
53
54         $this->chainDecoder = new ChainDecoder(array($this->decoder1, $this->decoder2));
55     }
56
57     public function testSupportsDecoding()
58     {
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')));
63     }
64
65     public function testDecode()
66     {
67         $this->decoder1->expects($this->never())->method('decode');
68         $this->decoder2->expects($this->once())->method('decode');
69
70         $this->chainDecoder->decode('string_to_decode', self::FORMAT_2);
71     }
72
73     /**
74      * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
75      */
76     public function testDecodeUnsupportedFormat()
77     {
78         $this->chainDecoder->decode('string_to_decode', self::FORMAT_3);
79     }
80 }