YamlTest.php

Same filename and directory in other branches
  1. 9 core/tests/Drupal/Tests/Core/Serialization/YamlTest.php
  2. 9 core/tests/Drupal/Tests/Component/Serialization/YamlTest.php
  3. 8.9.x core/tests/Drupal/Tests/Core/Serialization/YamlTest.php
  4. 8.9.x core/tests/Drupal/Tests/Component/Serialization/YamlTest.php
  5. 11.x core/tests/Drupal/Tests/Component/Serialization/YamlTest.php

Namespace

Drupal\Tests\Component\Serialization

File

core/tests/Drupal/Tests/Component/Serialization/YamlTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Component\Serialization;

use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;

/**
 * Tests the Yaml serialization implementation.
 *
 * @group Drupal
 * @group Serialization
 * @coversDefaultClass \Drupal\Component\Serialization\Yaml
 */
class YamlTest extends YamlTestBase {
  
  /**
   * Tests encoding and decoding basic data structures.
   *
   * @covers ::encode
   * @covers ::decode
   * @dataProvider providerEncodeDecodeTests
   */
  public function testEncodeDecode(array $data) : void {
    $this->assertSame($data, Yaml::decode(Yaml::encode($data)));
  }
  
  /**
   * Tests decoding YAML node anchors.
   *
   * @covers ::decode
   * @dataProvider providerDecodeTests
   */
  public function testDecode($string, $data) : void {
    $this->assertSame($data, Yaml::decode($string));
  }
  
  /**
   * Tests our encode settings.
   *
   * @covers ::encode
   */
  public function testEncode() : void {
    // cSpell:disable
    $this->assertSame('foo:
  bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\'
', Yaml::encode([
      'foo' => [
        'bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis',
      ],
    ]));
    // cSpell:enable
  }
  
  /**
   * @covers ::getFileExtension
   */
  public function testGetFileExtension() : void {
    $this->assertSame('yml', Yaml::getFileExtension());
  }
  
  /**
   * Tests that invalid YAML throws an exception.
   *
   * @covers ::decode
   */
  public function testError() : void {
    $this->expectException(InvalidDataTypeException::class);
    Yaml::decode('foo: [ads');
  }
  
  /**
   * Ensures that php object support is disabled.
   *
   * @covers ::encode
   */
  public function testEncodeObjectSupportDisabled() : void {
    $this->expectException(InvalidDataTypeException::class);
    $this->expectExceptionMessage('Object support when dumping a YAML file has been disabled.');
    $object = new \stdClass();
    $object->foo = 'bar';
    Yaml::encode([
      $object,
    ]);
  }
  
  /**
   * Ensures that decoding PHP objects does not work in Symfony.
   *
   * @covers ::decode
   */
  public function testDecodeObjectSupportDisabled() : void {
    $this->expectException(InvalidDataTypeException::class);
    $this->expectExceptionMessageMatches('/^Object support when parsing a YAML file has been disabled/');
    $yaml = <<<YAML
obj: !php/object "O:8:\\"stdClass\\":1:{s:3:\\"foo\\";s:3:\\"bar\\";}"
YAML;
    Yaml::decode($yaml);
  }

}

Classes

Title Deprecated Summary
YamlTest Tests the Yaml serialization implementation.

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