|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Component\Serializer\Encoder\ChainEncoder; |
| 15 | +use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface; |
| 16 | + |
| 17 | +class ChainEncoderTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + const FORMAT_1 = 'format1'; |
| 20 | + const FORMAT_2 = 'format2'; |
| 21 | + const FORMAT_3 = 'format3'; |
| 22 | + |
| 23 | + private $chainEncoder; |
| 24 | + private $encoder1; |
| 25 | + private $encoder2; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $this->encoder1 = $this |
| 30 | + ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface') |
| 31 | + ->getMock(); |
| 32 | + |
| 33 | + $this->encoder1 |
| 34 | + ->method('supportsEncoding') |
| 35 | + ->will($this->returnValueMap(array( |
| 36 | + array(self::FORMAT_1, true), |
| 37 | + array(self::FORMAT_2, false), |
| 38 | + array(self::FORMAT_3, false), |
| 39 | + ))); |
| 40 | + |
| 41 | + $this->encoder2 = $this |
| 42 | + ->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface') |
| 43 | + ->getMock(); |
| 44 | + |
| 45 | + $this->encoder2 |
| 46 | + ->method('supportsEncoding') |
| 47 | + ->will($this->returnValueMap(array( |
| 48 | + array(self::FORMAT_1, false), |
| 49 | + array(self::FORMAT_2, true), |
| 50 | + array(self::FORMAT_3, false), |
| 51 | + ))); |
| 52 | + |
| 53 | + $this->chainEncoder = new ChainEncoder(array($this->encoder1, $this->encoder2)); |
| 54 | + } |
| 55 | + |
| 56 | + public function testSupportsEncoding() |
| 57 | + { |
| 58 | + $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1)); |
| 59 | + $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2)); |
| 60 | + $this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3)); |
| 61 | + } |
| 62 | + |
| 63 | + public function testEncode() |
| 64 | + { |
| 65 | + $this->encoder1->expects($this->never())->method('encode'); |
| 66 | + $this->encoder2->expects($this->once())->method('encode'); |
| 67 | + |
| 68 | + $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_2); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @expectedException Symfony\Component\Serializer\Exception\RuntimeException |
| 73 | + */ |
| 74 | + public function testEncodeUnsupportedFormat() |
| 75 | + { |
| 76 | + $this->chainEncoder->encode(array('foo' => 123), self::FORMAT_3); |
| 77 | + } |
| 78 | + |
| 79 | + public function testNeedsNormalizationBasic() |
| 80 | + { |
| 81 | + $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1)); |
| 82 | + $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @dataProvider booleanProvider |
| 87 | + */ |
| 88 | + public function testNeedsNormalizationChainNormalizationAware($bool) |
| 89 | + { |
| 90 | + $chainEncoder = $this |
| 91 | + ->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder') |
| 92 | + ->getMock(); |
| 93 | + |
| 94 | + $chainEncoder->method('supportsEncoding')->willReturn(true); |
| 95 | + $chainEncoder->method('needsNormalization')->willReturn($bool); |
| 96 | + |
| 97 | + $sut = new ChainEncoder(array($chainEncoder)); |
| 98 | + |
| 99 | + $this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1)); |
| 100 | + } |
| 101 | + |
| 102 | + public function testNeedsNormalizationNormalizationAware() |
| 103 | + { |
| 104 | + $encoder = new NormalizationAwareEncoder(); |
| 105 | + $sut = new ChainEncoder(array($encoder)); |
| 106 | + |
| 107 | + $this->assertFalse($sut->needsNormalization(self::FORMAT_1)); |
| 108 | + } |
| 109 | + |
| 110 | + public function booleanProvider() |
| 111 | + { |
| 112 | + return array( |
| 113 | + array(true), |
| 114 | + array(false), |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface |
| 120 | +{ |
| 121 | +} |
| 122 | + |
| 123 | +class NormalizationAwareEncoder implements NormalizationAwareInterface |
| 124 | +{ |
| 125 | + public function supportsEncoding($format) |
| 126 | + { |
| 127 | + return true; |
| 128 | + } |
| 129 | +} |
0 commit comments