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