Commit 29dd37a2 authored by alexpott's avatar alexpott

Issue #2083983 by damiankloip: Unit test Drupal\serialization\Encoder\XmlEncoder class.

parent 4be4a983
......@@ -35,10 +35,26 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
protected $baseEncoder;
/**
* Constucts the XmlEncoder object, creating a BaseXmlEncoder class also.
* Gets the base encoder instance.
*
* @return \Symfony\Component\Serializer\Encoder\XmlEncoder
* The base encoder.
*/
public function getBaseEncoder() {
if (!isset($this->baseEncoder)) {
$this->baseEncoder = new BaseXmlEncoder();
}
return $this->baseEncoder;
}
/**
* Sets the base encoder instance.
*
* @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
*/
public function __construct() {
$this->baseEncoder = new BaseXmlEncoder();
public function setBaseEncoder($encoder) {
$this->baseEncoder = $encoder;
}
/**
......@@ -46,7 +62,7 @@ public function __construct() {
*/
public function encode($data, $format, array $context = array()){
$normalized = $this->serializer->normalize($data, $format, $context);
return $this->baseEncoder->encode($normalized, $format, $context);
return $this->getBaseEncoder()->encode($normalized, $format, $context);
}
/**
......@@ -60,7 +76,7 @@ public function supportsEncoding($format) {
* Implements \Symfony\Component\Serializer\Encoder\EncoderInterface::decode().
*/
public function decode($data, $format, array $context = array()){
return $this->baseEncoder->decode($data, $format, $context);
return $this->getBaseEncoder()->decode($data, $format, $context);
}
/**
......
<?php
/**
* @file
* Contains \Drupal\serialization\Tests\Encoder\XmlEncoderTest.
*/
namespace Drupal\serialization\Tests\Encoder;
use Drupal\serialization\Encoder\XmlEncoder;
use Drupal\Tests\UnitTestCase;
/**
* Tests the XmlEncoder class.
*
* @see \Drupal\serialization\Encoder\XmlEncoder
*/
class XmlEncoderTest extends UnitTestCase {
/**
* The XmlEncoder instance.
*
* @var \Drupal\serialization\Encoder\XmlEncoder
*/
protected $encoder;
/**
* @var \Symfony\Component\Serializer\Encoder\XmlEncoder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $baseEncoder;
/**
* An array of test data.
*
* @var array
*/
protected $testArray = array('test' => 'test');
public static function getInfo() {
return array(
'name' => 'XmlEncoderTest',
'description' => 'Tests the XmlEncoder class.',
'group' => 'Serialization',
);
}
public function setUp() {
$this->baseEncoder = $this->getMock('Symfony\Component\Serializer\Encoder\XmlEncoder');
$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() {
$serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')
->disableOriginalConstructor()
->setMethods(array('normalize'))
->getMock();
$serializer->expects($this->once())
->method('normalize')
->with($this->testArray, 'test', array())
->will($this->returnValue($this->testArray));
$this->encoder->setSerializer($serializer);
$this->baseEncoder->expects($this->once())
->method('encode')
->with($this->testArray, 'test', array())
->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', array())
->will($this->returnValue($this->testArray));
$this->assertEquals($this->testArray, $this->encoder->decode('test', 'test'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment