class YamlTest

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

Tests the Yaml serialization implementation.

@group Drupal @group Serialization @coversDefaultClass \Drupal\Component\Serialization\Yaml

Hierarchy

  • class \Drupal\Tests\Component\Serialization\YamlTestBase extends \PHPUnit\Framework\TestCase
    • class \Drupal\Tests\Component\Serialization\YamlTest extends \Drupal\Tests\Component\Serialization\YamlTestBase

Expanded class hierarchy of YamlTest

File

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

Namespace

Drupal\Tests\Component\Serialization
View source
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);
    }

}

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 @covers ::getFileExtension
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.