X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fserialization%2Ftests%2Fsrc%2FUnit%2FEncoder%2FXmlEncoderTest.php;fp=web%2Fcore%2Fmodules%2Fserialization%2Ftests%2Fsrc%2FUnit%2FEncoder%2FXmlEncoderTest.php;h=a347982b372fd6cb0b43ca77910d9449d9683b2a;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php b/web/core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php new file mode 100644 index 000000000..a347982b3 --- /dev/null +++ b/web/core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php @@ -0,0 +1,104 @@ + 'test']; + + protected function setUp() { + $this->baseEncoder = $this->getMock(BaseXmlEncoder::class); + $this->encoder = new XmlEncoder(); + $this->encoder->setBaseEncoder($this->baseEncoder); + } + + /** + * Tests the supportsEncoding() method. + */ + public function testSupportsEncoding() { + $this->assertTrue($this->encoder->supportsEncoding('xml')); + $this->assertFalse($this->encoder->supportsEncoding('json')); + } + + /** + * Tests the supportsDecoding() method. + */ + public function testSupportsDecoding() { + $this->assertTrue($this->encoder->supportsDecoding('xml')); + $this->assertFalse($this->encoder->supportsDecoding('json')); + } + + /** + * Tests the encode() method. + */ + public function testEncode() { + $this->baseEncoder->expects($this->once()) + ->method('encode') + ->with($this->testArray, 'test', []) + ->will($this->returnValue('test')); + + $this->assertEquals('test', $this->encoder->encode($this->testArray, 'test')); + } + + /** + * Tests the decode() method. + */ + public function testDecode() { + $this->baseEncoder->expects($this->once()) + ->method('decode') + ->with('test', 'test', []) + ->will($this->returnValue($this->testArray)); + + $this->assertEquals($this->testArray, $this->encoder->decode('test', 'test')); + } + + /** + * @covers ::getBaseEncoder + */ + public function testDefaultEncoderHasSerializer() { + // The serializer should be set on the Drupal encoder, which should then + // set it on our default encoder. + $encoder = new XmlEncoder(); + $serialzer = new Serializer([new GetSetMethodNormalizer()]); + $encoder->setSerializer($serialzer); + $base_encoder = $encoder->getBaseEncoder(); + $this->assertInstanceOf(BaseXmlEncoder::class, $base_encoder); + // Test the encoder. + $base_encoder->encode(['a' => new TestObject()], 'xml'); + } + +} + +class TestObject { + public function getA() { + return 'A'; + } + +}