3 namespace Drupal\Tests\Component\Serialization;
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
6 use Drupal\Component\Serialization\YamlSymfony;
9 * Tests the YamlSymfony serialization implementation.
12 * @group Serialization
13 * @coversDefaultClass \Drupal\Component\Serialization\YamlSymfony
15 class YamlSymfonyTest extends YamlTestBase {
18 * Tests encoding and decoding basic data structures.
22 * @dataProvider providerEncodeDecodeTests
24 public function testEncodeDecode($data) {
25 $this->assertEquals($data, YamlSymfony::decode(YamlSymfony::encode($data)));
29 * Tests decoding YAML node anchors.
32 * @dataProvider providerDecodeTests
34 public function testDecode($string, $data) {
35 $this->assertEquals($data, YamlSymfony::decode($string));
39 * Tests our encode settings.
43 public function testEncode() {
44 $this->assertEquals('foo:
45 bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\'
46 ', YamlSymfony::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']]));
50 * @covers ::getFileExtension
52 public function testGetFileExtension() {
53 $this->assertEquals('yml', YamlSymfony::getFileExtension());
57 * Tests that invalid YAML throws an exception.
61 public function testError() {
62 if (method_exists($this, 'expectException')) {
63 $this->expectException(InvalidDataTypeException::class);
66 $this->setExpectedException(InvalidDataTypeException::class);
68 YamlSymfony::decode('foo: [ads');
72 * Ensures that php object support is disabled.
76 public function testObjectSupportDisabled() {
77 if (method_exists($this, 'expectException')) {
78 $this->expectException(InvalidDataTypeException::class);
79 $this->expectExceptionMessage('Object support when dumping a YAML file has been disabled.');
82 $this->setExpectedException(InvalidDataTypeException::class, 'Object support when dumping a YAML file has been disabled.');
84 $object = new \stdClass();
86 YamlSymfony::encode([$object]);