class YamlTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Component/Serialization/YamlTest.php \Drupal\Tests\Component\Serialization\YamlTest
  2. 10 core/tests/Drupal/Tests/Component/Serialization/YamlTest.php \Drupal\Tests\Component\Serialization\YamlTest
  3. 9 core/tests/Drupal/Tests/Core/Serialization/YamlTest.php \Drupal\Tests\Core\Serialization\YamlTest
  4. 9 core/tests/Drupal/Tests/Component/Serialization/YamlTest.php \Drupal\Tests\Component\Serialization\YamlTest
  5. 8.9.x core/tests/Drupal/Tests/Core/Serialization/YamlTest.php \Drupal\Tests\Core\Serialization\YamlTest
  6. 8.9.x core/tests/Drupal/Tests/Component/Serialization/YamlTest.php \Drupal\Tests\Component\Serialization\YamlTest

Tests the Yaml serialization implementation.

Attributes

#[CoversClass(Yaml::class)] #[Group('Drupal')] #[Group('Serialization')]

Hierarchy

Expanded class hierarchy of YamlTest

File

core/tests/Drupal/Tests/Component/Serialization/YamlTest.php, line 16

Namespace

Drupal\Tests\Component\Serialization
View source
class YamlTest extends YamlTestBase {
  
  /**
   * Tests encoding and decoding basic data structures.
   *
   * @legacy-covers ::encode
   * @legacy-covers ::decode
   */
  public function testEncodeDecode(array $data) : void {
    $this->assertSame($data, Yaml::decode(Yaml::encode($data)));
  }
  
  /**
   * Tests decoding YAML node anchors.
   */
  public function testDecode($string, $data) : void {
    $this->assertSame($data, Yaml::decode($string));
  }
  
  /**
   * Tests our encode settings.
   */
  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
  }
  
  /**
   * Tests get file extension.
   */
  public function testGetFileExtension() : void {
    $this->assertSame('yml', Yaml::getFileExtension());
  }
  
  /**
   * Tests that invalid YAML throws an exception.
   *
   * @legacy-covers ::decode
   */
  public function testError() : void {
    $this->expectException(InvalidDataTypeException::class);
    Yaml::decode('foo: [ads');
  }
  
  /**
   * Ensures that php object support is disabled.
   */
  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.
   */
  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);
  }
  
  /**
   * Tests decoding PHP constants.
   *
   * @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledPecl()
   */
  public function testConstants() : void {
    $yaml = <<<YAML
    foo:
      !php/const PHP_INT_MAX
    YAML;
    $this->assertSame(PHP_INT_MAX, Yaml::decode($yaml)['foo']);
  }
  
  /**
   * Tests that missing constants cause exception.
   */
  public function testUndefinedConstant() : void {
    $this->expectExceptionMessage('The constant "DOES_NOT_EXIST" is not defined');
    $yaml = <<<YAML
    foo:
      !php/const DOES_NOT_EXIST
    YAML;
    Yaml::decode($yaml);
  }
  
  /**
   * Tests that enums can be encoded and parsed.
   */
  public function testEnums() : void {
    $data = [
      'foo' => EnumValue::Yes,
      'bar' => BackedEnumValue::Maybe,
    ];
    $yaml = Yaml::encode($data);
    $this->assertSame($yaml, <<<YAML
    foo: !php/enum Drupal\\Tests\\Component\\Serialization\\EnumValue::Yes
    bar: !php/enum Drupal\\Tests\\Component\\Serialization\\BackedEnumValue::Maybe
    
    YAML);
    // Test decoding of `!php/enum`-encoded enums.
    $decoded = Yaml::decode($yaml);
    $this->assertSame(EnumValue::Yes, $decoded['foo']);
    $this->assertSame(BackedEnumValue::Maybe, $decoded['bar']);
    // Test decoding of `!php/const`-encoded enums.
    $yaml_alternative = <<<YAML
    foo: !php/const Drupal\\Tests\\Component\\Serialization\\EnumValue::Yes
    bar: !php/const Drupal\\Tests\\Component\\Serialization\\BackedEnumValue::Maybe
    
    YAML;
    $decoded = Yaml::decode($yaml_alternative);
    $this->assertSame(EnumValue::Yes, $decoded['foo']);
    $this->assertSame(BackedEnumValue::Maybe, $decoded['bar']);
    // Test decoding of `!php/enum`-encoded arrow values.
    $yaml_alternative = <<<YAML
    bar: !php/enum Drupal\\Tests\\Component\\Serialization\\BackedEnumValue::Maybe->value
    
    YAML;
    $decoded = Yaml::decode($yaml_alternative);
    $this->assertSame(BackedEnumValue::Maybe->value, $decoded['bar']);
  }

}

Members

Title Sort descending Modifiers Object type Summary
YamlTest::testDecode public function Tests decoding YAML node anchors.
YamlTest::testDecodeObjectSupportDisabled public function Ensures that decoding PHP objects does not work in Symfony.
YamlTest::testEncode public function Tests our encode settings.
YamlTest::testEncodeDecode public function Tests encoding and decoding basic data structures.
YamlTest::testEncodeObjectSupportDisabled public function Ensures that php object support is disabled.
YamlTest::testError public function Tests that invalid YAML throws an exception.
YamlTest::testGetFileExtension public function Tests get file extension.
YamlTestBase::providerBoolTest public static function Tests different boolean serialization and deserialization.
YamlTestBase::providerDecodeTests public static function Some data that should be able to be deserialized.
YamlTestBase::providerEncodeDecodeTests public static function Some data that should be able to be serialized.

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