82c64cc149c633f475f2ab5d1c2d810a995ec8b2
[yaffs-website] / serializer / Tests / Encoder / ChainEncoderTest.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\ChainEncoder;
16 use Symfony\Component\Serializer\Encoder\EncoderInterface;
17 use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
18
19 class ChainEncoderTest extends TestCase
20 {
21     const FORMAT_1 = 'format1';
22     const FORMAT_2 = 'format2';
23     const FORMAT_3 = 'format3';
24
25     private $chainEncoder;
26     private $encoder1;
27     private $encoder2;
28
29     protected function setUp()
30     {
31         $this->encoder1 = $this
32             ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
33             ->getMock();
34
35         $this->encoder1
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),
42             )));
43
44         $this->encoder2 = $this
45             ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
46             ->getMock();
47
48         $this->encoder2
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),
54             )));
55
56         $this->chainEncoder = new ChainEncoder(array($this->encoder1, $this->encoder2));
57     }
58
59     public function testSupportsEncoding()
60     {
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')));
65     }
66
67     public function testEncode()
68     {
69         $this->encoder1->expects($this->never())->method('encode');
70         $this->encoder2->expects($this->once())->method('encode');
71
72         $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_2);
73     }
74
75     /**
76      * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
77      */
78     public function testEncodeUnsupportedFormat()
79     {
80         $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_3);
81     }
82
83     public function testNeedsNormalizationBasic()
84     {
85         $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1));
86         $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
87     }
88
89     /**
90      * @dataProvider booleanProvider
91      */
92     public function testNeedsNormalizationChainNormalizationAware($bool)
93     {
94         $chainEncoder = $this
95             ->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder')
96             ->getMock();
97
98         $chainEncoder->method('supportsEncoding')->willReturn(true);
99         $chainEncoder->method('needsNormalization')->willReturn($bool);
100
101         $sut = new ChainEncoder(array($chainEncoder));
102
103         $this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1));
104     }
105
106     public function testNeedsNormalizationNormalizationAware()
107     {
108         $encoder = new NormalizationAwareEncoder();
109         $sut = new ChainEncoder(array($encoder));
110
111         $this->assertFalse($sut->needsNormalization(self::FORMAT_1));
112     }
113
114     public function booleanProvider()
115     {
116         return array(
117             array(true),
118             array(false),
119         );
120     }
121 }
122
123 class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface
124 {
125 }
126
127 class NormalizationAwareEncoder implements EncoderInterface, NormalizationAwareInterface
128 {
129     public function supportsEncoding($format)
130     {
131         return true;
132     }
133
134     public function encode($data, $format, array $context = array())
135     {
136     }
137 }