class XmlEncoderTest

Same name in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\XmlEncoderTest
  2. 8.9.x core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\XmlEncoderTest
  3. 10 core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\XmlEncoderTest

@coversDefaultClass \Drupal\serialization\Encoder\XmlEncoder @group serialization

Hierarchy

Expanded class hierarchy of XmlEncoderTest

File

core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php, line 17

Namespace

Drupal\Tests\serialization\Unit\Encoder
View source
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 = [
        'test' => 'test',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->baseEncoder = $this->createMock(BaseXmlEncoder::class);
        $this->encoder = new XmlEncoder();
        $this->encoder
            ->setBaseEncoder($this->baseEncoder);
    }
    
    /**
     * Tests the supportsEncoding() method.
     */
    public function testSupportsEncoding() : void {
        $this->assertTrue($this->encoder
            ->supportsEncoding('xml'));
        $this->assertFalse($this->encoder
            ->supportsEncoding('json'));
    }
    
    /**
     * Tests the supportsDecoding() method.
     */
    public function testSupportsDecoding() : void {
        $this->assertTrue($this->encoder
            ->supportsDecoding('xml'));
        $this->assertFalse($this->encoder
            ->supportsDecoding('json'));
    }
    
    /**
     * Tests the encode() method.
     */
    public function testEncode() : void {
        $this->baseEncoder
            ->expects($this->once())
            ->method('encode')
            ->with($this->testArray, 'test', [])
            ->willReturn('test');
        $this->assertEquals('test', $this->encoder
            ->encode($this->testArray, 'test'));
    }
    
    /**
     * Tests the decode() method.
     */
    public function testDecode() : void {
        $this->baseEncoder
            ->expects($this->once())
            ->method('decode')
            ->with('test', 'test', [])
            ->willReturn($this->testArray);
        $this->assertEquals($this->testArray, $this->encoder
            ->decode('test', 'test'));
    }
    
    /**
     * @covers ::getBaseEncoder
     */
    public function testDefaultEncoderHasSerializer() : void {
        // The serializer should be set on the Drupal encoder, which should then
        // set it on our default encoder.
        $encoder = new XmlEncoder();
        $serializer = new Serializer([
            new GetSetMethodNormalizer(),
        ]);
        $encoder->setSerializer($serializer);
        $base_encoder = $encoder->getBaseEncoder();
        $this->assertInstanceOf(BaseXmlEncoder::class, $base_encoder);
        // Test the encoder.
        $base_encoder->encode([
            'a' => new TestObject(),
        ], 'xml');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
XmlEncoderTest::$baseEncoder protected property
XmlEncoderTest::$encoder protected property The XmlEncoder instance.
XmlEncoderTest::$testArray protected property An array of test data.
XmlEncoderTest::setUp protected function Overrides UnitTestCase::setUp
XmlEncoderTest::testDecode public function Tests the decode() method.
XmlEncoderTest::testDefaultEncoderHasSerializer public function @covers ::getBaseEncoder
XmlEncoderTest::testEncode public function Tests the encode() method.
XmlEncoderTest::testSupportsDecoding public function Tests the supportsDecoding() method.
XmlEncoderTest::testSupportsEncoding public function Tests the supportsEncoding() method.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.