YamlTest.php

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

Namespace

Drupal\Tests\Core\Serialization

File

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

View source
<?php

namespace Drupal\Tests\Core\Serialization;

use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\Core\Serialization\Yaml
 * @group Serialization
 */
class YamlTest extends UnitTestCase {
    
    /**
     * Test that the overridden serializer is called.
     *
     * @covers ::getSerializer
     * @runInSeparateProcess
     */
    public function testGetSeralization() {
        new Settings([
            'yaml_parser_class' => YamlParserProxy::class,
        ]);
        $this->assertEquals(YamlParserProxy::class, Settings::get('yaml_parser_class'));
        $mock = $this->getMockBuilder('\\stdClass')
            ->setMethods([
            'encode',
            'decode',
            'getFileExtension',
        ])
            ->getMock();
        $mock->expects($this->once())
            ->method('decode');
        YamlParserProxy::setMock($mock);
        Yaml::decode('---');
        new Settings([]);
    }

}
class YamlParserProxy implements SerializationInterface {
    
    /**
     * @var \Drupal\Component\Serialization\SerializationInterface
     */
    protected static $mock;
    public static function setMock($mock) {
        static::$mock = $mock;
    }
    public static function encode($data) {
        return static::$mock->encode($data);
    }
    public static function decode($raw) {
        return static::$mock->decode($raw);
    }
    public static function getFileExtension() {
        return static::$mock->getFileExtension();
    }

}

Classes

Title Deprecated Summary
YamlParserProxy
YamlTest @coversDefaultClass \Drupal\Core\Serialization\Yaml @group Serialization

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