XmlEncoderTest.php in Drupal 10
File
core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php
View source
<?php
namespace Drupal\Tests\serialization\Unit\Encoder;
use Drupal\serialization\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
use Drupal\Tests\UnitTestCase;
class XmlEncoderTest extends UnitTestCase {
protected $encoder;
protected $baseEncoder;
protected $testArray = [
'test' => 'test',
];
protected function setUp() : void {
$this->baseEncoder = $this
->createMock(BaseXmlEncoder::class);
$this->encoder = new XmlEncoder();
$this->encoder
->setBaseEncoder($this->baseEncoder);
}
public function testSupportsEncoding() {
$this
->assertTrue($this->encoder
->supportsEncoding('xml'));
$this
->assertFalse($this->encoder
->supportsEncoding('json'));
}
public function testSupportsDecoding() {
$this
->assertTrue($this->encoder
->supportsDecoding('xml'));
$this
->assertFalse($this->encoder
->supportsDecoding('json'));
}
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'));
}
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'));
}
public function testDefaultEncoderHasSerializer() {
$encoder = new XmlEncoder();
$serializer = new Serializer([
new GetSetMethodNormalizer(),
]);
$encoder
->setSerializer($serializer);
$base_encoder = $encoder
->getBaseEncoder();
$this
->assertInstanceOf(BaseXmlEncoder::class, $base_encoder);
$base_encoder
->encode([
'a' => new TestObject(),
], 'xml');
}
}
class TestObject {
public function getA() {
return 'A';
}
}