3 namespace Drupal\Tests\Component\Serialization;
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
6 use Drupal\Component\Serialization\YamlPecl;
9 * Tests the YamlPecl serialization implementation.
12 * @group Serialization
13 * @coversDefaultClass \Drupal\Component\Serialization\YamlPecl
14 * @requires extension yaml
16 class YamlPeclTest extends YamlTestBase {
19 * Tests encoding and decoding basic data structures.
23 * @dataProvider providerEncodeDecodeTests
25 public function testEncodeDecode($data) {
26 $this->assertEquals($data, YamlPecl::decode(YamlPecl::encode($data)));
30 * Ensures that php object support is disabled.
32 public function testObjectSupportDisabled() {
33 $object = new \stdClass();
35 $this->assertEquals(['O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}'], YamlPecl::decode(YamlPecl::encode([$object])));
36 $this->assertEquals(0, ini_get('yaml.decode_php'));
40 * Tests decoding YAML node anchors.
43 * @dataProvider providerDecodeTests
45 public function testDecode($string, $data) {
46 $this->assertEquals($data, YamlPecl::decode($string));
50 * Tests our encode settings.
54 public function testEncode() {
55 $this->assertEquals('---
57 bar: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis
59 ', YamlPecl::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']]));
63 * Tests YAML boolean callback.
65 * @param string $string
66 * String value for the YAML boolean.
67 * @param string|bool $expected
68 * The expected return value.
70 * @covers ::applyBooleanCallbacks
71 * @dataProvider providerBoolTest
73 public function testApplyBooleanCallbacks($string, $expected) {
74 $this->assertEquals($expected, YamlPecl::applyBooleanCallbacks($string, 'bool', NULL));
78 * @covers ::getFileExtension
80 public function testGetFileExtension() {
81 $this->assertEquals('yml', YamlPecl::getFileExtension());
85 * Tests that invalid YAML throws an exception.
87 * @covers ::errorHandler
89 public function testError() {
90 if (method_exists($this, 'expectException')) {
91 $this->expectException(InvalidDataTypeException::class);
94 $this->setExpectedException(InvalidDataTypeException::class);
96 YamlPecl::decode('foo: [ads');