cbff14a9b45b6f28edc78dc1c30ead92eba715a0
[yaffs-website] / web / core / modules / serialization / tests / src / Unit / Encoder / XmlEncoderTest.php
1 <?php
2
3 namespace Drupal\Tests\serialization\Unit\Encoder;
4
5 use Drupal\serialization\Encoder\XmlEncoder;
6 use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
7 use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
8 use Symfony\Component\Serializer\Serializer;
9 use Drupal\Tests\UnitTestCase;
10
11 /**
12  * @coversDefaultClass \Drupal\serialization\Encoder\XmlEncoder
13  * @group serialization
14  */
15 class XmlEncoderTest extends UnitTestCase {
16
17   /**
18    * The XmlEncoder instance.
19    *
20    * @var \Drupal\serialization\Encoder\XmlEncoder
21    */
22   protected $encoder;
23
24   /**
25    * @var \Symfony\Component\Serializer\Encoder\XmlEncoder|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $baseEncoder;
28
29   /**
30    * An array of test data.
31    *
32    * @var array
33    */
34   protected $testArray = ['test' => 'test'];
35
36   protected function setUp() {
37     $this->baseEncoder = $this->getMock(BaseXmlEncoder::class);
38     $this->encoder = new XmlEncoder();
39     $this->encoder->setBaseEncoder($this->baseEncoder);
40   }
41
42   /**
43    * Tests the supportsEncoding() method.
44    */
45   public function testSupportsEncoding() {
46     $this->assertTrue($this->encoder->supportsEncoding('xml'));
47     $this->assertFalse($this->encoder->supportsEncoding('json'));
48   }
49
50   /**
51    * Tests the supportsDecoding() method.
52    */
53   public function testSupportsDecoding() {
54     $this->assertTrue($this->encoder->supportsDecoding('xml'));
55     $this->assertFalse($this->encoder->supportsDecoding('json'));
56   }
57
58   /**
59    * Tests the encode() method.
60    */
61   public function testEncode() {
62     $this->baseEncoder->expects($this->once())
63       ->method('encode')
64       ->with($this->testArray, 'test', [])
65       ->will($this->returnValue('test'));
66
67     $this->assertEquals('test', $this->encoder->encode($this->testArray, 'test'));
68   }
69
70   /**
71    * Tests the decode() method.
72    */
73   public function testDecode() {
74     $this->baseEncoder->expects($this->once())
75       ->method('decode')
76       ->with('test', 'test', [])
77       ->will($this->returnValue($this->testArray));
78
79     $this->assertEquals($this->testArray, $this->encoder->decode('test', 'test'));
80   }
81
82   /**
83    * @covers ::getBaseEncoder
84    */
85   public function testDefaultEncoderHasSerializer() {
86     // The serializer should be set on the Drupal encoder, which should then
87     // set it on our default encoder.
88     $encoder = new XmlEncoder();
89     $serialzer = new Serializer([new GetSetMethodNormalizer()]);
90     $encoder->setSerializer($serialzer);
91     $base_encoder = $encoder->getBaseEncoder();
92     $this->assertInstanceOf(BaseXmlEncoder::class, $base_encoder);
93     // Test the encoder.
94     $base_encoder->encode(['a' => new TestObject()], 'xml');
95   }
96
97 }
98
99 class TestObject {
100
101   public function getA() {
102     return 'A';
103   }
104
105 }