3 namespace Drupal\Tests\serialization\Unit\Encoder;
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;
12 * @coversDefaultClass \Drupal\serialization\Encoder\XmlEncoder
13 * @group serialization
15 class XmlEncoderTest extends UnitTestCase {
18 * The XmlEncoder instance.
20 * @var \Drupal\serialization\Encoder\XmlEncoder
25 * @var \Symfony\Component\Serializer\Encoder\XmlEncoder|\PHPUnit_Framework_MockObject_MockObject
27 protected $baseEncoder;
30 * An array of test data.
34 protected $testArray = ['test' => 'test'];
36 protected function setUp() {
37 $this->baseEncoder = $this->getMock(BaseXmlEncoder::class);
38 $this->encoder = new XmlEncoder();
39 $this->encoder->setBaseEncoder($this->baseEncoder);
43 * Tests the supportsEncoding() method.
45 public function testSupportsEncoding() {
46 $this->assertTrue($this->encoder->supportsEncoding('xml'));
47 $this->assertFalse($this->encoder->supportsEncoding('json'));
51 * Tests the supportsDecoding() method.
53 public function testSupportsDecoding() {
54 $this->assertTrue($this->encoder->supportsDecoding('xml'));
55 $this->assertFalse($this->encoder->supportsDecoding('json'));
59 * Tests the encode() method.
61 public function testEncode() {
62 $this->baseEncoder->expects($this->once())
64 ->with($this->testArray, 'test', [])
65 ->will($this->returnValue('test'));
67 $this->assertEquals('test', $this->encoder->encode($this->testArray, 'test'));
71 * Tests the decode() method.
73 public function testDecode() {
74 $this->baseEncoder->expects($this->once())
76 ->with('test', 'test', [])
77 ->will($this->returnValue($this->testArray));
79 $this->assertEquals($this->testArray, $this->encoder->decode('test', 'test'));
83 * @covers ::getBaseEncoder
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);
94 $base_encoder->encode(['a' => new TestObject()], 'xml');
101 public function getA() {