You are here

class XmlEncoderTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/serialization/tests/src/Unit/Encoder/XmlEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\XmlEncoderTest
  2. 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 15

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',
  ];
  protected function setUp() : void {
    $this->baseEncoder = $this
      ->createMock(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();
    $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

Namesort descending Modifiers Type Description Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
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.